ruststream 0.7.5

Async messaging framework for Rust: broker-agnostic traits, router, codecs, and a conformance harness for broker authors.
Documentation
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Prometheus metrics for the consume and publish paths (`metrics` feature).
//!
//! - [Prometheus][prometheus]: one [`Metrics`] collector hands out a consume [`Layer`] and a
//!   [`PublishLayer`] over one [`Registry`], and [`Metrics::export`] renders it for a
//!   `/metrics` route; [`Metrics`] lists the series.
//!
//! [prometheus]: https://powersemmi.github.io/ruststream/latest/components/observability/metrics/

use std::future::Future;
use std::sync::Arc;

use prometheus::{
    Encoder, HistogramOpts, HistogramVec, IntCounterVec, Opts, Registry, TextEncoder,
};

use crate::runtime::{
    BlanketLayer, Context, Handler, HandlerOutcome, HandlerResult, Layer, Outgoing, PublishLayer,
    PublishNext, PublishPipeline,
};

#[cfg(feature = "poll-diagnostics")]
use crate::runtime::PollDiagnostics;
#[cfg(feature = "poll-diagnostics")]
use poll::PollCollector;

/// Default histogram buckets (seconds) for handler duration.
const DURATION_BUCKETS: &[f64] = &[
    0.000_5, 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0,
];

struct Inner {
    registry: Registry,
    consumed: IntCounterVec,
    consume_duration: HistogramVec,
    published: IntCounterVec,
}

/// Collects consume and publish metrics into a shared Prometheus registry.
///
/// Cheap to clone (shares one registry and counter set). Hand [`consume_layer`](Self::consume_layer)
/// to [`RustStream::layer`](crate::runtime::RustStream::layer) and
/// [`publish_layer`](Self::publish_layer) to
/// [`RustStream::publish_layer`](crate::runtime::RustStream::publish_layer).
///
/// The consume layer records every handled message, and the publish layer every publish
/// attempt, the failed ones included. The registry is the global default unless
/// [`with_registry`](Self::with_registry) passes your own.
///
/// | Metric | Type | Labels |
/// |---|---|---|
/// | `ruststream_messages_consumed_total` | counter | `name`, `status` |
/// | `ruststream_consume_duration_seconds` | histogram | `name` |
/// | `ruststream_messages_published_total` | counter | `name`, `status` |
/// | `ruststream_handler_poll_duration_seconds` | histogram | `name` |
/// | `ruststream_handler_poll_average_seconds` | gauge | `name` |
///
/// The last two exist with the `poll-diagnostics` feature, once
/// [`Metrics::observe_poll_diagnostics`] hands the collector the app's
/// [`PollDiagnostics`]: the time a handler spent in one sampled poll, and its moving average.
///
/// `name` is the subscription or destination, and `status` the outcome: `ack` or `nack` on the
/// consume side, `ok` or `error` on the publish side.
///
/// # Examples
///
/// ```
/// use ruststream::metrics::Metrics;
/// use ruststream::runtime::{App, AppInfo, RustStream};
///
/// fn app(metrics: &Metrics) -> impl App {
///     RustStream::new(AppInfo::new("svc", "0.1.0"))
///         .layer(metrics.consume_layer())
///         .publish_layer(metrics.publish_layer())
/// }
///
/// # async fn serve() -> Result<(), Box<dyn std::error::Error>> {
/// // One collector per process: its registry is what the `/metrics` route renders.
/// let metrics = Metrics::new()?;
/// app(&metrics).run().await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct Metrics {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for Metrics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Metrics").finish_non_exhaustive()
    }
}

impl Metrics {
    /// Creates a metrics collector registered in the global default registry.
    ///
    /// # Errors
    ///
    /// Returns an error if a metric with one of the same names is already registered (for example,
    /// constructing two `Metrics` on the global registry).
    pub fn new() -> Result<Self, prometheus::Error> {
        Self::with_registry(prometheus::default_registry().clone())
    }

