Skip to main content

fast_telemetry/
lib.rs

1//! Fast, thread-sharded counters and histograms for high-performance telemetry.
2//!
3//! OpenTelemetry counters use atomic operations with global synchronization,
4//! which causes cache-line bouncing across cores. This crate provides
5//! thread-local sharded counters that:
6//!
7//! - Increment with no cross-thread contention (just a thread-local write)
8//! - Are cache-line padded to avoid false sharing (using crossbeam's CachePadded)
9//! - Aggregate on read (sum all shards)
10//!
11//! # Labeled Metrics
12//!
13//! For dimensional metrics (counters/gauges/histograms broken down by label),
14//! use the `Labeled*` types with a `LabelEnum` implementation. These provide
15//! O(1) lookup via array indexing instead of HashMap lookups.
16//!
17//! ```ignore
18//! use fast_telemetry::{LabeledCounter, LabelEnum};
19//!
20//! #[derive(Copy, Clone, Debug)]
21//! enum HttpMethod { Get, Post, Put, Delete }
22//!
23//! impl LabelEnum for HttpMethod { /* ... */ }
24//!
25//! let counter: LabeledCounter<HttpMethod> = LabeledCounter::new(4);
26//! counter.inc(HttpMethod::Get);  // O(1) array index, no hashing
27//! ```
28
29mod export;
30pub(crate) mod internal;
31mod metric;
32pub mod span;
33mod temporality;
34
35pub use export::text::{DogStatsDExport, PrometheusExport};
36
37// These helpers are public only because the ExportMetrics proc macro generates
38// code that calls them. They are not part of the public API and may change
39// without notice.
40#[doc(hidden)]
41pub mod __macro_support {
42    pub use crate::export::text::{
43        __write_dogstatsd, __write_dogstatsd_distribution, __write_dogstatsd_distribution_delta,
44        __write_dogstatsd_distribution_delta_dynamic,
45        __write_dogstatsd_distribution_delta_dynamic_pairs, __write_dogstatsd_distribution_dynamic,
46        __write_dogstatsd_dynamic, __write_dogstatsd_dynamic_pairs, __write_dogstatsd_with_label,
47    };
48}
49pub use metric::{
50    Counter, Distribution, DynamicCounter, DynamicCounterSeries, DynamicDistribution,
51    DynamicDistributionSeries, DynamicGauge, DynamicGaugeI64, DynamicGaugeI64Series,
52    DynamicGaugeSeries, DynamicHistogram, DynamicHistogramSeries, DynamicHistogramSeriesView,
53    DynamicLabelSet, Gauge, GaugeF64, Histogram, LabelEnum, LabeledCounter, LabeledGauge,
54    LabeledHistogram,
55};
56#[cfg(feature = "eviction")]
57pub use metric::{advance_cycle, current_cycle};
58pub use span::{
59    CompletedSpan, Span, SpanAttribute, SpanCollector, SpanEvent, SpanId, SpanKind, SpanStatus,
60    SpanValue, TraceId, current_span_id, current_trace_id,
61};
62pub use temporality::Temporality;
63
64#[cfg(feature = "otlp")]
65pub use export::otlp::OtlpExport;
66
67#[cfg(feature = "macros")]
68pub use fast_telemetry_macros::{ExportMetrics, LabelEnum as DeriveLabel};
69
70// Internal compatibility aliases for moved modules.
71pub(crate) use internal::exp_buckets;
72pub(crate) use internal::thread_id;
73pub(crate) use metric::label;
74
75#[cfg(feature = "otlp")]
76pub mod otlp {
77    pub use crate::export::otlp::*;
78}