nm 0.1.30

Minimalistic high-performance metrics collection in highly concurrent environments
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
use std::marker::PhantomData;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::rc::Rc;
use std::sync::Arc;
use std::thread::LocalKey;

use crate::{
    Event, EventName, LOCAL_REGISTRY, Magnitude, MetricsPusher, ObservationBag, ObservationBagSync,
    PublishModel, Pull, Push, PusherPreRegistration,
};

/// Creates instances of [`Event`].
///
/// Required parameters:
/// * `name`
///
/// Use `Event::builder()` to create a new instance of this builder.
///
/// See [crate-level documentation][crate] for more details on how to create and use events.
#[derive(Debug)]
pub struct EventBuilder<P = Pull>
where
    P: PublishModel,
{
    name: EventName,

    /// Upper bounds (inclusive) of histogram buckets to use.
    /// Defaults to empty, which means no histogram for this event.
    ///
    /// Must be in ascending order if provided.
    /// Must not have a `Magnitude::MAX` bucket (automatically synthesized).
    histogram_buckets: &'static [Magnitude],

    /// If configured for the push publishing model, this is a pre-registration ticket
    /// from the pusher that will deliver the data to the reporting system.
    push_via: Option<PusherPreRegistration>,

    _p: PhantomData<P>,
    _single_threaded: PhantomData<*const ()>,
}

// EventBuilder is single-threaded (!Send, !Sync) and uses interior mutability only for
// metrics registration. Inconsistent state after a caught panic cannot affect safety.
impl<P: PublishModel> UnwindSafe for EventBuilder<P> {}
impl<P: PublishModel> RefUnwindSafe for EventBuilder<P> {}

impl<P> EventBuilder<P>
where
    P: PublishModel,
{
    pub(crate) fn new() -> Self {
        Self {
            name: EventName::default(),
            histogram_buckets: &[],
            push_via: None,
            _p: PhantomData,
            _single_threaded: PhantomData,
        }
    }

    /// Sets the name of the event. This is a required property.
    ///
    /// Recommended format: `big_medium_small_units`.
    /// For example: `net_http_connect_time_ns`.
    ///
    /// # Example
    ///
    /// ```
    /// use nm::Event;
    ///
    /// thread_local! {
    ///     static MY_EVENT: Event = Event::builder()
    ///         .name("http_requests_duration_ms")
    ///         .build();
    /// }
    /// ```
    #[must_use]
    pub fn name(self, name: impl Into<EventName>) -> Self {
        Self {
            name: name.into(),
            ..self
        }
    }

    /// Sets the upper bounds (inclusive) of histogram buckets to use
    /// when creating a histogram of event magnitudes.
    ///
    /// The default is to not create a histogram.
    ///
    /// # Example
    ///
    /// ```
    /// use nm::{Event, Magnitude};
    ///
    /// const RESPONSE_TIME_BUCKETS_MS: &[Magnitude] = &[1, 10, 50, 100, 500, 1000];
    ///
    /// thread_local! {
    ///     static HTTP_RESPONSE_TIME_MS: Event = Event::builder()
    ///         .name("http_response_time_ms")
    ///         .histogram(RESPONSE_TIME_BUCKETS_MS)
    ///         .build();
    /// }
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if bucket magnitudes are not in ascending order.
    ///
    /// Panics if one of the values is `Magnitude::MAX`. You do not need to specify this
    /// bucket yourself - it is automatically synthesized for all histograms to catch values
    /// that exceed the user-defined buckets.
    #[must_use]
    pub fn histogram(self, buckets: &'static [Magnitude]) -> Self {
        if !buckets.is_empty() {
            #[expect(
                clippy::indexing_slicing,
                reason = "windows() guarantees that we have exactly two elements"
            )]
            {
                assert!(
                    buckets.windows(2).all(|w| w[0] < w[1]),
                    "histogram buckets must be in ascending order"
                );
            }

            assert!(
                !buckets.contains(&Magnitude::MAX),
                "histogram buckets must not contain Magnitude::MAX"
            );
        }

        Self {
            histogram_buckets: buckets,
            ..self
        }
    }
}

