-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs(rust-sdk): Document metrics #17545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
szokeasaurusrex
wants to merge
1
commit into
master
Choose a base branch
from
szokeasaurusrex/metrics-rust
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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). | ||
|
|
||
| ```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() | ||
| }, | ||
| )); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| }, | ||
| )); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.