oxtel_tower/lib.rs
1//! HTTP instrumentation for [tower](https://docs.rs/tower) services, and so
2//! for axum and hyper, following the OpenTelemetry semantic conventions.
3//! Part of [oxtel](https://github.com/agentstatelabs/oxtel).
4//!
5//! - [`HttpServerLayer`]: a span per request, continuing the caller's trace;
6//! `http.server.request.duration` and `http.server.active_requests`; an
7//! access log.
8//! - [`HttpClientLayer`]: a span per request, trace context sent along, and
9//! `http.client.request.duration`. [`ClientCall`] records the same for a
10//! client that isn't a tower service.
11//! - [`MetricsService`]: Prometheus `/metrics` as a tower service, for an
12//! axum router or a hyper server.
13//!
14//! ```no_run
15//! # #[cfg(feature = "axum")] {
16//! use axum::Router;
17//! use axum::routing::get;
18//! use oxtel_tower::{HttpServerLayer, MetricsService};
19//!
20//! # fn app(exporter: oxtel_prometheus::PrometheusExporter) -> Router {
21//! Router::new()
22//! .route("/items/{id}", get(|| async { "item" }))
23//! // `Router::layer` runs after routing, so `http.route` is the template.
24//! .layer(HttpServerLayer::new())
25//! .route_service("/metrics", MetricsService::new(exporter))
26//! # }
27//! # }
28//! ```
29//!
30//! Spans and events are recorded with `tracing` and metrics with `metrics`,
31//! so they go wherever the program's subscriber and recorder send them:
32//! with `oxtel::init()`, to OTLP or Prometheus.
33
34mod client;
35mod metrics_service;
36mod request;
37mod server;
38
39pub use client::{ClientCall, ClientRequestInfo, HttpClient, HttpClientLayer};
40pub use metrics_service::MetricsService;
41pub use request::{RequestInfo, SpanName};
42pub use server::{HttpServer, HttpServerLayer};
43
44/// The futures the services return.
45pub mod future {
46 pub use crate::client::ResponseFuture as ClientResponseFuture;
47 pub use crate::server::ResponseFuture as ServerResponseFuture;
48}
49
50#[doc(hidden)]
51pub mod __private {
52 pub use tracing;
53}
54
55/// Give the current `metrics` recorder the units and descriptions of the
56/// HTTP metrics, so the duration histograms are in seconds with the
57/// conventions' buckets. The layers do this when they're made, so make them
58/// after installing the recorder (as `oxtel::init()` does), or call this
59/// once the recorder is in place.
60pub fn describe_metrics() {
61 metrics::describe_histogram!(
62 "http.server.request.duration",
63 metrics::Unit::Seconds,
64 "Duration of HTTP server requests."
65 );
66 metrics::describe_histogram!(
67 "http.client.request.duration",
68 metrics::Unit::Seconds,
69 "Duration of HTTP client requests."
70 );
71 metrics::describe_gauge!(
72 "http.server.active_requests",
73 "Number of active HTTP server requests."
74 );
75}
76
77/// The span [`HttpServerLayer`] makes for a request, from its
78/// [`RequestInfo`], with every standard field declared. Add fields of your
79/// own after the info, for middleware to record later:
80///
81/// ```
82/// # let info = oxtel_tower::RequestInfo {
83/// # method: "GET", method_original: None, path: "/", scheme: "http", route: None,
84/// # server_address: None, server_port: None, protocol_version: "1.1", user_agent: None,
85/// # };
86/// let span = oxtel_tower::server_span!(&info, app.user.id = tracing::field::Empty);
87/// ```
88#[macro_export]
89macro_rules! server_span {
90 ($info:expr $(,)?) => {
91 $crate::server_span!($info, otel.status_description = $crate::__private::tracing::field::Empty)
92 };
93 ($info:expr, $($field:tt)+) => {{
94 let info: &$crate::RequestInfo<'_> = $info;
95 $crate::__private::tracing::info_span!(
96 target: "oxtel_tower",
97 "HTTP server request",
98 otel.name = %info.span_name(),
99 otel.kind = "server",
100 http.request.method = info.method,
101 http.request.method_original = info.method_original,
102 url.path = info.path,
103 url.scheme = info.scheme,
104 http.route = info.route,
105 server.address = info.server_address,
106 server.port = info.server_port,
107 network.protocol.version = info.protocol_version,
108 user_agent.original = info.user_agent,
109 http.response.status_code = $crate::__private::tracing::field::Empty,
110 error.type = $crate::__private::tracing::field::Empty,
111 otel.status_code = $crate::__private::tracing::field::Empty,
112 $($field)+
113 )
114 }};
115}