From ee9a01ef4037e8170a19fd149b453ae9bbfb8e91 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 25 Jul 2024 11:09:01 +0200 Subject: [PATCH] OTEL: remove custom Header{Injector,Extractor} implementations --- crates/cli/src/server.rs | 17 +---------------- crates/tower/src/trace_context.rs | 16 +--------------- 2 files changed, 2 insertions(+), 31 deletions(-) diff --git a/crates/cli/src/server.rs b/crates/cli/src/server.rs index 2d6666f2..8d2b0669 100644 --- a/crates/cli/src/server.rs +++ b/crates/cli/src/server.rs @@ -38,6 +38,7 @@ use mas_tower::{ KV, }; use opentelemetry::{Key, KeyValue}; +use opentelemetry_http::HeaderExtractor; use opentelemetry_semantic_conventions::trace::{ HTTP_REQUEST_METHOD, HTTP_RESPONSE_STATUS_CODE, HTTP_ROUTE, NETWORK_PROTOCOL_NAME, NETWORK_PROTOCOL_VERSION, URL_PATH, URL_QUERY, URL_SCHEME, USER_AGENT_ORIGINAL, @@ -53,22 +54,6 @@ use crate::app_state::AppState; const MAS_LISTENER_NAME: Key = Key::from_static_str("mas.listener.name"); -/// Same as the extractor from opentelemetry-http, but using http@1 -struct HeaderExtractor<'a>(pub &'a http::HeaderMap); - -impl<'a> opentelemetry::propagation::Extractor for HeaderExtractor<'a> { - fn get(&self, key: &str) -> Option<&str> { - self.0.get(key).and_then(|value| value.to_str().ok()) - } - - fn keys(&self) -> Vec<&str> { - self.0 - .keys() - .map(http::HeaderName::as_str) - .collect::>() - } -} - #[inline] fn otel_http_method(request: &Request) -> &'static str { match request.method() { diff --git a/crates/tower/src/trace_context.rs b/crates/tower/src/trace_context.rs index 3df9c1e4..c203e374 100644 --- a/crates/tower/src/trace_context.rs +++ b/crates/tower/src/trace_context.rs @@ -14,25 +14,11 @@ use http::Request; use opentelemetry::propagation::Injector; +use opentelemetry_http::HeaderInjector; use tower::{Layer, Service}; use tracing::Span; use tracing_opentelemetry::OpenTelemetrySpanExt; -/// Same as the one in opentelemetry-http, but updated for the http@1 upgrade -pub struct HeaderInjector<'a>(pub &'a mut http::HeaderMap); - -impl<'a> Injector for HeaderInjector<'a> { - /// Set a key and value in the [`http::HeaderMap`]. Does nothing if the key - /// or value are not valid inputs. - fn set(&mut self, key: &str, value: String) { - if let Ok(name) = http::header::HeaderName::from_bytes(key.as_bytes()) { - if let Ok(val) = http::header::HeaderValue::from_str(&value) { - self.0.insert(name, val); - } - } - } -} - /// A trait to get an [`Injector`] from a request. trait AsInjector { type Injector<'a>: Injector