Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bottlecap/src/bin/bottlecap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ async fn extension_loop_active(
trace_processor.clone(),
trace_agent_channel.clone(),
stats_concentrator.clone(),
Arc::clone(&proxy_aggregator),
);

// Validate and get the appropriate flush strategy for the current mode
Expand Down Expand Up @@ -1391,6 +1392,7 @@ fn start_otlp_agent(
trace_processor: Arc<dyn trace_processor::TraceProcessor + Send + Sync>,
trace_tx: Sender<SendDataBuilderInfo>,
stats_concentrator: StatsConcentratorHandle,
proxy_aggregator: Arc<TokioMutex<proxy_aggregator::Aggregator>>,
) -> Option<CancellationToken> {
if !should_enable_otlp_agent(config) {
return None;
Expand All @@ -1405,6 +1407,7 @@ fn start_otlp_agent(
trace_processor,
trace_tx,
stats_generator,
proxy_aggregator,
cancel_token.clone(),
);

Expand Down
12 changes: 12 additions & 0 deletions bottlecap/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ pub struct LambdaConfig {
/// present in the Lambda event payload (`DD_DSM_KAFKA_GROUP`).
pub dsm_kafka_group: Option<String>,

/// `DD_METRICS_OTEL_ENABLED`: the tracer libraries' flag for sending `OpenTelemetry`
/// metrics to `localhost:4318`. When set, the extension also accepts
/// `/v1/metrics` and forwards it to Datadog's OTLP intake, as
/// `DD_OTLP_CONFIG_METRICS_ENABLED` does.
pub metrics_otel_enabled: bool,

/// `DD_TRACE_EXPERIMENTAL_FEATURES_ENABLED`: gates `additional_metric_tags` and
/// `additional_metric_tags_cardinality_limit` below, matching the Serverless
/// Compatibility Layer (`datadog-trace-agent`).
Expand Down Expand Up @@ -187,6 +193,7 @@ impl Default for LambdaConfig {
dsm_consume_enabled: false,
dsm_exchange_name: None,
dsm_kafka_group: None,
metrics_otel_enabled: false,
trace_experimental_features_enabled: false,
additional_metric_tags: Vec::new(),
additional_metric_tags_cardinality_limit: None,
Expand Down Expand Up @@ -326,6 +333,10 @@ pub struct LambdaConfigSource {
#[serde(deserialize_with = "deser_opt_str")]
pub dsm_kafka_group: Option<String>,

/// `DD_METRICS_OTEL_ENABLED`: see `LambdaConfig::metrics_otel_enabled`.
#[serde(deserialize_with = "deser_opt_bool")]
pub metrics_otel_enabled: Option<bool>,

/// `DD_TRACE_EXPERIMENTAL_FEATURES_ENABLED`: see `LambdaConfig::trace_experimental_features_enabled`.
#[serde(deserialize_with = "deser_opt_bool")]
pub trace_experimental_features_enabled: Option<bool>,
Expand Down Expand Up @@ -367,6 +378,7 @@ impl DatadogConfigExtension for LambdaConfig {
api_security_sample_delay,
trace_remove_integration_service_names_enabled,
lambda_durable_function_log_buffer_size,
metrics_otel_enabled,
trace_experimental_features_enabled,
],
option: [span_dedup_timeout, api_key_secret_reload_interval, appsec_rules, serverless_error_sampler_enabled, dsm_exchange_name, dsm_kafka_group],
Expand Down
229 changes: 215 additions & 14 deletions bottlecap/src/otlp/agent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use axum::{
Router,
extract::{Request, State},
http::{StatusCode, header},
http::{HeaderValue, StatusCode, header},
response::{IntoResponse, Response},
routing::post,
};
Expand All @@ -14,17 +14,24 @@ use opentelemetry_proto::tonic::collector::trace::v1::{
use prost::Message;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::{net::TcpListener, sync::mpsc::Sender};
use tokio::{
net::TcpListener,
sync::{Mutex, mpsc::Sender},
};
use tokio_util::sync::CancellationToken;
use tonic_types::Status as ProtoStatus;
use tracing::{debug, error};

use crate::{
config::Config,
http::{extract_request_body, handler_not_found},
otlp::processor::{OtlpEncoding, Processor as OtlpProcessor},
otlp::{
processor::{OtlpEncoding, Processor as OtlpProcessor},
should_enable_otlp_metrics, should_enable_otlp_traces,
},
tags::provider,
traces::{
proxy_aggregator::{self, ProxyRequest},
stats_generator::StatsGenerator,
trace_aggregator::SendDataBuilderInfo,
trace_processor::{StatsComputedBy, TraceProcessor},
Expand All @@ -36,6 +43,19 @@ const OTLP_AGENT_GRPC_PORT: u16 = 4317;
const DEFAULT_MAX_RECV_MSG_SIZE: usize = 4 * 1024 * 1024; // 4MB default
const MAX_RECV_MSG_SIZE_CAP: usize = 50 * 1024 * 1024; // 50MB cap (matches trace agent)

const OTLP_METRICS_PATH: &str = "/v1/metrics";
const DD_OTEL_METRIC_CONFIG_HEADER: &str = "dd-otel-metric-config";
// Turns resource attributes (service, env, runtime-id, DD_TAGS) into metric tags.
Comment on lines +47 to +48

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is runtime-id unconditionally included in the attributes? My concern is it could trigger high metric storage due to its unique cardinality.

const DD_OTEL_METRIC_CONFIG: &str = r#"{"resource_attributes_as_tags": true}"#;

/// State for `/v1/metrics`: requests are queued unchanged and sent to Datadog's OTLP intake
/// by the proxy flusher.
#[derive(Clone)]
struct MetricsProxy {
config: Arc<Config>,
proxy_aggregator: Arc<Mutex<proxy_aggregator::Aggregator>>,
}

/// Shared state and trace processing pipeline used by both HTTP and gRPC handlers.
#[derive(Clone)]
struct TracePipeline {
Expand Down Expand Up @@ -140,6 +160,8 @@ impl TraceService for TracePipeline {

pub struct Agent {
pipeline: TracePipeline,
metrics_proxy: Option<MetricsProxy>,
traces_enabled: bool,
cancel_token: CancellationToken,
http_port: Option<u16>,
grpc_port: Option<u16>,
Expand All @@ -152,18 +174,27 @@ impl Agent {
trace_processor: Arc<dyn TraceProcessor + Send + Sync>,
trace_tx: Sender<SendDataBuilderInfo>,
stats_generator: Arc<StatsGenerator>,
proxy_aggregator: Arc<Mutex<proxy_aggregator::Aggregator>>,
cancel_token: CancellationToken,
) -> Self {
let http_port = config
.otlp_config_receiver_protocols_http_endpoint
.as_ref()
.map(|ep| Self::parse_port(Some(ep), OTLP_AGENT_HTTP_PORT));
let traces_enabled = should_enable_otlp_traces(&config);
let metrics_enabled = should_enable_otlp_metrics(&config);

let http_endpoint = config.otlp_config_receiver_protocols_http_endpoint.as_ref();
let http_port = (metrics_enabled || (traces_enabled && http_endpoint.is_some()))
.then(|| Self::parse_port(http_endpoint, OTLP_AGENT_HTTP_PORT));

let grpc_port = config
.otlp_config_receiver_protocols_grpc_endpoint
.as_ref()
.filter(|_| traces_enabled)
.map(|ep| Self::parse_port(Some(ep), OTLP_AGENT_GRPC_PORT));

let metrics_proxy = metrics_enabled.then(|| MetricsProxy {
config: Arc::clone(&config),
proxy_aggregator,
});

let pipeline = TracePipeline {
config: Arc::clone(&config),
tags_provider: Arc::clone(&tags_provider),
Expand All @@ -175,6 +206,8 @@ impl Agent {

Self {
pipeline,
metrics_proxy,
traces_enabled,
cancel_token,
http_port,
grpc_port,
Expand Down Expand Up @@ -208,11 +241,14 @@ impl Agent {
let mut handles: Vec<tokio::task::JoinHandle<()>> = Vec::new();

if let Some(port) = self.http_port {
let pipeline = self.pipeline.clone();
let router = Self::http_router(
self.traces_enabled.then(|| self.pipeline.clone()),
self.metrics_proxy.clone(),
);
let cancel_token = self.cancel_token.clone();

handles.push(tokio::spawn(async move {
if let Err(e) = Self::start_http(port, pipeline, cancel_token).await {
if let Err(e) = Self::start_http(port, router, cancel_token).await {
error!("OTLP HTTP agent error: {e:?}");
}
}));
Expand Down Expand Up @@ -241,16 +277,31 @@ impl Agent {
Ok(())
}

fn http_router(pipeline: Option<TracePipeline>, metrics_proxy: Option<MetricsProxy>) -> Router {
let mut router = Router::new();
if let Some(pipeline) = pipeline {
router = router.merge(
Router::new()
.route("/v1/traces", post(Self::v1_traces))
.with_state(pipeline),
);
}
if let Some(metrics_proxy) = metrics_proxy {
router = router.merge(
Router::new()
.route(OTLP_METRICS_PATH, post(Self::v1_metrics))
.with_state(metrics_proxy),
);
}
router.fallback(handler_not_found)
}

async fn start_http(
port: u16,
pipeline: TracePipeline,
router: Router,
cancel_token: CancellationToken,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let socket = SocketAddr::from(([127, 0, 0, 1], port));
let router = Router::new()
.route("/v1/traces", post(Self::v1_traces))
.fallback(handler_not_found)
.with_state(pipeline);

let listener = TcpListener::bind(&socket).await?;
debug!("OTLP HTTP | Starting collector on {}", socket);
Expand Down Expand Up @@ -366,6 +417,49 @@ impl Agent {
}
}

async fn v1_metrics(State(metrics_proxy): State<MetricsProxy>, request: Request) -> Response {
let encoding = OtlpEncoding::from_content_type(
request
.headers()
.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok()),
);

let (mut parts, body) = match extract_request_body(request).await {
Ok(r) => r,
Err(e) => {
error!("OTLP HTTP | Failed to extract metrics request body: {e}");
return Self::otlp_error_response(
StatusCode::BAD_REQUEST,
format!("Failed to extract request body: {e}"),
encoding,
);
}
};

parts.headers.insert(
DD_OTEL_METRIC_CONFIG_HEADER,
HeaderValue::from_static(DD_OTEL_METRIC_CONFIG),
);
let proxy_request = ProxyRequest {
headers: parts.headers,
body,
target_url: format!(
"https://otlp.{}{OTLP_METRICS_PATH}",
metrics_proxy.config.site
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is hardcoded, nothing can redirect this URL, so otlp_metrics_forwarding_test.rs can't run the real binary. DD_SITE doesn't help: the URL is always https://otlp., so it can't point at the harness's http://127.0.0.1: intake which goes against our tests/HARNESS.md where No test reads internal state. DSM already does this with the same proxy aggregator, you may want to use that as reference.

};
metrics_proxy
.proxy_aggregator
.lock()
.await
.add(proxy_request);
debug!("OTLP HTTP | Queued metrics for the OTLP intake");

// An empty trace export response encodes the same as an empty metrics one.
Self::otlp_success_response(encoding)
}

fn otlp_error_response(
status_code: StatusCode,
message: String,
Expand Down Expand Up @@ -451,9 +545,116 @@ impl Agent {
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;

use axum::body::Body;
use tower::ServiceExt;

fn metrics_proxy() -> MetricsProxy {
MetricsProxy {
config: Arc::new(Config {
site: "datadoghq.eu".to_string(),
..Config::default()
}),
proxy_aggregator: Arc::new(Mutex::new(proxy_aggregator::Aggregator::default())),
}
}

fn post_request(path: &str, content_type: &str, body: &'static [u8]) -> Request {
Request::builder()
.method("POST")
.uri(path)
.header(header::CONTENT_TYPE, content_type)
.body(Body::from(body))
.unwrap()
}

#[tokio::test]
async fn test_v1_metrics_queues_request_for_otlp_intake() {
let metrics_proxy = metrics_proxy();
let router = Agent::http_router(None, Some(metrics_proxy.clone()));

let response = router
.oneshot(post_request(
"/v1/metrics",
"application/x-protobuf",
b"\x0a\x02\x08\x01",
))
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get(header::CONTENT_TYPE).unwrap(),
"application/x-protobuf"
);

let batch = metrics_proxy.proxy_aggregator.lock().await.get_batch();
assert_eq!(batch.len(), 1);
let request = batch.first().unwrap();
assert_eq!(request.target_url, "https://otlp.datadoghq.eu/v1/metrics");
assert_eq!(request.body.as_ref(), b"\x0a\x02\x08\x01");
assert_eq!(
request.headers.get(header::CONTENT_TYPE).unwrap(),
"application/x-protobuf"
);
assert_eq!(
request.headers.get(DD_OTEL_METRIC_CONFIG_HEADER).unwrap(),
DD_OTEL_METRIC_CONFIG
);
}

#[tokio::test]
async fn test_v1_metrics_replies_json_for_json_request() {
let router = Agent::http_router(None, Some(metrics_proxy()));

let response = router
.oneshot(post_request("/v1/metrics", "application/json", b"{}"))
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get(header::CONTENT_TYPE).unwrap(),
"application/json"
);
}

#[tokio::test]
async fn test_v1_metrics_not_found_when_metrics_disabled() {
let router = Agent::http_router(None, None);

let response = router
.oneshot(post_request("/v1/metrics", "application/x-protobuf", b""))
.await
.unwrap();

assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn test_v1_traces_not_found_when_only_metrics_enabled() {
let metrics_proxy = metrics_proxy();
let router = Agent::http_router(None, Some(metrics_proxy.clone()));

let response = router
.oneshot(post_request("/v1/traces", "application/x-protobuf", b""))
.await
.unwrap();

assert_eq!(response.status(), StatusCode::NOT_FOUND);
assert!(
metrics_proxy
.proxy_aggregator
.lock()
.await
.get_batch()
.is_empty()
);
}

#[test]
fn test_parse_port_with_valid_endpoint() {
let endpoint = Some("localhost:8080".to_string());
Expand Down
Loading
Loading