diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a887ff9bf6..376b961b7d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: override: true - name: Run tests run: cargo --version && - cargo test --verbose --manifest-path=opentelemetry/Cargo.toml --features trace,metrics,serialize,tokio,serde,http,tonic,reqwest,testing && + cargo test --verbose --manifest-path=opentelemetry/Cargo.toml --features trace,metrics,serialize,tokio,serde,http,reqwest,testing && cargo test --manifest-path=opentelemetry-jaeger/Cargo.toml && cargo test --manifest-path=opentelemetry-zipkin/Cargo.toml meta: diff --git a/examples/grpc/Cargo.toml b/examples/grpc/Cargo.toml index 9cea6e874e..db87194869 100644 --- a/examples/grpc/Cargo.toml +++ b/examples/grpc/Cargo.toml @@ -16,7 +16,7 @@ http = "0.2" tonic = "0.3" prost = "0.6" tokio = { version = "0.2", features = ["full"] } -opentelemetry = { path = "../../opentelemetry", features = ["tonic"] } +opentelemetry = { path = "../../opentelemetry" } opentelemetry-jaeger = { path = "../../opentelemetry-jaeger" } [build-dependencies] diff --git a/examples/grpc/src/client.rs b/examples/grpc/src/client.rs index ab2e95f9ec..7238cbc141 100644 --- a/examples/grpc/src/client.rs +++ b/examples/grpc/src/client.rs @@ -4,10 +4,24 @@ use opentelemetry::global; use opentelemetry::sdk::propagation::TraceContextPropagator; use opentelemetry::trace::TraceError; use opentelemetry::{ + propagation::Injector, trace::{TraceContextExt, Tracer}, Context, KeyValue, }; +struct MetadataMap<'a>(&'a mut tonic::metadata::MetadataMap); + +impl<'a> Injector for MetadataMap<'a> { + /// Set a key and value in the MetadataMap. Does nothing if the key or value are not valid inputs + fn set(&mut self, key: &str, value: String) { + if let Ok(key) = tonic::metadata::MetadataKey::from_bytes(key.as_bytes()) { + if let Ok(val) = tonic::metadata::MetadataValue::from_str(&value) { + self.0.insert(key, val); + } + } + } +} + pub mod hello_world { tonic::include_proto!("helloworld"); } @@ -30,7 +44,7 @@ async fn main() -> Result<(), Box name: "Tonic".into(), }); global::get_text_map_propagator(|propagator| { - propagator.inject_context(&cx, request.metadata_mut()) + propagator.inject_context(&cx, &mut MetadataMap(request.metadata_mut())) }); let response = client.say_hello(request).await?; diff --git a/examples/grpc/src/server.rs b/examples/grpc/src/server.rs index 426916e333..c1222e1ba5 100644 --- a/examples/grpc/src/server.rs +++ b/examples/grpc/src/server.rs @@ -6,6 +6,7 @@ use opentelemetry::global; use opentelemetry::sdk::propagation::TraceContextPropagator; use opentelemetry::trace::TraceError; use opentelemetry::{ + propagation::Extractor, trace::{Span, Tracer}, KeyValue, }; @@ -15,6 +16,26 @@ pub mod hello_world { tonic::include_proto!("helloworld"); // The string specified here must match the proto package name. } +struct MetadataMap<'a>(&'a tonic::metadata::MetadataMap); + +impl<'a> Extractor for MetadataMap<'a> { + /// Get a value for a key from the MetadataMap. If the value can't be converted to &str, returns None + fn get(&self, key: &str) -> Option<&str> { + self.0.get(key).and_then(|metadata| metadata.to_str().ok()) + } + + /// Collect all the keys from the MetadataMap. + fn keys(&self) -> Vec<&str> { + self.0 + .keys() + .map(|key| match key { + tonic::metadata::KeyRef::Ascii(v) => v.as_str(), + tonic::metadata::KeyRef::Binary(v) => v.as_str(), + }) + .collect::>() + } +} + #[derive(Debug, Default)] pub struct MyGreeter {} @@ -24,7 +45,8 @@ impl Greeter for MyGreeter { &self, request: Request, // Accept request of type HelloRequest ) -> Result, Status> { - let parent_cx = global::get_text_map_propagator(|prop| prop.extract(request.metadata())); + let parent_cx = + global::get_text_map_propagator(|prop| prop.extract(&MetadataMap(request.metadata()))); let span = global::tracer("greeter").start_with_context("Processing reply", parent_cx); span.set_attribute(KeyValue::new("request", format!("{:?}", request))); diff --git a/opentelemetry/Cargo.toml b/opentelemetry/Cargo.toml index bd744fecfe..6169184546 100644 --- a/opentelemetry/Cargo.toml +++ b/opentelemetry/Cargo.toml @@ -34,7 +34,6 @@ serde = { version = "1.0", features = ["derive", "rc"], optional = true } http = { version = "0.2", optional = true } thiserror = "1" tokio = { version = "0.2", default-features = false, features = ["rt-core", "blocking", "time", "stream"], optional = true } -tonic = { version = "0.3", default-features = false, optional = true } reqwest = { version = "0.10", default-features = false, features = ["blocking"], optional = true } surf = { version = "2.0", default-features = false, optional = true } diff --git a/opentelemetry/src/propagation/mod.rs b/opentelemetry/src/propagation/mod.rs index 50a4df8fb3..209630ab85 100644 --- a/opentelemetry/src/propagation/mod.rs +++ b/opentelemetry/src/propagation/mod.rs @@ -228,38 +228,6 @@ impl Extractor for http::HeaderMap { } } -#[cfg(feature = "tonic")] -#[cfg_attr(docsrs, doc(cfg(feature = "tonic")))] -impl Injector for tonic::metadata::MetadataMap { - /// Set a key and value in the MetadataMap. Does nothing if the key or value are not valid inputs - fn set(&mut self, key: &str, value: String) { - if let Ok(key) = tonic::metadata::MetadataKey::from_bytes(key.as_bytes()) { - if let Ok(val) = tonic::metadata::MetadataValue::from_str(&value) { - self.insert(key, val); - } - } - } -} - -#[cfg(feature = "tonic")] -#[cfg_attr(docsrs, doc(cfg(feature = "tonic")))] -impl Extractor for tonic::metadata::MetadataMap { - /// Get a value for a key from the MetadataMap. If the value can't be converted to &str, returns None - fn get(&self, key: &str) -> Option<&str> { - self.get(key).and_then(|metadata| metadata.to_str().ok()) - } - - /// Collect all the keys from the MetadataMap. - fn keys(&self) -> Vec<&str> { - self.keys() - .map(|key| match key { - tonic::metadata::KeyRef::Ascii(v) => v.as_str(), - tonic::metadata::KeyRef::Binary(v) => v.as_str(), - }) - .collect::>() - } -} - #[cfg(test)] mod tests { use super::*;