Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/platforms/rust/common/metrics/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Set Up Metrics
sidebar_title: Metrics
description: "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."
sidebar_section: features
new: true
sidebar_order: 5700
---

With [Sentry's Application Metrics](/product/explore/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.

## Requirements

<PlatformContent includePath="metrics/requirements" />

## Usage

<PlatformContent includePath="metrics/usage" />

## Options

<PlatformContent includePath="metrics/options" />

## Default Attributes

<PlatformContent includePath="metrics/default-attributes" />
10 changes: 10 additions & 0 deletions platform-includes/metrics/default-attributes/rust.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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 |
38 changes: 38 additions & 0 deletions platform-includes/metrics/options/rust.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
### Enabling Metrics

To capture metrics, you need to compile the `sentry` crate with the `metrics` feature and set [`enable_metrics: true`](https://docs.rs/sentry/0.48.0/sentry/struct.ClientOptions.html#structfield.enable_metrics) in the [`ClientOptions`](https://docs.rs/sentry/0.48.0/sentry/struct.ClientOptions.html).
Copy link
Copy Markdown
Member

@lcian lcian Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To capture metrics, you need to compile the `sentry` crate with the `metrics` feature and set [`enable_metrics: true`](https://docs.rs/sentry/0.48.0/sentry/struct.ClientOptions.html#structfield.enable_metrics) in the [`ClientOptions`](https://docs.rs/sentry/0.48.0/sentry/struct.ClientOptions.html).
To capture metrics, you need to compile the `sentry` crate with the `metrics` feature and set [`enable_metrics: true`](https://docs.rs/sentry/latest/sentry/struct.ClientOptions.html#structfield.enable_metrics) in the [`ClientOptions`](https://docs.rs/sentry/latest/sentry/struct.ClientOptions.html).

Seems like a nice idea to link to the API docs (it's not a pattern I've been using before), however I would suggest to link to the latest so that the user is brought to the latest release every time.


```rust {filename:main.rs}
let _guard = sentry::init((
"___PUBLIC_DSN___",
sentry::ClientOptions {
enable_metrics: true,
..Default::default()
},
));
```

Set `enable_metrics: false` or omit the option to stop sending metrics.

### Filtering Metrics

Use [`before_send_metric`](https://docs.rs/sentry/0.48.0/sentry/struct.ClientOptions.html#structfield.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`:

```rust {filename:main.rs}
let _guard = sentry::init((
"___PUBLIC_DSN___",
sentry::ClientOptions {
enable_metrics: true,
before_send_metric: Some(std::sync::Arc::new(|metric| {
if metric.name == "debug.metric" {
return None;
}

Some(metric)
})),
..Default::default()
},
));
```
18 changes: 18 additions & 0 deletions platform-includes/metrics/requirements/rust.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Metrics for Rust are supported in Sentry Rust SDK version `0.48.0` or later when compiled with the `metrics` feature.

```toml {filename:Cargo.toml}
[dependencies]
sentry = { version = "{{@inject packages.version('sentry.rust') }}", features = ["metrics"] }
```

To capture metrics, you must also set `enable_metrics: true` when initializing the SDK.

```rust {filename:main.rs}
let _guard = sentry::init((
"___PUBLIC_DSN___",
sentry::ClientOptions {
enable_metrics: true, // <-- Add this line
..Default::default()
},
));
```
48 changes: 48 additions & 0 deletions platform-includes/metrics/usage/rust.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
The SDK's [`sentry::metrics` module](https://docs.rs/sentry/0.48.0/sentry/metrics/index.html) contains functions for capturing each of the [three metric types](/product/explore/metrics/#metric-types) that Sentry supports.

Metrics are automatically associated with the trace and, if applicable, the span they are captured within.

### Capture a Counter

Capture [counter metrics](/product/explore/metrics/#counters) using [`metrics::counter`](https://docs.rs/sentry/0.48.0/sentry/metrics/fn.counter.html).

Counters track occurrences, such as handled HTTP requests, and are always unitless.

```rust {filename:main.rs}
use sentry::metrics;

metrics::counter("http.requests", 1).capture();
```

### Capture a Gauge

Capture [gauge metrics](/product/explore/metrics/#gauges) using [`metrics::gauge`](https://docs.rs/sentry/0.48.0/sentry/metrics/fn.gauge.html).

Gauges track current state or level, such as queue depth or active connections, and support [units](https://docs.rs/sentry/0.48.0/sentry/metrics/struct.GaugeMetric.html#method.unit).

```rust {filename:main.rs}
use sentry::metrics;

metrics::gauge("queue.depth", 42).capture();
```

### Distribution

Capture [distribution metrics](/product/explore/metrics/#distributions) using [`metrics::distribution`](https://docs.rs/sentry/0.48.0/sentry/metrics/fn.distribution.html)

Distributions track values that vary and need statistical analysis, such as response times or payload sizes, and they support [units](https://docs.rs/sentry/0.48.0/sentry/metrics/struct.DistributionMetric.html#method.unit).

```rust {filename:main.rs}
use sentry::metrics;
use sentry::protocol::Unit;

metrics::distribution("http.response_time", 187.5)
.unit(Unit::Millisecond)
.capture();
```

### Setting Attributes

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](https://getsentry.github.io/sentry-conventions/) for attribute keys.
Loading