Set Up Metrics
Metrics allow you to send, view and query counters, gauges and measurements from your Sentry-configured apps to track application health and drill down into related traces, logs, and errors.
With Sentry's Application Metrics, you can send counters, gauges, and distributions from your Rust applications to Sentry. Once in Sentry, these metrics can be viewed alongside relevant errors and searched using their individual attributes.
Metrics for Rust are supported in Sentry Rust SDK version 0.48.0 or later when compiled with the metrics feature.
Cargo.toml[dependencies]
sentry = { version = "0.48.2", features = ["metrics"] }
[dependencies]
sentry = { version = "0.48.2", features = ["metrics"] }
Metrics are enabled by default.
The SDK's sentry::metrics module contains functions for capturing each of the three metric types that Sentry supports.
Metrics are automatically associated with the trace and, if applicable, the span they are captured within.
Capture counter metrics using metrics::counter.
Counters track occurrences, such as handled HTTP requests, and are always unitless.
main.rsuse sentry::metrics;
metrics::counter("http.requests", 1).capture();
use sentry::metrics;
metrics::counter("http.requests", 1).capture();
Capture gauge metrics using metrics::gauge.
Gauges track current state or level, such as queue depth or active connections, and support units.
main.rsuse sentry::metrics;
metrics::gauge("queue.depth", 42).capture();
use sentry::metrics;
metrics::gauge("queue.depth", 42).capture();
Capture distribution metrics using metrics::distribution.
Distributions track values that vary and need statistical analysis, such as response times or payload sizes, and they support units.
main.rsuse sentry::metrics;
use sentry::protocol::Unit;
metrics::distribution("http.response_time", 187.5)
.unit(Unit::Millisecond)
.capture();
use sentry::metrics;
use sentry::protocol::Unit;
metrics::distribution("http.response_time", 187.5)
.unit(Unit::Millisecond)
.capture();
Attributes let you filter and group metrics in Sentry. Add them with .attribute(key, value) before you call .capture(). You can add multiple attributes by chaining calls to .attribute.
When possible, use Sentry Semantic Conventions for attribute keys.
Use before_send_metric to drop or update metrics before Sentry sends them. Return None to drop a metric.
For example, to filter all metrics with the name "debug.metric", you could use the following before_send_metric:
main.rslet _guard = sentry::init((
"___PUBLIC_DSN___",
sentry::ClientOptions {
before_send_metric: Some(std::sync::Arc::new(|metric| {
if metric.name == "debug.metric" {
return None;
}
Some(metric)
})),
..Default::default()
},
));
let _guard = sentry::init((
"___PUBLIC_DSN___",
sentry::ClientOptions {
before_send_metric: Some(std::sync::Arc::new(|metric| {
if metric.name == "debug.metric" {
return None;
}
Some(metric)
})),
..Default::default()
},
));
Metrics are enabled by default when the sentry crate is compiled with the metrics feature. To stop sending metrics, set enable_metrics: false in the ClientOptions.
main.rslet _guard = sentry::init((
"___PUBLIC_DSN___",
sentry::ClientOptions {
enable_metrics: false,
..Default::default()
},
));
let _guard = sentry::init((
"___PUBLIC_DSN___",
sentry::ClientOptions {
enable_metrics: false,
..Default::default()
},
));
Sentry automatically attaches these attributes to every metric:
| Attribute | Description | When Present |
|---|---|---|
sentry.sdk.name | SDK name | Always |
sentry.sdk.version | SDK version | Always |
sentry.environment | Environment from SDK configuration | If configured |
sentry.release | Release from SDK configuration | If configured |
server.address | Server name from SDK configuration | If configured |
user.id, user.name, user.email | User identifiers from the current scope | If a user is set and send_default_pii is enabled |
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").