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;
32#[cfg(feature = "runtime")]
33mod runtime;
34pub mod span;
35mod temporality;
36
37pub use export::text::{DogStatsDExport, PrometheusExport};
38
39// These helpers are public only because the ExportMetrics proc macro generates
40// code that calls them. They are not part of the public API and may change
41// without notice.
42#[doc(hidden)]
43pub mod __macro_support {
44    pub use crate::export::text::{
45        __write_dogstatsd, __write_dogstatsd_distribution, __write_dogstatsd_distribution_delta,
46        __write_dogstatsd_distribution_delta_dynamic,
47        __write_dogstatsd_distribution_delta_dynamic_pairs, __write_dogstatsd_distribution_dynamic,
48        __write_dogstatsd_dynamic, __write_dogstatsd_dynamic_pairs, __write_dogstatsd_with_label,
49        FastFormat,
50    };
51}
52pub use metric::ExportMetrics;
53pub use metric::{
54    Counter, CounterSet, CounterSetBuffer, Distribution, DistributionSnapshot, DynamicCounter,
55    DynamicCounterSeries, DynamicCounterSet, DynamicCounterSetSeries, DynamicDistribution,
56    DynamicDistributionSeries, DynamicGauge, DynamicGaugeI64, DynamicGaugeI64Series,
57    DynamicGaugeSeries, DynamicHistogram, DynamicHistogramSeries, DynamicHistogramSeriesView,
58    DynamicLabelSet, Gauge, GaugeF64, Histogram, HistogramSnapshot, LabelEnum, LabeledCounter,
59    LabeledGauge, LabeledHistogram, LabeledSampledTimer, MaxGauge, MaxGaugeF64, MetricKind,
60    MetricLabel, MetricLabels, MetricLabelsIter, MetricMeta, MetricVisitor, MinGauge, MinGaugeF64,
61    SampledTimer, SampledTimerGuard,
62};
63#[cfg(feature = "eviction")]
64pub use metric::{advance_cycle, current_cycle};
65#[cfg(feature = "runtime")]
66pub use runtime::{MetricScope, RegisteredMetrics, Runtime, RuntimeConfig};
67pub use span::{
68    CompletedSpan, Span, SpanAttribute, SpanCollector, SpanEvent, SpanId, SpanKind, SpanStatus,
69    SpanValue, TraceId, current_span_id, current_trace_id,
70};
71pub use temporality::Temporality;
72
73#[cfg(feature = "otlp")]
74pub use export::otlp::OtlpExport;
75
76#[cfg(feature = "clickhouse")]
77pub use export::clickhouse::{ClickHouseExport, ClickHouseMetricBatch};
78
79#[cfg(feature = "macros")]
80pub use fast_telemetry_macros::{ExportMetrics, LabelEnum as DeriveLabel};
81
82// Internal compatibility aliases for moved modules.
83pub(crate) use internal::exp_buckets;
84pub(crate) use internal::thread_id;
85pub(crate) use metric::label;
86
87#[cfg(feature = "otlp")]
88pub mod otlp {
89    pub use crate::export::otlp::*;
90}
91
92#[cfg(feature = "clickhouse")]
93pub mod clickhouse {
94    pub use crate::export::clickhouse::*;
95}