Skip to content
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
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "init4-bin-base"
description = "Internal utilities for binaries produced by the init4 team"
keywords = ["init4", "bin", "base"]

version = "0.15.0"
version = "0.15.1"
edition = "2021"
rust-version = "1.85"
authors = ["init4", "James Prestwich", "evalir"]
Expand All @@ -30,6 +30,7 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json", "re
# OTLP
opentelemetry_sdk = "0.30.0"
opentelemetry = "0.30.0"
opentelemetry-http = "0.30.0"
opentelemetry-otlp = "0.30.0"
opentelemetry-semantic-conventions = { version = "0.30.0", features = ["semconv_experimental"] }
tracing-opentelemetry = "0.31.0"
Expand All @@ -47,12 +48,12 @@ oauth2 = { version = "5.0.0", optional = true }
tokio = { version = "1.36.0", optional = true }

# Other
thiserror = "2.0.11"
axum = "0.8.1"
serde = { version = "1", features = ["derive"] }
thiserror = "2.0.11"
tower = "0.5.2"
async-trait = { version = "0.1.80", optional = true }
eyre = { version = "0.6.12", optional = true }
axum = { version = "0.8.1", optional = true }
tower = { version = "0.5.2", optional = true }

# AWS
aws-config = { version = "1.1.7", optional = true }
Expand All @@ -72,7 +73,7 @@ tokio = { version = "1.43.0", features = ["macros"] }
default = ["alloy", "rustls"]
alloy = ["dep:alloy"]
aws = ["alloy", "alloy?/signer-aws", "dep:async-trait", "dep:aws-config", "dep:aws-sdk-kms"]
perms = ["dep:oauth2", "dep:tokio", "dep:reqwest", "dep:signet-tx-cache", "dep:eyre", "dep:axum", "dep:tower"]
perms = ["dep:oauth2", "dep:tokio", "dep:reqwest", "dep:signet-tx-cache", "dep:eyre"]
rustls = ["dep:rustls", "rustls/aws-lc-rs"]

[[example]]
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub mod utils {
/// Prometheus metrics utilities.
pub mod metrics;

/// Axum OpenTelemetry utilities.
pub mod otel_axum;

/// OpenTelemetry utilities.
pub mod otlp;

Expand Down
69 changes: 69 additions & 0 deletions src/utils/otel_axum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use axum::extract::{MatchedPath, Request};
use tower::{Layer, Service};
use tracing::{info_span, instrument::Instrumented, Instrument};
use tracing_opentelemetry::OpenTelemetrySpanExt;

/// A [`Layer`] that adds OpenTelemetry spans to Axum requests.
#[derive(Debug, Clone, Copy)]
pub struct OtelAxumSpanLayer;

/// A simple service
#[derive(Debug, Clone)]
pub struct OtelAxumSpanner<S> {
inner: S,
}

impl<S> Layer<S> for OtelAxumSpanLayer {
type Service = OtelAxumSpanner<S>;

fn layer(&self, inner: S) -> Self::Service {
OtelAxumSpanner { inner }
}
}

impl<S, Body> Service<Request<Body>> for OtelAxumSpanner<S>
where
S: Service<Request<Body>>,
{
type Response = S::Response;
type Error = S::Error;
type Future = Instrumented<S::Future>;

fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, req: Request<Body>) -> Self::Future {
let parent_context = opentelemetry::global::get_text_map_propagator(|propagator| {
propagator.extract(&opentelemetry_http::HeaderExtractor(req.headers()))
});

let method = req.method().to_string();
let uri = req.uri().clone();
let route = req
.extensions()
.get::<MatchedPath>()
.map(|r| r.as_str())
.unwrap_or_else(|| uri.path());
let name = format!("{method} {route}");
let name = name.trim();

let span = info_span!(
"Http Request",
"otel.name" = name,
"otel.target" = name,
"otel.kind" = "server",
"http.request.method" = method,
"url.path" = uri.path(),
"url.scheme" = uri.scheme_str().unwrap_or(""),
"http.route" = route,
"http.response.status_code" = tracing::field::Empty,
);
span.set_parent(parent_context);

self.inner.call(req).instrument(span)
}
}