1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use opentelemetry::{
    trace::{SpanBuilder, SpanKind, TraceContextExt, Tracer},
    Array, Context, KeyValue, Value,
};
use std::{
    borrow::Cow,
    fmt::{self, Debug, Formatter},
    net::SocketAddr,
    sync::Arc,
    time::{Instant, SystemTime},
};
use trillium::{async_trait, Conn, Handler, HeaderName, KnownHeaderName, Status};

type StringExtractionFn = dyn Fn(&Conn) -> Option<Cow<'static, str>> + Send + Sync + 'static;

/// Trillium handler that instruments per-request spans as per [semantic conventions for http][http-spans].
///
/// [http-spans]: https://opentelemetry.io/docs/specs/semconv/http/http-spans
#[derive(Clone)]
pub struct Trace<T> {
    pub(crate) route: Option<Arc<StringExtractionFn>>,
    pub(crate) error_type: Option<Arc<StringExtractionFn>>,
    pub(crate) headers: Vec<HeaderName<'static>>,
    pub(crate) enable_local_address_and_port: bool,
    tracer: T,
    socket_addr: Option<SocketAddr>,
}

impl<Span> Debug for Trace<Span> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("Trace")
            .field(
                "route",
                &match self.route {
                    Some(_) => "Some(..)",
                    _ => "None",
                },
            )
            .field(
                "error_type",
                &match self.error_type {
                    Some(_) => "Some(..)",
                    _ => "None",
                },
            )
            .field("tracer", &"..")
            .finish()
    }
}

/// Alias for [`Trace::new`]
pub fn trace<T: Tracer>(tracer: T) -> Trace<T> {
    Trace::new(tracer)
}

impl<T: Tracer> Trace<T> {
    /// Constructs a new [`Trace`] handler from a Tracer
    pub fn new(tracer: T) -> Self {
        Trace {
            route: None,
            error_type: None,
            enable_local_address_and_port: false,
            tracer,
            headers: vec![],
            socket_addr: None,
        }
    }

    /// provides a route specification to include in the trace spans.
    ///
    /// in order to avoid forcing anyone to use a particular router, this is provided as a
    /// configuration hook.
    ///
    /// for use with [`trillium-router`](https://docs.trillium.rs/trillium_router/index.html),
    /// ```
    /// use trillium_router::RouterConnExt;
    /// trillium_opentelemetry::Metrics::new(&opentelemetry::global::meter("example"))
    ///     .with_route(|conn| conn.route().map(|r| r.to_string().into()));
    /// ```
    pub fn with_route<F>(mut self, route: F) -> Self
    where
        F: Fn(&Conn) -> Option<Cow<'static, str>> + Send + Sync + 'static,
    {
        self.route = Some(Arc::new(route));
        self
    }

    /// Provides an optional low-cardinality error type specification to include in the trace spans.
    ///
    /// The implementation of this is application specific, but will often look like checking the
    /// [`Conn::state`] for an error enum and mapping that to a low-cardinality `&'static str`.
    pub fn with_error_type<F>(mut self, error_type: F) -> Self
    where
        F: Fn(&Conn) -> Option<Cow<'static, str>> + Send + Sync + 'static,
    {
        self.error_type = Some(Arc::new(error_type));
        self
    }

    /// Specify a list of request headers to include in the trace spans
    pub fn with_headers(
        mut self,
        headers: impl IntoIterator<Item = impl Into<HeaderName<'static>>>,
    ) -> Self {
        self.headers = headers.into_iter().map(Into::into).collect();
        self
    }

    /// Enable population of the local socket address and port in the trace spans.
    ///
    /// This populates the `network.local.address` and `network.local.port` attributes.
    pub fn with_local_address_and_port(mut self) -> Self {
        self.enable_local_address_and_port = true;
        self
    }
}

#[derive(Clone, Debug)]
pub(crate) struct TraceContext {
    pub(crate) context: Context,
}

struct RouteWasAvailable;

