From 5c4463d5ab8362e04e9a77a6b6e24d0513df800d Mon Sep 17 00:00:00 2001 From: Fergus Dixon Date: Tue, 24 Oct 2023 16:13:08 +0100 Subject: [PATCH 1/6] Update to opentelem dependencies, and trace ID resolving --- .gitignore | 3 ++- Cargo.toml | 12 ++++++------ src/axum.rs | 4 +--- src/formatter.rs | 12 ++++++++---- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 944bbd1..dd45c5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target/ Cargo.lock -.idea/ \ No newline at end of file +.idea/ +.DS_Store diff --git a/Cargo.toml b/Cargo.toml index 19eb720..8e94f94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ddtrace" -version = "0.1.1" +version = "0.1.2" authors = ["David Steiner "] edition = "2021" license = "MIT" @@ -17,15 +17,15 @@ axum = ["dep:axum", "dep:tokio", "dep:axum-tracing-opentelemetry"] [dependencies] axum = { version = "^0.6.10", optional = true } -axum-tracing-opentelemetry = { version = "^0.11.0", optional = true } +axum-tracing-opentelemetry = { version = "^0.14.1", optional = true } chrono = "^0.4.24" -opentelemetry = { version = "^0.19.0", features = ["rt-tokio"] } -opentelemetry-datadog = "^0.7.0" -opentelemetry-otlp = { version = "^0.12.0" } +opentelemetry = { version = "^0.20.0", features = ["rt-tokio"] } +opentelemetry-datadog = "^0.8.0" +opentelemetry-otlp = { version = "^0.13.0" } serde = { version = "^1.0.156", features = ["derive"] } serde_json = "^1.0.95" tokio = { version = "^1.26.0", features = ["signal"], optional = true } tracing = "^0.1.37" -tracing-opentelemetry = "^0.19.0" +tracing-opentelemetry = "^0.20.0" tracing-serde = "^0.1.3" tracing-subscriber = { version = "^0.3.16", features = ["env-filter", "json"] } diff --git a/src/axum.rs b/src/axum.rs index bee36b6..c3c59c9 100644 --- a/src/axum.rs +++ b/src/axum.rs @@ -5,9 +5,7 @@ //! //! [`axum-tracing-opentelemetry`]: https://github.com/davidB/axum-tracing-opentelemetry -pub use axum_tracing_opentelemetry::opentelemetry_tracing_layer; -pub use axum_tracing_opentelemetry::opentelemetry_tracing_layer_grpc; - +pub use axum_tracing_opentelemetry::middleware::OtelAxumLayer; pub async fn shutdown_signal() { tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) .expect("failed to install signal handler") diff --git a/src/formatter.rs b/src/formatter.rs index 8fb47be..2efda8b 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -8,7 +8,7 @@ use std::io; use chrono::Utc; -use opentelemetry::trace::{SpanId, TraceId}; +use opentelemetry::trace::{SpanId, TraceContextExt, TraceId}; use serde::ser::{SerializeMap, Serializer as _}; use serde::Serialize; use tracing::{Event, Subscriber}; @@ -44,9 +44,13 @@ fn lookup_trace_info(span_ref: &SpanRef) -> Option where S: Subscriber + for<'a> LookupSpan<'a>, { - span_ref.extensions().get::().map(|o| TraceInfo { - trace_id: o.builder.trace_id.unwrap_or(TraceId::INVALID).into(), - span_id: o.builder.span_id.unwrap_or(SpanId::INVALID).into(), + span_ref.extensions().get::().map(|o| { + let parent_id = o.parent_cx.span().span_context().trace_id(); + let trace_id = o.builder.trace_id.unwrap_or(parent_id); + TraceInfo { + trace_id: trace_id.into(), + span_id: o.builder.span_id.unwrap_or(SpanId::INVALID).into(), + } }) } From 0edb57167b7c80edbdb6d9cbf096c1660fe00d4e Mon Sep 17 00:00:00 2001 From: Fergus Dixon Date: Tue, 24 Oct 2023 19:16:37 +0100 Subject: [PATCH 2/6] lock to fix propogation --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8e94f94..117a926 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,6 @@ serde = { version = "^1.0.156", features = ["derive"] } serde_json = "^1.0.95" tokio = { version = "^1.26.0", features = ["signal"], optional = true } tracing = "^0.1.37" -tracing-opentelemetry = "^0.20.0" +tracing-opentelemetry = "^0.21.0" tracing-serde = "^0.1.3" tracing-subscriber = { version = "^0.3.16", features = ["env-filter", "json"] } From 72be0dc773b16f9c1718b311b988951450d22e05 Mon Sep 17 00:00:00 2001 From: Fergus Dixon Date: Tue, 24 Oct 2023 19:29:54 +0100 Subject: [PATCH 3/6] rewrite preference of trace ID --- src/formatter.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 2efda8b..b08b8ab 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -45,8 +45,14 @@ where S: Subscriber + for<'a> LookupSpan<'a>, { span_ref.extensions().get::().map(|o| { - let parent_id = o.parent_cx.span().span_context().trace_id(); - let trace_id = o.builder.trace_id.unwrap_or(parent_id); + // If the parent ID is empty/0 - then fall back to the builder's ID + let parent_span_cx = o.parent_cx.span(); + + let trace_id = if parent_span_cx.span_context().trace_id() != TraceId::INVALID { + parent_span_cx.span_context().trace_id() + } else { + o.builder.trace_id.unwrap_or(TraceId::INVALID) + }; TraceInfo { trace_id: trace_id.into(), span_id: o.builder.span_id.unwrap_or(SpanId::INVALID).into(), From 229de5a3ed7d130504db0df94c6476facfd03c55 Mon Sep 17 00:00:00 2001 From: Fergus Dixon Date: Tue, 24 Oct 2023 19:47:37 +0100 Subject: [PATCH 4/6] revert trace ID resolution logic --- src/formatter.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index b08b8ab..8fb47be 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -8,7 +8,7 @@ use std::io; use chrono::Utc; -use opentelemetry::trace::{SpanId, TraceContextExt, TraceId}; +use opentelemetry::trace::{SpanId, TraceId}; use serde::ser::{SerializeMap, Serializer as _}; use serde::Serialize; use tracing::{Event, Subscriber}; @@ -44,19 +44,9 @@ fn lookup_trace_info(span_ref: &SpanRef) -> Option where S: Subscriber + for<'a> LookupSpan<'a>, { - span_ref.extensions().get::().map(|o| { - // If the parent ID is empty/0 - then fall back to the builder's ID - let parent_span_cx = o.parent_cx.span(); - - let trace_id = if parent_span_cx.span_context().trace_id() != TraceId::INVALID { - parent_span_cx.span_context().trace_id() - } else { - o.builder.trace_id.unwrap_or(TraceId::INVALID) - }; - TraceInfo { - trace_id: trace_id.into(), - span_id: o.builder.span_id.unwrap_or(SpanId::INVALID).into(), - } + span_ref.extensions().get::().map(|o| TraceInfo { + trace_id: o.builder.trace_id.unwrap_or(TraceId::INVALID).into(), + span_id: o.builder.span_id.unwrap_or(SpanId::INVALID).into(), }) } From 6c109a1231e2d5f19fa7dd9585192c6161f4bdb3 Mon Sep 17 00:00:00 2001 From: Fergus Dixon Date: Wed, 17 Jul 2024 16:12:46 +0100 Subject: [PATCH 5/6] another bash at this, and revert changes made in 0.1.1 --- Cargo.toml | 11 ++++++----- src/formatter.rs | 3 ++- src/tracer.rs | 7 +++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 117a926..e22826b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,15 +17,16 @@ axum = ["dep:axum", "dep:tokio", "dep:axum-tracing-opentelemetry"] [dependencies] axum = { version = "^0.6.10", optional = true } -axum-tracing-opentelemetry = { version = "^0.14.1", optional = true } +axum-tracing-opentelemetry = { version = "0.18.0", optional = true } chrono = "^0.4.24" -opentelemetry = { version = "^0.20.0", features = ["rt-tokio"] } -opentelemetry-datadog = "^0.8.0" -opentelemetry-otlp = { version = "^0.13.0" } +opentelemetry = { version = "0.22.0" } +opentelemetry-datadog = "0.10.0" +opentelemetry-otlp = { version = "0.15.0" , features = ["tls-roots"]} +opentelemetry_sdk = { version = "0.22.0", features = ["rt-tokio"] } serde = { version = "^1.0.156", features = ["derive"] } serde_json = "^1.0.95" tokio = { version = "^1.26.0", features = ["signal"], optional = true } tracing = "^0.1.37" -tracing-opentelemetry = "^0.21.0" +tracing-opentelemetry = "0.23.0" tracing-serde = "^0.1.3" tracing-subscriber = { version = "^0.3.16", features = ["env-filter", "json"] } diff --git a/src/formatter.rs b/src/formatter.rs index 8fb47be..8d73a2b 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -8,6 +8,7 @@ use std::io; use chrono::Utc; +use opentelemetry::trace::TraceContextExt; use opentelemetry::trace::{SpanId, TraceId}; use serde::ser::{SerializeMap, Serializer as _}; use serde::Serialize; @@ -45,7 +46,7 @@ where S: Subscriber + for<'a> LookupSpan<'a>, { span_ref.extensions().get::().map(|o| TraceInfo { - trace_id: o.builder.trace_id.unwrap_or(TraceId::INVALID).into(), + trace_id: o.parent_cx.span().span_context().trace_id().into(), span_id: o.builder.span_id.unwrap_or(SpanId::INVALID).into(), }) } diff --git a/src/tracer.rs b/src/tracer.rs index 17d89b8..a2d208d 100644 --- a/src/tracer.rs +++ b/src/tracer.rs @@ -4,11 +4,11 @@ //! to send traces to the Datadog agent in batches over gRPC. //! //! It also contains a convenience function to build a layer with the tracer. -use opentelemetry::sdk::trace::{RandomIdGenerator, Sampler, Tracer}; -use opentelemetry::sdk::{trace, Resource}; pub use opentelemetry::trace::{TraceError, TraceResult}; use opentelemetry::KeyValue; use opentelemetry_otlp::WithExportConfig; +use opentelemetry_sdk::trace::{RandomIdGenerator, Sampler, Tracer}; +use opentelemetry_sdk::{trace, Resource}; use std::time::Duration; use tracing::Subscriber; use tracing_opentelemetry::{OpenTelemetryLayer, PreSampledTracer}; @@ -18,7 +18,6 @@ pub fn build_tracer(service_name: &str) -> TraceResult { let exporter = opentelemetry_otlp::new_exporter() .tonic() .with_timeout(Duration::from_secs(3)); - opentelemetry_otlp::new_pipeline() .tracing() .with_trace_config( @@ -31,7 +30,7 @@ pub fn build_tracer(service_name: &str) -> TraceResult { .with_id_generator(RandomIdGenerator::default()), ) .with_exporter(exporter) - .install_batch(opentelemetry::runtime::Tokio) + .install_batch(opentelemetry_sdk::runtime::Tokio) } pub fn build_layer(service_name: &str) -> TraceResult> From c10adabf50d9a66b9df5b3d716879f5eb03c15fe Mon Sep 17 00:00:00 2001 From: Fergus Dixon Date: Mon, 22 Jul 2024 13:30:43 +0100 Subject: [PATCH 6/6] update more deps --- Cargo.toml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e22826b..258c1a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,17 +16,17 @@ exclude = [".pre-commit-config.yaml"] axum = ["dep:axum", "dep:tokio", "dep:axum-tracing-opentelemetry"] [dependencies] -axum = { version = "^0.6.10", optional = true } -axum-tracing-opentelemetry = { version = "0.18.0", optional = true } +axum = { version = "^0.7.5", optional = true } +axum-tracing-opentelemetry = { version = "0.19.0", optional = true } chrono = "^0.4.24" -opentelemetry = { version = "0.22.0" } -opentelemetry-datadog = "0.10.0" -opentelemetry-otlp = { version = "0.15.0" , features = ["tls-roots"]} -opentelemetry_sdk = { version = "0.22.0", features = ["rt-tokio"] } +opentelemetry = { version = "0.23.0" } +opentelemetry-datadog = "0.11.0" +opentelemetry-otlp = { version = "0.16.0" , features = ["tls-roots"]} +opentelemetry_sdk = { version = "0.23.0", features = ["rt-tokio"] } serde = { version = "^1.0.156", features = ["derive"] } serde_json = "^1.0.95" tokio = { version = "^1.26.0", features = ["signal"], optional = true } tracing = "^0.1.37" -tracing-opentelemetry = "0.23.0" +tracing-opentelemetry = "0.24.0" tracing-serde = "^0.1.3" tracing-subscriber = { version = "^0.3.16", features = ["env-filter", "json"] }