-
Notifications
You must be signed in to change notification settings - Fork 19
feat(otel): add support for OTLP trace export #1641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 38 commits into
main
from
rachel.yang/OTLP-trace-export
Mar 27, 2026
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
1e86c8d
WIP: OTLP trace export
rachelyangdog 01662c2
linting
rachelyangdog 91a6559
linting on comments
rachelyangdog 293ecd8
fix endpoint and mapping
rachelyangdog 2dd4b57
implement feedback
rachelyangdog 42de22c
lint and private fields
rachelyangdog f359ff5
send_with_retry and move to libdd_trace_utils
rachelyangdog 9bbc48e
lint + nits
rachelyangdog db08608
lint
rachelyangdog f1f7921
config change and sampling update
rachelyangdog 82435cb
Merge branch 'main' into rachel.yang/OTLP-trace-export
rachelyangdog e1b519b
linting issues
rachelyangdog 8773444
Merge branch 'main' into rachel.yang/OTLP-trace-export
rachelyangdog c0cfacf
merge refactor and update
rachelyangdog 679110c
add test
rachelyangdog eeeb68f
more edits
rachelyangdog 381c488
header change
rachelyangdog ad516d6
edits from review
rachelyangdog 2ab492f
linting
rachelyangdog 9d5e0f6
Merge branch 'main' into rachel.yang/OTLP-trace-export
rachelyangdog e59b071
lint
rachelyangdog 66e5b43
zach review
rachelyangdog a662508
fix lint and errors
rachelyangdog eec9836
revisions
rachelyangdog e4ca73c
revisions part 2
rachelyangdog 39e639b
lint and cargo
rachelyangdog 1c7f3dd
span name edit
rachelyangdog b964349
error message update
rachelyangdog 7f9cf75
service.name and scope update
rachelyangdog 1285cd1
update codeowners
rachelyangdog 7a031ef
remove some attributes and refactor
rachelyangdog 2b376c8
linter
rachelyangdog e8c15c1
refactor tests
rachelyangdog 4acc420
update codeowners
rachelyangdog 1b6d3f4
new attributes
rachelyangdog 8fc9a14
add metastruct and tracestate
rachelyangdog a167acb
minor nit fixes
rachelyangdog 3b26275
Merge branch 'main' into rachel.yang/OTLP-trace-export
rachelyangdog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! OTLP trace export configuration. | ||
|
|
||
| use http::HeaderMap; | ||
| use std::time::Duration; | ||
|
|
||
| /// OTLP trace export protocol. HTTP/JSON is currently supported. | ||
| #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] | ||
| pub(crate) enum OtlpProtocol { | ||
| /// HTTP with JSON body (Content-Type: application/json). Default for HTTP. | ||
| #[default] | ||
| HttpJson, | ||
| /// HTTP with protobuf body. (Not supported yet) | ||
| #[allow(dead_code)] | ||
| HttpProtobuf, | ||
| /// gRPC. (Not supported yet) | ||
| #[allow(dead_code)] | ||
| Grpc, | ||
| } | ||
|
|
||
| /// Default timeout for OTLP export requests. | ||
| pub const DEFAULT_OTLP_TIMEOUT: Duration = Duration::from_secs(10); | ||
|
|
||
| /// Parsed OTLP trace exporter configuration. | ||
| #[derive(Clone, Debug)] | ||
| pub struct OtlpTraceConfig { | ||
| /// Full URL to POST traces to (e.g. `http://localhost:4318/v1/traces`). | ||
| pub endpoint_url: String, | ||
| /// Pre-validated HTTP headers to include in each request. | ||
| pub headers: HeaderMap, | ||
| /// Request timeout. | ||
| pub timeout: Duration, | ||
| /// Protocol (for future use; currently only HttpJson is supported). | ||
| #[allow(dead_code)] | ||
| pub(crate) protocol: OtlpProtocol, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! OTLP HTTP/JSON trace exporter. | ||
|
|
||
| use super::config::OtlpTraceConfig; | ||
| use crate::trace_exporter::error::{InternalErrorKind, RequestError, TraceExporterError}; | ||
| use libdd_common::{http_common, Endpoint, HttpClient}; | ||
| use libdd_trace_utils::send_with_retry::{ | ||
| send_with_retry, RetryBackoffType, RetryStrategy, SendWithRetryError, | ||
| }; | ||
|
|
||
| /// Max total attempts for OTLP export (1 initial + up to 4 retries on transient failures). | ||
| const OTLP_MAX_ATTEMPTS: u32 = 5; | ||
| /// Initial backoff between retries (milliseconds). | ||
| const OTLP_RETRY_DELAY_MS: u64 = 100; | ||
|
|
||
| /// Send OTLP trace payload (JSON bytes) to the configured endpoint with retries. | ||
| /// | ||
| /// Uses [`send_with_retry`] for consistent retry behaviour and observability across exporters. | ||
| /// | ||
| /// `test_token` is forwarded as `X-Datadog-Test-Session-Token` when set, enabling snapshot tests | ||
| /// against the Datadog test agent's OTLP endpoint. | ||
| pub async fn send_otlp_traces_http( | ||
| client: &HttpClient, | ||
| config: &OtlpTraceConfig, | ||
| test_token: Option<&str>, | ||
| json_body: Vec<u8>, | ||
| ) -> Result<(), TraceExporterError> { | ||
|
rachelyangdog marked this conversation as resolved.
|
||
| let url = libdd_common::parse_uri(&config.endpoint_url).map_err(|e| { | ||
| TraceExporterError::Internal(InternalErrorKind::InvalidWorkerState(format!( | ||
| "Invalid OTLP endpoint URL: {}", | ||
| e | ||
| ))) | ||
| })?; | ||
|
|
||
| let target = Endpoint { | ||
| url, | ||
| timeout_ms: config.timeout.as_millis() as u64, | ||
| ..Endpoint::default() | ||
| }; | ||
|
|
||
| let mut headers = config.headers.clone(); | ||
| headers.insert( | ||
| http::header::CONTENT_TYPE, | ||
| libdd_common::header::APPLICATION_JSON, | ||
| ); | ||
| if let Some(token) = test_token { | ||
| if let Ok(val) = http::HeaderValue::from_str(token) { | ||
| headers.insert( | ||
| http::HeaderName::from_static("x-datadog-test-session-token"), | ||
| val, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| let retry_strategy = RetryStrategy::new( | ||
| OTLP_MAX_ATTEMPTS, | ||
| OTLP_RETRY_DELAY_MS, | ||
| RetryBackoffType::Exponential, | ||
| None, | ||
| ); | ||
|
|
||
| match send_with_retry(client, &target, json_body, &headers, &retry_strategy).await { | ||
| Ok(_) => Ok(()), | ||
| Err(e) => Err(map_send_error(e).await), | ||
| } | ||
| } | ||
|
|
||
| async fn map_send_error(err: SendWithRetryError) -> TraceExporterError { | ||
| match err { | ||
| SendWithRetryError::Http(response, _) => { | ||
| let status = response.status(); | ||
| let body_bytes = http_common::collect_response_bytes(response) | ||
| .await | ||
| .unwrap_or_default(); | ||
| let body_str = String::from_utf8_lossy(&body_bytes); | ||
| TraceExporterError::Request(RequestError::new(status, &body_str)) | ||
| } | ||
| SendWithRetryError::Timeout(_) => { | ||
| TraceExporterError::Io(std::io::Error::from(std::io::ErrorKind::TimedOut)) | ||
| } | ||
| SendWithRetryError::Network(error, _) => TraceExporterError::from(error), | ||
| SendWithRetryError::Build(_) => TraceExporterError::Internal( | ||
| InternalErrorKind::InvalidWorkerState("Failed to build OTLP request".to_string()), | ||
| ), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ | ||
|
rachelyangdog marked this conversation as resolved.
|
||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! OTLP trace export for libdatadog. | ||
| //! | ||
| //! When an OTLP endpoint is configured via | ||
| //! [`crate::trace_exporter::TraceExporterBuilder::set_otlp_endpoint`], the trace exporter sends | ||
| //! traces in OTLP HTTP/JSON format to that endpoint instead of the Datadog agent. The host language | ||
| //! is responsible for resolving the endpoint from its own configuration (e.g. | ||
| //! `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`). | ||
| //! | ||
| //! ## Sampling | ||
| //! | ||
| //! The exporter enforces the sampling decision already made by the tracer: unsampled chunks are | ||
| //! dropped via `drop_chunks` before export. It does not apply its own sampling policy. The tracer | ||
| //! (e.g. dd-trace-py) is responsible for inheriting the sampling decision from the distributed | ||
| //! trace context; when no decision is present, the tracer typically uses 100% (always on). | ||
| //! | ||
| //! ## Partial flush | ||
| //! | ||
| //! For the POC, partial flush is disabled. The tracer should only invoke the exporter when all | ||
| //! spans from a local trace are closed (i.e. send complete trace chunks). This crate does not | ||
| //! buffer or flush partially—it exports whatever trace chunks it receives. | ||
|
|
||
| pub mod config; | ||
| pub mod exporter; | ||
|
|
||
| pub use config::OtlpTraceConfig; | ||
| pub use exporter::send_otlp_traces_http; | ||
| pub use libdd_trace_utils::otlp_encoder::{map_traces_to_otlp, OtlpResourceInfo}; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.