Skip to main content

rama_http/layer/trace/
on_failure.rs

1use super::{DEFAULT_ERROR_LEVEL, Latency};
2use rama_core::telemetry::tracing::{Level, Span};
3use rama_utils::latency::LatencyUnit;
4use std::{fmt, time::Duration};
5
6/// Trait used to tell [`Trace`] what to do when a request fails.
7///
8/// See the [module docs](../trace/index.html#on_failure) for details on exactly when the
9/// `on_failure` callback is called.
10///
11/// [`Trace`]: super::Trace
12pub trait OnFailure<FailureClass>: Send + Sync + 'static {
13    /// Do the thing.
14    ///
15    /// `latency` is the duration since the request was received.
16    ///
17    /// `span` is the `tracing` [`Span`], corresponding to this request, produced by the closure
18    /// passed to [`TraceLayer::make_span_with`]. It can be used to [record field values][record]
19    /// that weren't known when the span was created.
20    ///
21    /// [`Span`]: https://docs.rs/tracing/latest/tracing/span/index.html
22    /// [record]: https://docs.rs/tracing/latest/tracing/span/struct.Span.html#method.record
23    /// [`TraceLayer::make_span_with`]: crate::layer::trace::TraceLayer::make_span_with
24    fn on_failure(&self, failure_classification: FailureClass, latency: Duration, span: &Span);
25}
26
27impl<FailureClass> OnFailure<FailureClass> for () {
28    #[inline]
29    fn on_failure(&self, _: FailureClass, _: Duration, _: &Span) {}
30}
31
32impl<F, FailureClass> OnFailure<FailureClass> for F
33where
34    F: Fn(FailureClass, Duration, &Span) + Send + Sync + 'static,
35{
36    fn on_failure(&self, failure_classification: FailureClass, latency: Duration, span: &Span) {
37        self(failure_classification, latency, span)
38    }
39}
40
41/// The default [`OnFailure`] implementation used by [`Trace`].
42///
43/// [`Trace`]: super::Trace
44#[derive(Clone, Debug)]
45pub struct DefaultOnFailure {
46    level: Level,
47    latency_unit: LatencyUnit,
48}
49
50impl Default for DefaultOnFailure {
51    fn default() -> Self {
52        Self {
53            level: DEFAULT_ERROR_LEVEL,
54            latency_unit: LatencyUnit::Millis,
55        }
56    }
57}
58
59impl DefaultOnFailure {
60    /// Create a new `DefaultOnFailure`.
61    #[must_use]
62    pub fn new() -> Self {
63        Self::default()
64    }
65
66    rama_utils::macros::generate_set_and_with! {
67        /// Set the [`Level`] used for [tracing events].
68        ///
69        /// Defaults to [`Level::ERROR`].
70        ///
71        /// [tracing events]: https://docs.rs/tracing/latest/tracing/#events
72        pub fn level(mut self, level: Level) -> Self {
73            self.level = level;
74            self
75        }
76    }
77
78    rama_utils::macros::generate_set_and_with! {
79        /// Set the [`LatencyUnit`] latencies will be reported in.
80        ///
81        /// Defaults to [`LatencyUnit::Millis`].
82        pub fn latency_unit(mut self, latency_unit: LatencyUnit) -> Self {
83            self.latency_unit = latency_unit;
84            self
85        }
86    }
87}
88
89impl<FailureClass> OnFailure<FailureClass> for DefaultOnFailure
90where
91    FailureClass: fmt::Display,
92{
93    fn on_failure(&self, failure_classification: FailureClass, latency: Duration, span: &Span) {
94        let latency = Latency {
95            unit: self.latency_unit,
96            duration: latency,
97        };
98        event_dynamic_lvl!(
99            parent: span,
100            self.level,
101            classification = %failure_classification,
102            %latency,
103            "response failed"
104        );
105    }
106}