    /// Creates a metrics collector registered in `registry`.
    ///
    /// # Errors
    ///
    /// Returns an error if one of the metric names is already registered in `registry`.
    pub fn with_registry(registry: Registry) -> Result<Self, prometheus::Error> {
        let consumed = IntCounterVec::new(
            Opts::new(
                "ruststream_messages_consumed_total",
                "Messages handled, by name and outcome.",
            ),
            &["name", "status"],
        )?;
        let consume_duration = HistogramVec::new(
            HistogramOpts::new(
                "ruststream_consume_duration_seconds",
                "Handler execution time, by name.",
            )
            .buckets(DURATION_BUCKETS.to_vec()),
            &["name"],
        )?;
        let published = IntCounterVec::new(
            Opts::new(
                "ruststream_messages_published_total",
                "Messages published, by name and outcome.",
            ),
            &["name", "status"],
        )?;

        registry.register(Box::new(consumed.clone()))?;
        registry.register(Box::new(consume_duration.clone()))?;
        registry.register(Box::new(published.clone()))?;

        Ok(Self {
            inner: Arc::new(Inner {
                registry,
                consumed,
                consume_duration,
                published,
            }),
        })
    }

    /// The registry the metrics are registered in.
    #[must_use]
    pub fn registry(&self) -> &Registry {
        &self.inner.registry
    }

    /// A static consume-side [`Layer`] that times each handler and counts its outcome.
    #[must_use]
    pub fn consume_layer(&self) -> MetricsLayer {
        MetricsLayer {
            inner: Arc::clone(&self.inner),
        }
    }

    /// A publish-side [`PublishLayer`] that counts each published message.
    #[must_use]
    pub fn publish_layer(&self) -> MetricsPublish {
        MetricsPublish {
            inner: Arc::clone(&self.inner),
        }
    }

    /// Registers the poll-time diagnostics of `diagnostics` in the registry: the
    /// `ruststream_handler_poll_duration_seconds` histogram and the
    /// `ruststream_handler_poll_average_seconds` gauge, labelled per subscription and read from
    /// the reports at every [`export`](Self::export).
    ///
    /// # Errors
    ///
    /// Returns an error if the two metric names are already registered in the registry, for
    /// example by a second call.
    ///
    /// # Examples
    ///
    /// ```
    /// use prometheus::{Error, Registry};
    /// use ruststream::metrics::Metrics;
    /// use ruststream::runtime::{App, AppInfo, PollDiagnostics, RustStream};
    ///
    /// /// The registry is the service's own, shared with the collectors it already exports.
    /// fn app(registry: Registry) -> Result<impl App, Error> {
    ///     let metrics = Metrics::with_registry(registry)?;
    ///     let diagnostics = PollDiagnostics::new();
    ///     metrics.observe_poll_diagnostics(&diagnostics)?;
    ///     Ok(RustStream::new(AppInfo::new("svc", "0.1.0"))
    ///         .layer(metrics.consume_layer())
    ///         .poll_diagnostics(diagnostics))
    /// }
    /// ```
    #[cfg(feature = "poll-diagnostics")]
    pub fn observe_poll_diagnostics(
        &self,
        diagnostics: &PollDiagnostics,
    ) -> Result<(), prometheus::Error> {
        self.inner
            .registry
            .register(Box::new(PollCollector::new(diagnostics.clone())?))
    }

    /// Renders the registry in the Prometheus text exposition format.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying encoder fails to write the gathered metric families.
    pub fn export(&self) -> Result<String, prometheus::Error> {
        let mut buf = Vec::new();
        let encoder = TextEncoder::new();
        encoder.encode(&self.inner.registry.gather(), &mut buf)?;
        Ok(String::from_utf8_lossy(&buf).into_owned())
    }
}

const fn consume_status(result: HandlerResult) -> &'static str {
    match result {
        HandlerResult::Ack => "ack",
        // A delayed nack is still a nack for counting purposes.
        HandlerResult::Nack { .. } | HandlerResult::NackAfter { .. } => "nack",
    }
}

