From 26e88c8fe7aa494b1f6b0aa93991b32b8746fa11 Mon Sep 17 00:00:00 2001 From: Raphael Amorim Date: Sun, 14 Jan 2024 17:05:49 +0100 Subject: [PATCH] inject_context and inject_context_on_context --- src/http.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/http.rs b/src/http.rs index bde80c1..509d3fe 100644 --- a/src/http.rs +++ b/src/http.rs @@ -4,6 +4,7 @@ use opentelemetry::propagation::{Extractor, Injector}; use opentelemetry::Context; +use tracing_opentelemetry_instrumentation_sdk as otel; // copy from crate opentelemetry-http (to not be dependants of on 3rd: http, ...) pub struct HeaderInjector<'a>(pub &'a mut http::HeaderMap); @@ -36,13 +37,20 @@ impl<'a> Extractor for HeaderExtractor<'a> { } } -pub fn inject_context(context: &Context, headers: &mut http::HeaderMap) { +pub fn inject_context_on_context(context: &Context, headers: &mut http::HeaderMap) { let mut injector = HeaderInjector(headers); opentelemetry::global::get_text_map_propagator(|propagator| { propagator.inject_context(context, &mut injector); }); } +pub fn inject_context(headers: &mut http::HeaderMap) { + let mut injector = HeaderInjector(headers); + opentelemetry::global::get_text_map_propagator(|propagator| { + propagator.inject_context(&otel::find_current_context(), &mut injector); + }); +} + // If remote request has no span data the propagator defaults to an unsampled context #[must_use] pub fn extract_context(headers: &http::HeaderMap) -> Context {