impl EventBuilder<Pull> {
    /// Configures the event to be published using the push model,
    /// whereby a pusher is used to explicitly publish data for reporting purposes.
    ///
    /// Any data from such an event will only reach reports after [`MetricsPusher::push()`][1]
    /// is called on the referenced pusher.
    ///
    /// This can provide lower overhead than the pull model, which is the default,
    /// at the cost of delaying data updates until the pusher is invoked.
    /// Note that if nothing ever calls the pusher on a thread,
    /// the data from that thread will never be published.
    ///
    /// # Example
    ///
    /// ```
    /// use nm::{Event, MetricsPusher, Push};
    ///
    /// let pusher = MetricsPusher::new();
    /// let push_event: Event<Push> = Event::builder()
    ///     .name("push_example")
    ///     .pusher(&pusher)
    ///     .build();
    /// # // Example usage would require calling pusher.push()
    /// ```
    ///
    /// [1]: crate::MetricsPusher::push
    #[must_use]
    pub fn pusher(self, pusher: &MetricsPusher) -> EventBuilder<Push> {
        EventBuilder {
            name: self.name,
            histogram_buckets: self.histogram_buckets,
            push_via: Some(pusher.pre_register()),
            _p: PhantomData,
            _single_threaded: PhantomData,
        }
    }

    /// Configures the event to be published using the push model,
    /// whereby a pusher is used to explicitly publish data for reporting purposes.
    ///
    /// Any data from such an event will only reach reports after [`MetricsPusher::push()`][1]
    /// is called on the referenced pusher.
    ///
    /// This can provide lower overhead than the pull model, which is the default,
    /// at the cost of delaying data updates until the pusher is invoked.
    /// Note that if nothing ever calls the pusher on a thread,
    /// the data from that thread will never be published.
    ///
    /// # Example
    ///
    /// ```
    /// use nm::{Event, MetricsPusher, Push};
    ///
    /// thread_local! {
    ///     static PUSHER: MetricsPusher = MetricsPusher::new();
    ///
    ///     static PUSH_EVENT: Event<Push> = Event::builder()
    ///         .name("push_local_example")
    ///         .pusher_local(&PUSHER)
    ///         .build();
    /// }
    /// # // Example usage would require calling PUSHER.with(MetricsPusher::push)
    /// ```
    ///
    /// [1]: crate::MetricsPusher::push
    #[must_use]
    pub fn pusher_local(self, pusher: &'static LocalKey<MetricsPusher>) -> EventBuilder<Push> {
        pusher.with(|p| self.pusher(p))
    }

    /// Builds the event with the current configuration.
    ///
    /// # Panics
    ///
    /// Panics if a required parameter is not set.
    ///
    /// Panics if an event with this name has already been registered on this thread.
    /// You can only create an event with each name once per thread.
    #[must_use]
    #[cfg_attr(test, mutants::skip)] // Cargo-mutants does not understand this signature - every mutation is unviable waste of time.
    pub fn build(self) -> Event<Pull> {
        assert!(!self.name.is_empty());

        let observation_bag = Arc::new(ObservationBagSync::new(self.histogram_buckets));

        // This will panic if it is already registered. This is not strictly required and
        // we may relax this constraint in the future but for now we keep it here to help
        // uncover problematic patterns and learn when/where relaxed constraints may be useful.
        LOCAL_REGISTRY.with_borrow(|r| r.register(self.name, Arc::clone(&observation_bag)));

        Event::new(Pull {
            observations: observation_bag,
        })
    }
}