/// The [`Layer`] handed out by [`Metrics::consume_layer`]. Wraps a handler with timing and counters.
#[derive(Clone)]
pub struct MetricsLayer {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for MetricsLayer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MetricsLayer").finish_non_exhaustive()
    }
}

impl<H> Layer<H> for MetricsLayer {
    type Handler = MetricsHandler<H>;

    fn layer(&self, inner: H) -> Self::Handler {
        MetricsHandler {
            inner,
            metrics: Arc::clone(&self.inner),
        }
    }
}

// Lets the consume metric ride the application-wide stack and reach handlers mounted through a
// router (whose concrete types the router hides), the same way [`TracingLayer`] does.
impl BlanketLayer for MetricsLayer {
    fn apply<M, C, S, H>(&self, handler: H) -> impl Handler<M, C, S> + 'static
    where
        M: Send + Sync + 'static,
        C: Send + 'static,
        S: Send + Sync + 'static,
        H: Handler<M, C, S> + 'static,
    {
        self.layer(handler)
    }
}

/// The handler produced by [`MetricsLayer::layer`].
#[derive(Clone)]
pub struct MetricsHandler<H> {
    inner: H,
    metrics: Arc<Inner>,
}

impl<H> std::fmt::Debug for MetricsHandler<H> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MetricsHandler").finish_non_exhaustive()
    }
}

impl<M, C, S, H> Handler<M, C, S> for MetricsHandler<H>
where
    M: Sync,
    C: Send,
    S: Send + Sync,
    H: Handler<M, C, S>,
{
    fn handle(
        &self,
        msg: &M,
        ctx: &mut Context<'_, C, S>,
    ) -> impl Future<Output = HandlerOutcome> + Send {
        let name = ctx.name().to_owned();
        async move {
            let started = std::time::Instant::now();
            // Classify by the outcome inside the settlement; the continuation (if any) passes
            // through untouched to the dispatcher.
            let settle = self.inner.handle(msg, ctx).await;
            self.metrics
                .consume_duration
                .with_label_values(&[name.as_str()])
                .observe(started.elapsed().as_secs_f64());
            self.metrics
                .consumed
                .with_label_values(&[name.as_str(), consume_status(settle.outcome())])
                .inc();
            settle
        }
    }
}

/// The [`PublishLayer`] handed out by [`Metrics::publish_layer`].
#[derive(Clone)]
pub struct MetricsPublish {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for MetricsPublish {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MetricsPublish").finish_non_exhaustive()
    }
}

impl PublishLayer for MetricsPublish {
    fn on_publish<'a, N: PublishPipeline, P: crate::Publisher>(
        &'a self,
        out: &'a mut Outgoing<'a>,
        next: PublishNext<'a, N, P>,
    ) -> impl Future<Output = Result<(), Box<dyn std::error::Error + Send + Sync>>> + Send + 'a
    {
        let name = out.name().to_owned();
        async move {
            let result = next.run(out).await;
            let status = if result.is_ok() { "ok" } else { "error" };
            self.inner
                .published
                .with_label_values(&[name.as_str(), status])
                .inc();
            result
        }
    }
}

/// The poll-time diagnostics as a collector the registry reads at every gather.
#[cfg(feature = "poll-diagnostics")]
mod poll {
    use std::collections::HashMap;

    use prometheus::core::{Collector, Desc};
    use prometheus::proto::{
        Bucket, Gauge, Histogram, LabelPair, Metric, MetricFamily, MetricType,
    };

    use crate::runtime::PollDiagnostics;
    use crate::runtime::poll_diagnostics::BUCKET_BOUNDS_NS;

    const DURATION: &str = "ruststream_handler_poll_duration_seconds";
    const DURATION_HELP: &str = "Time a handler spent in one sampled poll, by name.";
    const AVERAGE: &str = "ruststream_handler_poll_average_seconds";
    const AVERAGE_HELP: &str = "Moving average of the time a handler spent in one poll, by name.";