#[async_trait]
impl<T> Handler for Trace<T>
where
    T: Tracer + Send + Sync + 'static,
    T::Span: Send + Sync + 'static,
{
    async fn init(&mut self, info: &mut trillium::Info) {
        if self.enable_local_address_and_port {
            self.socket_addr = info.tcp_socket_addr().cloned();
        }
    }
    async fn run(&self, mut conn: Conn) -> Conn {
        let start_time =
            Some(SystemTime::now() - conn.inner().start_time().duration_since(Instant::now()));

        let scheme = if conn.is_secure() { "https" } else { "http" };
        let method = conn.method().as_str();

        let version = conn
            .inner()
            .http_version()
            .as_str()
            .strip_prefix("HTTP/")
            .unwrap();

        let mut attributes = vec![
            KeyValue::new("http.request.method", method),
            KeyValue::new("url.path", conn.inner().path().to_string()),
            KeyValue::new("url.scheme", scheme),
            KeyValue::new("url.query", conn.inner().querystring().to_string()),
            KeyValue::new("network.protocol.name", "http"),
            KeyValue::new("network.protocol.version", version),
        ];

        if let Some(socket_addr) = &self.socket_addr {
            attributes.push(KeyValue::new(
                "network.local.address",
                socket_addr.ip().to_string(),
            ));

            attributes.push(KeyValue::new(
                "network.local.port",
                i64::from(socket_addr.port()),
            ));
        }

        if let Some(peer_ip) = conn.inner().peer_ip() {
            attributes.push(KeyValue::new("client.address", peer_ip.to_string()));
        }

        for (header_name, header_values) in self.headers.iter().filter_map(|hn| {
            conn.request_headers()
                .get_values(hn.clone())
                .map(|v| (hn, v))
        }) {
            attributes.push(KeyValue::new(
                format!(
                    "http.request.header.{}",
                    header_name.as_ref().to_lowercase()
                ),
                Value::Array(Array::String(
                    header_values.iter().map(|x| x.to_string().into()).collect(),
                )),
            ));
        }

        let address_and_port = conn.inner().host().map(|host| {
            host.split_once(':')
                .and_then(|(host, port)| Some((String::from(host), port.parse().ok()?)))
                .unwrap_or_else(|| (String::from(host), if conn.is_secure() { 443 } else { 80 }))
        });

        if let Some((address, port)) = address_and_port {
            attributes.push(KeyValue::new("server.address", address));
            attributes.push(KeyValue::new("server.port", port));
        }

        if let Some(user_agent) = conn.request_headers().get_str(KnownHeaderName::UserAgent) {
            attributes.push(KeyValue::new("user_agent.original", user_agent.to_string()));
        }

        let name = if let Some(route) = self.route.as_ref().and_then(|route| route(&conn)) {
            conn.set_state(RouteWasAvailable);
            attributes.push(KeyValue::new("http.route", route.clone()));
            format!("{} {route}", conn.method().as_str()).into()
        } else {
            conn.method().as_str().into()
        };

        let span = self.tracer.build(SpanBuilder {
            name,
            start_time,
            span_kind: Some(SpanKind::Server),
            attributes: Some(attributes),
            ..SpanBuilder::default()
        });
        let context = Context::current_with_span(span);

        conn.with_state(TraceContext { context })
    }

    async fn before_send(&self, mut conn: Conn) -> Conn {
        let Some(TraceContext { context }) = conn.state().cloned() else {
            return conn;
        };

        let span = context.span();

        let error_type = self
            .error_type
            .as_ref()
            .and_then(|et| et(&conn))
            .or_else(|| {
                let status = conn.status().unwrap_or(Status::NotFound);
                if status.is_server_error() {
                    Some((status as u16).to_string().into())
                } else {
                    None
                }
            });

        if conn.status().map_or(false, |s| s.is_server_error()) {
            span.set_status(opentelemetry::trace::Status::Error {
                description: "".into(), // see error.type
            });
        }

        let status: i64 = (conn.status().unwrap_or(Status::NotFound) as u16).into();

        let mut attributes = vec![KeyValue::new("http.response.status_code", status)];

        if conn.take_state::<RouteWasAvailable>().is_none() {
            let route = self.route.as_ref().and_then(|route| route(&conn));
            if let Some(route) = &route {
                attributes.push(KeyValue::new("http.route", route.clone()));
                span.update_name(format!("{} {route}", conn.method().as_str()));
            }
        }

        if let Some(error_type) = error_type {
            attributes.push(KeyValue::new("error.type", error_type));
        }

        span.set_attributes(attributes);

        {
            let context = context.clone();
            conn.inner_mut().after_send(move |send_status| {
                let span = context.span();
                if !send_status.is_success() {
                    span.set_status(opentelemetry::trace::Status::Error {
                        description: "http send error".into(),
                    });
                    span.set_attribute(KeyValue::new("error.type", "http send error"));
                }
                span.end();
            });
        }

        conn
    }
}