diff --git a/tracing-opentelemetry/Cargo.toml b/tracing-opentelemetry/Cargo.toml index 1781eb99f3..54895eba83 100644 --- a/tracing-opentelemetry/Cargo.toml +++ b/tracing-opentelemetry/Cargo.toml @@ -24,10 +24,10 @@ default = ["tracing-log"] [dependencies] opentelemetry = { version = "0.17", default-features = false, features = ["trace"] } -tracing = { path = "../tracing", version = "0.1", default-features = false, features = ["std"] } -tracing-core = { path = "../tracing-core", version = "0.1" } -tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", default-features = false, features = ["registry", "std"] } -tracing-log = { path = "../tracing-log", version = "0.1", default-features = false, optional = true } +tracing = { version = "0.1.36", default-features = false, features = ["std"] } +tracing-core = { version = "0.1.28" } +tracing-subscriber = { version = "0.3.14", default-features = false, features = ["registry", "std"] } +tracing-log = { version = "0.1.3", default-features = false, optional = true } once_cell = "1" [dev-dependencies] diff --git a/tracing-opentelemetry/src/layer.rs b/tracing-opentelemetry/src/layer.rs index 533fe19406..ec5ecc7339 100644 --- a/tracing-opentelemetry/src/layer.rs +++ b/tracing-opentelemetry/src/layer.rs @@ -916,10 +916,12 @@ where } } - // Assign end time, build and start span, drop span to export - builder - .with_end_time(SystemTime::now()) - .start_with_context(&self.tracer, &parent_cx); + if builder.end_time.is_none() { + // Assign end time + builder = builder.with_end_time(SystemTime::now()); + } + // Build and start span, drop span to export + builder.start_with_context(&self.tracer, &parent_cx); } } diff --git a/tracing-opentelemetry/src/span_ext.rs b/tracing-opentelemetry/src/span_ext.rs index ade736815a..2784a774b9 100644 --- a/tracing-opentelemetry/src/span_ext.rs +++ b/tracing-opentelemetry/src/span_ext.rs @@ -114,6 +114,9 @@ pub trait OpenTelemetrySpanExt { /// make_request(Span::current().context()) /// ``` fn context(&self) -> Context; + + fn set_start_time(&self, start_time: std::time::SystemTime); + fn set_end_time(&self, end_time: std::time::SystemTime); } impl OpenTelemetrySpanExt for tracing::Span { @@ -167,4 +170,24 @@ impl OpenTelemetrySpanExt for tracing::Span { cx.unwrap_or_default() } + + fn set_start_time(&self, start_time: std::time::SystemTime) { + self.with_subscriber(|(id, subscriber)| { + if let Some(get_context) = subscriber.downcast_ref::() { + get_context.with_context(subscriber, id, |otel_data, _tracer| { + otel_data.builder.start_time = Some(start_time); + }) + } + }); + } + + fn set_end_time(&self, end_time: std::time::SystemTime) { + self.with_subscriber(|(id, subscriber)| { + if let Some(get_context) = subscriber.downcast_ref::() { + get_context.with_context(subscriber, id, |otel_data, _tracer| { + otel_data.builder.end_time = Some(end_time); + }) + } + }); + } }