    pub(super) struct PollCollector {
        diagnostics: PollDiagnostics,
        descs: [Desc; 2],
    }

    impl PollCollector {
        pub(super) fn new(diagnostics: PollDiagnostics) -> Result<Self, prometheus::Error> {
            let desc = |name: &str, help: &str| {
                Desc::new(
                    name.to_owned(),
                    help.to_owned(),
                    vec!["name".to_owned()],
                    HashMap::new(),
                )
            };
            Ok(Self {
                diagnostics,
                descs: [desc(DURATION, DURATION_HELP)?, desc(AVERAGE, AVERAGE_HELP)?],
            })
        }
    }

    impl Collector for PollCollector {
        fn desc(&self) -> Vec<&Desc> {
            self.descs.iter().collect()
        }

        // Nanosecond counts and bounds stay far below 2^52, where an `f64` holds them exactly.
        #[allow(clippy::cast_precision_loss)]
        fn collect(&self) -> Vec<MetricFamily> {
            let mut durations = Vec::new();
            let mut averages = Vec::new();
            for stats in self.diagnostics.stats() {
                let (samples, sum_ns, counts) = stats.histogram();
                let mut cumulative = 0;
                let buckets = BUCKET_BOUNDS_NS
                    .iter()
                    .zip(counts)
                    .map(|(&bound, count)| {
                        cumulative += count;
                        let mut bucket = Bucket::default();
                        bucket.set_upper_bound(bound as f64 / 1e9);
                        bucket.set_cumulative_count(cumulative);
                        bucket
                    })
                    .collect();
                let mut histogram = Histogram::default();
                histogram.set_sample_count(samples);
                histogram.set_sample_sum(sum_ns as f64 / 1e9);
                histogram.set_bucket(buckets);
                let mut metric = Metric::default();
                metric.set_label(label(stats.name()));
                metric.set_histogram(histogram);
                durations.push(metric);

                let mut gauge = Gauge::default();
                gauge.set_value(stats.report().average().as_secs_f64());
                let mut metric = Metric::default();
                metric.set_label(label(stats.name()));
                metric.set_gauge(gauge);
                averages.push(metric);
            }
            vec![
                family(DURATION, DURATION_HELP, MetricType::HISTOGRAM, durations),
                family(AVERAGE, AVERAGE_HELP, MetricType::GAUGE, averages),
            ]
        }
    }

    fn label(subscription: &str) -> Vec<LabelPair> {
        let mut pair = LabelPair::default();
        pair.set_name("name".to_owned());
        pair.set_value(subscription.to_owned());
        vec![pair]
    }

    fn family(name: &str, help: &str, kind: MetricType, metrics: Vec<Metric>) -> MetricFamily {
        let mut family = MetricFamily::default();
        family.set_name(name.to_owned());
        family.set_help(help.to_owned());
        family.set_field_type(kind);
        family.set_metric(metrics);
        family
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use prometheus::Registry;

    use super::{Metrics, consume_status};
    use crate::runtime::HandlerResult;

    #[test]
    fn the_registry_and_the_status_mapping() {
        let metrics = Metrics::with_registry(Registry::new()).unwrap();

        // registry() exposes the registry the three collectors were registered in: registering a
        // duplicate name there fails, which proves it is that same registry.
        let dup = prometheus::IntCounter::new("ruststream_messages_consumed_total", "dup").unwrap();
        assert!(metrics.registry().register(Box::new(dup)).is_err());

        // Every outcome maps to its counter label.
        assert_eq!(consume_status(HandlerResult::Ack), "ack");
        assert_eq!(consume_status(HandlerResult::drop()), "nack");
        assert_eq!(
            consume_status(HandlerResult::retry_after(Duration::from_secs(1))),
            "nack"
        );
    }
}