impl EventBuilder<Push> {
    /// Builds the event with the current configuration.
    ///
    /// # Panics
    ///
    /// Panics if a required parameter is not set.
    ///
    /// Panics if an event with this name has already been registered on this thread.
    /// You can only create an event with each name once per thread.
    #[must_use]
    pub fn build(self) -> Event<Push> {
        assert!(!self.name.is_empty());

        let observation_bag = Rc::new(ObservationBag::new(self.histogram_buckets));

        let pre_registration = self.push_via.expect("push_via must be set for push model");

        // This completes the registration that was started with the pre-registration ticket.
        // After this, the data set published by the pusher will include data from this event.
        pre_registration.register(self.name, Rc::clone(&observation_bag));

        Event::new(Push {
            observations: observation_bag,
        })
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use std::panic::{RefUnwindSafe, UnwindSafe};

    use static_assertions::{assert_impl_all, assert_not_impl_any};

    use super::*;
    use crate::LocalEventRegistry;

    assert_impl_all!(EventBuilder<Pull>: UnwindSafe, RefUnwindSafe);
    assert_impl_all!(EventBuilder<Push>: UnwindSafe, RefUnwindSafe);

    #[test]
    #[should_panic]
    fn build_without_name_panics() {
        drop(Event::builder().build());
    }

    #[test]
    #[should_panic]
    fn build_with_empty_name_panics() {
        drop(Event::builder().name("").build());
    }

    #[test]
    #[should_panic]
    fn build_with_magnitude_max_in_histogram_panics() {
        drop(
            Event::builder()
                .name("build_with_magnitude_max_in_histogram_panics")
                .histogram(&[1, 2, Magnitude::MAX])
                .build(),
        );
    }

    #[test]
    #[should_panic]
    fn build_with_non_ascending_histogram_buckets_panics() {
        drop(
            Event::builder()
                .name("build_with_non_ascending_histogram_buckets")
                .histogram(&[3, 2, 1])
                .build(),
        );
    }

    #[test]
    fn build_correctly_configures_histogram() {
        let no_histogram = Event::builder().name("no_histogram").build();
        assert!(no_histogram.snapshot().bucket_magnitudes.is_empty());
        assert!(no_histogram.snapshot().bucket_counts.is_empty());

        let empty_buckets = Event::builder()
            .name("empty_buckets")
            .histogram(&[])
            .build();
        assert!(empty_buckets.snapshot().bucket_magnitudes.is_empty());
        assert!(empty_buckets.snapshot().bucket_counts.is_empty());

        let buckets = &[1, 10, 100, 1000];
        let event_with_buckets = Event::builder()
            .name("with_buckets")
            .histogram(buckets)
            .build();

        let snapshot = event_with_buckets.snapshot();

        // Note: this does not include the `Magnitude::MAX` bucket.
        // The data for that bucket is synthesized at reporting time.
        assert_eq!(snapshot.bucket_magnitudes, buckets);
        assert_eq!(snapshot.bucket_counts.len(), buckets.len());
    }

    #[test]
    fn pull_build_registers_with_registry() {
        let previous_count = LOCAL_REGISTRY.with_borrow(LocalEventRegistry::event_count);

        // It does not matter whether we drop it - events are eternal,
        // dropping just means we can no longer observe occurrences of this event.
        drop(
            Event::builder()
                .name("pull_build_registers_with_registry")
                .build(),
        );

        let new_count = LOCAL_REGISTRY.with_borrow(LocalEventRegistry::event_count);

        assert_eq!(new_count, previous_count + 1);
    }

    #[test]
    fn pusher_build_registers_with_pusher() {
        let pusher = MetricsPusher::new();

        // It does not matter whether we drop it - events are eternal,
        // dropping just means we can no longer observe occurrences of this event.
        drop(
            Event::builder()
                .name("push_build_registers_with_pusher")
                .pusher(&pusher)
                .build(),
        );

        assert_eq!(pusher.event_count(), 1);
    }

    thread_local!(static LOCAL_PUSHER: MetricsPusher = MetricsPusher::new());

    #[test]
    fn pusher_local_build_registers_with_pusher() {
        // It does not matter whether we drop it - events are eternal,
        // dropping just means we can no longer observe occurrences of this event.
        drop(
            Event::builder()
                .name("push_build_registers_with_pusher")
                .pusher_local(&LOCAL_PUSHER)
                .build(),
        );

        assert_eq!(LOCAL_PUSHER.with(MetricsPusher::event_count), 1);
    }

    #[test]
    fn single_threaded_type() {
        assert_not_impl_any!(EventBuilder: Send, Sync);
    }

    #[test]
    #[should_panic]
    fn register_pull_and_push_same_name_panics() {
        let pusher = MetricsPusher::new();

        // It does not matter whether we drop it - events are eternal,
        // dropping just means we can no longer observe occurrences of this event.
        drop(
            Event::builder()
                .name("conflicting_name_pull_and_push")
                .pusher(&pusher)
                .build(),
        );

        drop(
            Event::builder()
                .name("conflicting_name_pull_and_push")
                .build(),
        );
    }
}