Skip to content

Commit

Permalink
Removed opentelemetry_http dependency and http crate bumped to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiapenati committed Jan 11, 2024
1 parent 637e7e6 commit 93ab7ef
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ license.workspace = true
autoexamples = false

[dependencies]
http = "0.2.11"
http = "1"
opentelemetry.workspace = true
opentelemetry-http = "0.10.0"
pin-project = "1.1.3"
tower-layer = "0.3.2"
tower-service = "0.3.2"
Expand Down
20 changes: 20 additions & 0 deletions src/trace/extractor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Implementation of fields injector.

use http::HeaderMap;

pub struct HeaderExtractor<'a>(pub &'a HeaderMap);

impl<'a> opentelemetry::propagation::Extractor for HeaderExtractor<'a> {
fn get(&self, key: &str) -> Option<&str> {
self.0
.get(key)
.and_then(|header_value| header_value.to_str().ok())
}

fn keys(&self) -> Vec<&str> {
self.0
.keys()
.map(|header_name| header_name.as_str())
.collect()
}
}
3 changes: 2 additions & 1 deletion src/trace/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use std::{
};

use http::{Request, Response};
use opentelemetry_http::{HeaderExtractor, HeaderInjector};
use pin_project::pin_project;
use tower_layer::Layer;
use tower_service::Service;
use tracing::{Level, Span};
use tracing_opentelemetry::OpenTelemetrySpanExt;

use super::{extractor::HeaderExtractor, injector::HeaderInjector};

/// Describes the relationship between the [`Span`] and the service producing the span.
#[derive(Clone, Copy, Debug)]
enum SpanKind {
Expand Down
3 changes: 2 additions & 1 deletion src/trace/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use std::{
};

use http::{Method, Request, Response, Version};
use opentelemetry_http::{HeaderExtractor, HeaderInjector};
use pin_project::pin_project;
use tower_layer::Layer;
use tower_service::Service;
use tracing::{Level, Span};
use tracing_opentelemetry::OpenTelemetrySpanExt;

use super::{extractor::HeaderExtractor, injector::HeaderInjector};

/// Describes the relationship between the [`Span`] and the service producing the span.
#[derive(Clone, Copy, Debug)]
enum SpanKind {
Expand Down
17 changes: 17 additions & 0 deletions src/trace/injector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Implementation of fields injector.

use std::str::FromStr;

use http::{HeaderMap, HeaderName, HeaderValue};

pub struct HeaderInjector<'a>(pub &'a mut HeaderMap);

impl<'a> opentelemetry::propagation::Injector for HeaderInjector<'a> {
fn set(&mut self, key: &str, value: String) {
if let Ok(header_name) = HeaderName::from_str(key) {
if let Ok(header_value) = HeaderValue::from_str(&value) {
self.0.insert(header_name, header_value);
}
}
}
}
2 changes: 2 additions & 0 deletions src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ pub use self::{
http::{Http, HttpLayer},
};

mod extractor;
pub mod grpc;
pub mod http;
mod injector;

0 comments on commit 93ab7ef

Please sign in to comment.