Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetry: remove tonic dependency #414

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 15 additions & 1 deletion examples/grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -30,7 +44,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>
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?;
Expand Down
24 changes: 23 additions & 1 deletion examples/grpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use opentelemetry::global;
use opentelemetry::sdk::propagation::TraceContextPropagator;
use opentelemetry::trace::TraceError;
use opentelemetry::{
propagation::Extractor,
trace::{Span, Tracer},
KeyValue,
};
Expand All @@ -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::<Vec<_>>()
}
}

#[derive(Debug, Default)]
pub struct MyGreeter {}

Expand All @@ -24,7 +45,8 @@ impl Greeter for MyGreeter {
&self,
request: Request<HelloRequest>, // Accept request of type HelloRequest
) -> Result<Response<HelloReply>, 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)));

Expand Down
1 change: 0 additions & 1 deletion opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
32 changes: 0 additions & 32 deletions opentelemetry/src/propagation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down