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
Copied
[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.rs
Copied
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.rs
Copied
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.rs
Copied
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.rs
Copied
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.rs
Copied
let _guard = sentry::init((
    "___PUBLIC_DSN___",
    sentry::ClientOptions {
        enable_metrics: false,
        ..Default::default()
    },
));

Sentry automatically attaches these attributes to every metric:

AttributeDescriptionWhen Present
sentry.sdk.nameSDK nameAlways
sentry.sdk.versionSDK versionAlways
sentry.environmentEnvironment from SDK configurationIf configured
sentry.releaseRelease from SDK configurationIf configured
server.addressServer name from SDK configurationIf configured
user.id, user.name, user.emailUser identifiers from the current scopeIf a user is set and send_default_pii is enabled
Was this helpful?
Help improve this content
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").