- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.6k
feat(python): Document metrics #15299
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
Changes from all commits
2eaf71a
              48dc13f
              2438b78
              db3ad6b
              d501e05
              17b3ef8
              3d6ee2d
              7e34ad2
              659384b
              6f72644
              973daae
              cc376fa
              94a8623
              4dd0114
              7324695
              9a2b6b8
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| title: Set Up Metrics | ||
| sidebar_title: Metrics | ||
| description: "Metrics allow you to send, view and query counters, gauges and measurements sent from your applications within Sentry." | ||
| sidebar_order: 5755 | ||
| --- | ||
|  | ||
| <Alert> | ||
|  | ||
| This feature is currently in limited beta. Please reach out on [GitHub](https://github.com/getsentry/sentry-python/discussions/5042) if you have feedback or questions. Features in beta are still in-progress and may have bugs. We recognize the irony. | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference between limited beta and beta? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are in a limited beta right now, users can only see metrics in their Sentry if a feature flag is turned on for their Sentry organization. | ||
|  | ||
| </Alert> | ||
|  | ||
| With Sentry Metrics, you can send counters, gauges and distributions from your 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" /> | ||
|  | ||
| ## Setup | ||
|  | ||
| <PlatformContent includePath="metrics/setup" /> | ||
|  | ||
| ## Usage | ||
|  | ||
| <PlatformContent includePath="metrics/usage" /> | ||
|  | ||
| ## Options | ||
|  | ||
| <PlatformContent includePath="metrics/options" /> | ||
|  | ||
| ## Default Attributes | ||
|  | ||
| <PlatformContent includePath="metrics/default-attributes" /> | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ### Core Attributes | ||
|  | ||
| - `environment`: The environment set in the SDK if defined. This is sent from the SDK as `sentry.environment`. | ||
| - `release`: The release set in the SDK if defined. This is sent from the SDK as `sentry.release`. | ||
| - `trace.parent_span_id`: The span ID of the span that was active when the metric was collected (only set if there was an active span). This is sent from the SDK as `sentry.trace.parent_span_id`. | ||
| - `sdk.name`: The name of the SDK that sent the metric. This is sent from the SDK as `sentry.sdk.name`. | ||
| - `sdk.version`: The version of the SDK that sent the metric. This is sent from the SDK as `sentry.sdk.version`. | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Server Attributes | ||
|  | ||
| - `server.address`: The address of the server that sent the metric. Equivalent to `server_name` that gets attached to Sentry errors. | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ### User Attributes | ||
|  | ||
| If user information is available in the current scope, the following attributes are added to the metric: | ||
|  | ||
| - `user.id`: The user ID. | ||
| - `user.name`: The username. | ||
| - `user.email`: The email address. | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| The Python SDK automatically sets several default attributes on all metrics to provide context and improve debugging: | ||
|  | ||
| <Include name="metrics/default-attributes/core" /> | ||
|  | ||
| <Include name="metrics/default-attributes/server" /> | ||
|  | ||
| <Include name="metrics/default-attributes/user" /> | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #### before_send_metric | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this should link to the options doc, and this option should be added to it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The organization is based on the logs documentation. Currently we do not have experimental options in the options page, so I will keep it here for now. But I agree that we could document experimental options as well and link to them in other pages. | ||
|  | ||
| To filter metrics, or update them before they are sent to Sentry, you can use the `before_send_metric` option. | ||
|  | ||
| ```python | ||
| import sentry_sdk | ||
| from sentry_sdk.types import Metric, Hint | ||
| from typing import Optional | ||
|  | ||
| def before_metric(metric: Metric, _hint: Hint) -> Optional[Metric]: | ||
| # Filter out all failed checkouts on the acme tenant | ||
| if metric["name"] == "checkout.failed" and metric["attributes"].get("tenant") == "acme": | ||
| return None | ||
|  | ||
| return metric | ||
|  | ||
| sentry_sdk.init( | ||
| dsn="___PUBLIC_DSN___", | ||
| _experiments={ | ||
| "enable_metrics": True, | ||
| "before_send_metric": before_metric, | ||
| }, | ||
| ) | ||
| ``` | ||
|  | ||
| The `before_send_metric` function receives a metric object, and should return the metric object if you want it to be sent to Sentry, or `None` if you want to discard it. | ||
|  | ||
| The metric dict has the following keys: | ||
|  | ||
| - `name`: (`str`) The name of the metric. | ||
| - `type`: (`str` - one of `counter`, `gauge`, `distribution`) The type of metric. | ||
| - `value`: (`float`) The numeric value of the metric. | ||
| - `unit`: (`int`) The unit of measurement for the metric value. | ||
| - `attributes`: (`dict[str, str | bool | float | int]`) Additional attributes to be sent with the metric. | ||
| - `timestamp`: (`float`) Timestamp in seconds (epoch time) indicating when the metric was recorded. | ||
| - `trace_id`: (`Optional[str]`) The trace ID of the trace this metric belongs to. | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Metrics for Python are supported in Sentry Python SDK version `2.43.0` and above. | ||
|  | ||
| ```bash {tabTitle:pip} | ||
| pip install "sentry-sdk" | ||
| ``` | ||
|  | ||
| ```bash {tabTitle:uv} | ||
| uv add "sentry-sdk" | ||
| ``` | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| To enable metrics, you need to initialize the SDK with the `enable_metrics` option set to `True`. | ||
|  | ||
| ```python | ||
| sentry_sdk.init( | ||
| dsn="___PUBLIC_DSN___", | ||
| _experiments={ | ||
| enable_metrics: True, | ||
| }, | ||
| ) | ||
| ``` | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| Once the feature is enabled on the SDK and the SDK is initialized, you can send metrics using the `sentry_sdk.metrics` APIs. | ||
|  | ||
| The `metrics` namespace exposes three methods that you can use to capture different types of metric information: `count`, `gauge`, and `distribution`. | ||
|  | ||
| ```python | ||
| from sentry_sdk import metrics | ||
|  | ||
| metrics.count("checkout.failed", 1) | ||
| metrics.gauge("queue.depth", 42) | ||
| metrics.distribution("cart.amount_usd", 187.5) | ||
| ``` | ||
|  | ||
| You can also pass additional attributes directly to `count`, `gauge`, and `distribution` via the `attributes` kwarg. | ||
|  | ||
| ```python | ||
| from sentry_sdk import metrics | ||
|  | ||
| metrics.count( | ||
| "checkout.failed", | ||
| 1, | ||
| attributes={ | ||
| "route": "/checkout", | ||
| "tenant": "acme", | ||
| "provider": "stripe", | ||
| }, | ||
| ) | ||
| ``` | 
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.
We should have a notice here that is still in beta, maybe tell folks how to apply for the open beta.
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.
Good idea, added!