metrics_exporter_sentry/
lib.rs

1//! Metrics Exporter for Sentry.
2//!
3//! `metrics-exporter-sentry` is a [`metrics`] compatible exporter which
4//! supports reporting metrics to [Sentry](https://docs.sentry.io/product/metrics/).
5//!
6//! # Usage
7//!
8//! ```no_run
9//! use metrics_exporter_sentry::SentryRecorder;
10//!
11//! // Setup Sentry SDK.
12//! let _sentry = sentry::init("https://key@sentry.io/123");
13//!
14//! // Install the exporter.
15//! metrics::set_global_recorder(SentryRecorder::new()).unwrap();
16//!
17//! // Metric visible in Sentry!
18//! metrics::counter!("requests").increment(1);
19//! ```
20//!
21//! ## Units
22//!
23//! In Sentry units are an integral part of each metric, in fact you can emit
24//! multiple metrics with the same name but a different unit and they are
25//! treated as separate metrics!
26//!
27//! By default all metrics are emitted with the [`none`](sentry::metrics::MetricUnit::None) unit.
28//! You can change the unit using metrics `describe_*` macros:
29//!
30//! ```
31//! use metrics::{histogram, describe_histogram, Unit};
32//! # let request_duration = std::time::Duration::from_secs(1);
33//!
34//! describe_histogram!("request.duration", Unit::Seconds, "Duration of a request in seconds");
35//! histogram!("request.duration").record(request_duration);
36//! ```
37//!
38//! Or by using the special `unit` tag:
39//!
40//! ```
41//! use metrics::histogram;
42//! # let request_duration = std::time::Duration::from_secs(1);
43//!
44//! histogram!("request.duration", "unit" => "seconds").record(request_duration);
45//! ```
46//!
47//! The conversion from [`metrics::Unit`] to [`sentry::metrics::MetricUnit`] is on a best effort
48//! basis and some variants of [`metrics::Unit`] are emitted as a
49//! [custom](sentry::metrics::MetricUnit::Custom) Sentry unit.
50//!
51//! When passing the unit as a tag, the unit is parsed directly as a [`sentry::metrics::MetricUnit`],
52//! allowing you to pass units which cannot be represented as a [`metrics::Unit`].
53//!
54//! Generally it's recommended to use the `describe_*` macros.
55//!
56//! # Incompatibilities
57//!
58//! [`metrics`] supports operations on counters and gauges which cannot be represented in Sentry
59//! metrics.
60//!
61//! The [`Counter::absolute`](metrics::Counter::absolute), [`Gauge::increment`](metrics::Gauge::increment)
62//! and [`Gauge::decrement`](metrics::Gauge::decrement) operations are ignored by the [`SentryRecorder`].
63//!
64//! Sentry supports another type of metrics, sets. At this time [`metrics`] does not support
65//! sets and thus cannot be emitted with this exporter.
66
67mod recorder;
68
69pub use self::recorder::*;