Skip to content

Commit

Permalink
fixup! rust: use W3C context from MQTT message
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Buffon <[email protected]>
  • Loading branch information
nbuffon committed Oct 24, 2024
1 parent 9380cae commit 262ac32
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions rust/src/transport/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@ pub(crate) fn get_mqtt_span(span_kind: SpanKind, topic: &str, payload_size: i64)
.start(&tracer)
}

pub(crate) fn get_reception_mqtt_span(publish: Publish) -> BoxedSpan {
let tracer = global::tracer("iot3.core");

let topic = from_utf8(&publish.topic.to_vec())
.unwrap_or_default()
.to_string();
let size = publish.payload.len();

let propagator = TraceContextPropagator::new();
let trace_cx = propagator.extract(&ExtractWrapper(publish));
let span_cx = trace_cx.span().span_context().clone();

tracer
.span_builder("IoT3 Core MQTT Message")
.with_kind(SpanKind::Consumer)
.with_attributes(vec![
KeyValue::new("iot3.core.mqtt.topic", topic),
KeyValue::new("iot3.core.mqtt.payload_size", size as i64),
KeyValue::new("iot3.core.sdk_language", "rust"),
])
.with_links(vec![Link::with_context(span_cx)])
.start(&tracer)
}

struct ExtractWrapper(Publish);
impl Extractor for ExtractWrapper {
fn get(&self, key: &str) -> Option<&str> {
Expand All @@ -212,27 +236,3 @@ impl Extractor for ExtractWrapper {
}
}
}

pub(crate) fn get_reception_mqtt_span(publish: Publish) -> BoxedSpan {
let tracer = global::tracer("iot3.core");

let topic = from_utf8(&publish.topic.to_vec())
.unwrap_or_default()
.to_string();
let size = publish.payload.len();

let propagator = TraceContextPropagator::new();
let trace_cx = propagator.extract(&ExtractWrapper(publish));
let span_cx = trace_cx.span().span_context().clone();

tracer
.span_builder("IoT3 Core MQTT Message")
.with_kind(SpanKind::Consumer)
.with_attributes(vec![
KeyValue::new("iot3.core.mqtt.topic", topic),
KeyValue::new("iot3.core.mqtt.payload_size", size as i64),
KeyValue::new("iot3.core.sdk_language", "rust"),
])
.with_links(vec![Link::with_context(span_cx)])
.start(&tracer)
}

0 comments on commit 262ac32

Please sign in to comment.