Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
feat: update traces
Browse files Browse the repository at this point in the history
  • Loading branch information
ralvescosta committed Feb 5, 2023
1 parent 9f64b89 commit 4cfed98
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 97 deletions.
2 changes: 1 addition & 1 deletion traces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ opentelemetry-otlp = { version = "0.11.0", features = ["tonic", "tls", "tls-root
tracing = { version = "0.1.37" }
serde = { version = "1.0.152", features = ["derive"] }
tonic = { version = "0.8.1", features = ["tls"] }
tokio = { version = "1.24.2", features = ["default"] }
tokio = { version = "1.25.0", features = ["default"] }
93 changes: 0 additions & 93 deletions traces/src/amqp.rs

This file was deleted.

53 changes: 53 additions & 0 deletions traces/src/grpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use opentelemetry::{
global::{self, BoxedSpan, BoxedTracer},
propagation::{Extractor, Injector},
trace::Tracer,
Context,
};

pub struct ExMetadataMap<'a>(&'a tonic::metadata::MetadataMap);

impl<'a> Extractor for ExMetadataMap<'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<_>>()
}
}

pub struct InjMetadataMap<'a>(&'a mut tonic::metadata::MetadataMap);

impl<'a> Injector for InjMetadataMap<'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::try_from(&value) {
self.0.insert(key, val);
}
}
}
}

pub fn span(meta: &tonic::metadata::MetadataMap, tracer: &BoxedTracer) -> (Context, BoxedSpan) {
let ctx = global::get_text_map_propagator(|prop| prop.extract(&ExMetadataMap(meta)));

let span = tracer.start_with_context("Processing reply", &ctx);

(ctx, span)
}

pub fn inject(ctx: &Context, meta: &mut tonic::metadata::MetadataMap) {
global::get_text_map_propagator(|propagator| {
propagator.inject_context(&ctx, &mut InjMetadataMap(meta))
});
}
4 changes: 1 addition & 3 deletions traces/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
///deprecated
pub mod amqp;

pub mod grpc;
pub mod jaeger;
pub mod otlp;

Expand Down

0 comments on commit 4cfed98

Please sign in to comment.