fast-telemetry-macros
Derive macros for the fast-telemetry crate.
Macros
#[derive(LabelEnum)]
Auto-generates LabelEnum trait implementation for enums used as metric labels.
use ;
// Generates:
// impl LabelEnum for HttpMethod {
// const CARDINALITY: usize = 4;
// const LABEL_NAME: &'static str = "method";
// fn as_index(self) -> usize { ... }
// fn from_index(index: usize) -> Self { ... }
// fn variant_name(self) -> &'static str { ... }
// }
let counter: = new;
counter.inc;
Attributes
| Attribute | Target | Required | Description |
|---|---|---|---|
#[label_name = "..."] |
enum | Yes | Prometheus label name (e.g., "method", "error_type") |
#[label = "..."] |
variant | No | Override auto-generated variant name |
Snake Case Conversion
Variant names are automatically converted to snake_case:
#[derive(ExportMetrics)]
Auto-generates Prometheus, DogStatsD, and optional OTLP export methods for metric structs.
use ;
let metrics = ApiMetrics ;
let mut output = Stringnew;
metrics.export_prometheus;
// Output:
// # HELP api_requests_total Total requests processed
// # TYPE api_requests_total counter
// api_requests_total 1234
// # HELP api_connections Current active connections
// # TYPE api_connections gauge
// api_connections 42
// # HELP api_latency Request latency in microseconds
// # TYPE api_latency histogram
// api_latency_bucket{le="10"} 100
// ...
Attributes
| Attribute | Target | Required | Description |
|---|---|---|---|
#[metric_prefix = "..."] |
struct | No | Prefix for all metric names |
#[otlp] |
struct | No | Generate export_otlp() method (requires fast-telemetry/otlp feature) |
#[help = "..."] |
field | No | Help text (defaults to field name) |
Generated methods:
export_prometheus(&self, output: &mut String)export_dogstatsd(&self, output: &mut String, tags: &[(&str, &str)])export_dogstatsd_delta(&self, output: &mut String, tags: &[(&str, &str)], state: &mut ...State)export_dogstatsd_with_temporality(&self, output: &mut String, tags: &[(&str, &str)], temporality: fast_telemetry::Temporality, state: &mut ...State)export_otlp(&self, output: &mut Vec<fast_telemetry::otlp::pb::Metric>)when#[otlp]is present
The delta-capable methods use a generated state type named
<YourStructName>DogStatsDState.
Supported Field Types
| Type | Prometheus Type |
|---|---|
Counter |
counter |
Distribution |
histogram |
Gauge |
gauge |
GaugeF64 |
gauge |
Histogram |
histogram |
LabeledCounter<L> |
counter (with labels) |
LabeledGauge<L> |
gauge (with labels) |
LabeledHistogram<L> |
histogram (with labels) |
DynamicCounter |
counter (with runtime labels) |
DynamicDistribution |
histogram (with runtime labels) |
DynamicGauge |
gauge (with runtime labels) |
DynamicGaugeI64 |
gauge (with runtime labels) |
DynamicHistogram |
histogram (with runtime labels) |
Complete Example
use ;
// Define label enums
// Define metrics struct
Output:
# HELP http_requests_total Total HTTP requests
# TYPE http_requests_total counter
http_requests_total 1
# HELP http_requests_by_method HTTP requests by method
# TYPE http_requests_by_method counter
http_requests_by_method{method="get"} 1
http_requests_by_method{method="post"} 0
http_requests_by_method{method="put"} 0
http_requests_by_method{method="delete"} 0
# HELP http_requests_by_status HTTP requests by status class
# TYPE http_requests_by_status counter
http_requests_by_status{status="success2xx"} 1
http_requests_by_status{status="redirect3xx"} 0
http_requests_by_status{status="client_error4xx"} 0
http_requests_by_status{status="server_error5xx"} 0
# HELP http_latency_by_method Request latency by method (microseconds)
# TYPE http_latency_by_method histogram
http_latency_by_method_bucket{method="get",le="10"} 0
http_latency_by_method_bucket{method="get",le="50"} 0
http_latency_by_method_bucket{method="get",le="100"} 0
http_latency_by_method_bucket{method="get",le="500"} 1
...
# HELP http_in_flight Current in-flight requests
# TYPE http_in_flight gauge
http_in_flight 5