From 800b9e3e375ee74fe432da5f932f0d49a63c188a Mon Sep 17 00:00:00 2001 From: Zhongyang Wu Date: Thu, 1 Jun 2023 09:59:09 -0700 Subject: [PATCH] fix: allow span id to be less than 16 characters in jaeger propagator (#1084) --- opentelemetry-jaeger/CHANGELOG.md | 4 ++++ opentelemetry-jaeger/src/lib.rs | 27 +++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/opentelemetry-jaeger/CHANGELOG.md b/opentelemetry-jaeger/CHANGELOG.md index 6628532400..6b75c144ec 100644 --- a/opentelemetry-jaeger/CHANGELOG.md +++ b/opentelemetry-jaeger/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog +## Main +### Fixed +- allow span id to be less than 16 characters in propagator [#1084](https://github.com/open-telemetry/opentelemetry-rust/pull/1084) + ## v0.18.0 ### Added diff --git a/opentelemetry-jaeger/src/lib.rs b/opentelemetry-jaeger/src/lib.rs index e568982e0b..cf912caf15 100644 --- a/opentelemetry-jaeger/src/lib.rs +++ b/opentelemetry-jaeger/src/lib.rs @@ -484,11 +484,17 @@ mod propagator { /// Extract span id from the header. fn extract_span_id(&self, span_id: &str) -> Result { - if span_id.len() != 16 { - return Err(()); + match span_id.len() { + // exact 16 + 16 => SpanId::from_hex(span_id).map_err(|_| ()), + // more than 16 is invalid + 17.. => Err(()), + // less than 16 will result padding on left + _ => { + let padded = format!("{span_id:0>16}"); + SpanId::from_hex(&padded).map_err(|_| ()) + } } - - SpanId::from_hex(span_id).map_err(|_| ()) } /// Extract flag from the header @@ -589,6 +595,7 @@ mod propagator { const SHORT_TRACE_ID_STR: &str = "4d0000000000000016"; const TRACE_ID: u128 = 0x0000_0000_0000_004d_0000_0000_0000_0016; const SPAN_ID_STR: &str = "0000000000017c29"; + const SHORT_SPAN_ID_STR: &str = "17c29"; const SPAN_ID: u64 = 0x0000_0000_0001_7c29; fn get_extract_data() -> Vec<(&'static str, &'static str, u8, SpanContext)> { @@ -617,6 +624,18 @@ mod propagator { TraceState::default(), ), ), + ( + SHORT_TRACE_ID_STR, + SHORT_SPAN_ID_STR, + 1, + SpanContext::new( + TraceId::from_u128(TRACE_ID), + SpanId::from_u64(SPAN_ID), + TraceFlags::SAMPLED, + true, + TraceState::default(), + ), + ), ( LONG_TRACE_ID_STR, SPAN_ID_STR,