-
Notifications
You must be signed in to change notification settings - Fork 19
WIP: 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
Open
rachelyangdog
wants to merge
11
commits into
main
Choose a base branch
from
rachel.yang/OTLP-trace-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WIP: OTLP trace export #1641
Changes from all commits
Commits
Show all changes
11 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 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
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,35 @@ | ||
| // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! OTLP trace export configuration. | ||
|
|
||
| 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) | ||
| HttpProtobuf, | ||
| /// gRPC. (Not supported yet) | ||
| 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, | ||
| /// Optional HTTP headers (key-value pairs). | ||
| pub headers: Vec<(String, String)>, | ||
| /// 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,78 @@ | ||
| // 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, | ||
| }; | ||
| use std::collections::HashMap; | ||
|
|
||
| /// 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. | ||
| /// | ||
| /// Note: dynamic OTLP headers from `OTEL_EXPORTER_OTLP_HEADERS` are not forwarded because | ||
| /// [`send_with_retry`] requires `&'static str` header keys. Support for arbitrary OTEL headers | ||
| /// would require the API to accept `HashMap<String, String>`. | ||
|
Comment on lines
+23
to
+25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mmm true 🤔 |
||
| pub async fn send_otlp_traces_http( | ||
| client: &HttpClient, | ||
| config: &OtlpTraceConfig, | ||
| json_body: Vec<u8>, | ||
| ) -> Result<(), TraceExporterError> { | ||
rachelyangdog marked this conversation as resolved.
Show resolved
Hide 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 headers: HashMap<&'static str, String> = | ||
| HashMap::from([("Content-Type", "application/json".to_string())]); | ||
|
|
||
| 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,29 @@ | ||
| // Copyright 2024-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // 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 | ||
| //! | ||
| //! By default, the exporter does not apply its own sampling: it exports every trace it receives | ||
| //! from the tracer. 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should parse the configurations from the host language, and not in libdatadg.
The way we would configure the TraceExporter for
agent_urlfor instance would be