Skip to content

Commit

Permalink
fix: propagate tracing information from spog to internal services
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulf Lilleengen authored and ctron committed Oct 24, 2023
1 parent 903ecbf commit 10810ad
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions infrastructure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ tracing-opentelemetry = "0.19.0"
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "tracing-log"] }
tracing-actix-web = "0.7.8"
url = "2.4.0"
reqwest = "*"
http = "0.2.9"

trustification-auth = { path = "../auth", features = ["actix"] }
2 changes: 1 addition & 1 deletion infrastructure/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod infra;
mod tracing;

pub mod app;
pub mod endpoint;
pub mod health;
pub mod tracing;

pub use infra::*;

Expand Down
54 changes: 53 additions & 1 deletion infrastructure/src/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::fmt;

use opentelemetry::propagation::Injector;
use opentelemetry::Context;
use reqwest::RequestBuilder;
use tracing_bunyan_formatter::BunyanFormattingLayer;

#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq)]
Expand All @@ -25,6 +27,56 @@ impl fmt::Display for Tracing {
}
}

pub trait PropagateCurrentContext {
fn propagate_current_context(self) -> Self
where
Self: Sized;
}

impl PropagateCurrentContext for reqwest::RequestBuilder {
#[inline]
fn propagate_current_context(self) -> Self
where
Self: Sized,
{
self.propagate_context(&opentelemetry::Context::current())
}
}

pub trait WithTracing {
fn propagate_context(self, cx: &Context) -> Self;
}

impl WithTracing for RequestBuilder {
fn propagate_context(self, cx: &Context) -> Self {
let headers = opentelemetry::global::get_text_map_propagator(|prop| {
let mut injector = HeaderInjector::new();
prop.inject_context(cx, &mut injector);
injector.0
});
self.headers(headers)
}
}

struct HeaderInjector(http::HeaderMap);

impl HeaderInjector {
pub fn new() -> Self {
Self(Default::default())
}
}

impl Injector for HeaderInjector {
/// Set a key and value in the 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);
}
}
}
}

/// Try getting the sampling rate from the environment variables
fn sampling_from_env() -> Option<f64> {
std::env::var_os("OTEL_TRACES_SAMPLER_ARG").and_then(|s| s.to_str().map(|s| s.parse::<f64>().ok()).unwrap())
Expand Down
6 changes: 6 additions & 0 deletions spog/api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use trustification_auth::{
client::{TokenInjector, TokenProvider},
swagger_ui::SwaggerUiOidc,
};
use trustification_infrastructure::tracing::PropagateCurrentContext;
use trustification_infrastructure::{app::http::HttpServerBuilder, MainContext};
use trustification_version::version;
use utoipa::OpenApi;
Expand Down Expand Up @@ -201,6 +202,7 @@ impl AppState {
.client
.get(url)
.query(&[("id", id)])
.propagate_current_context()
.inject_token(provider)
.await?
.send()
Expand All @@ -227,6 +229,7 @@ impl AppState {
.query(&[("q", q)])
.query(&[("offset", offset), ("limit", limit)])
.apply(&options)
.propagate_current_context()
.inject_token(provider)
.await?
.send()
Expand All @@ -248,6 +251,7 @@ impl AppState {
.client
.get(url)
.query(&[("advisory", id)])
.propagate_current_context()
.inject_token(provider)
.await?
.send()
Expand All @@ -274,6 +278,7 @@ impl AppState {
.query(&[("q", q)])
.query(&[("offset", offset), ("limit", limit)])
.apply(&options)
.propagate_current_context()
.inject_token(provider)
.await?
.send()
Expand All @@ -295,6 +300,7 @@ impl AppState {
let response = self
.client
.post(url)
.propagate_current_context()
.inject_token(provider)
.await?
.json(&AnalyzeRequest { purls })
Expand Down

0 comments on commit 10810ad

Please sign in to comment.