Skip to content

Commit ffb1d62

Browse files
feat(python): Document metrics (#15299)
Document trace-connected metrics use in the Python SDK.
1 parent 2f5ca7c commit ffb1d62

File tree

10 files changed

+140
-4
lines changed

10 files changed

+140
-4
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: Set Up Metrics
3+
sidebar_title: Metrics
4+
description: "Metrics allow you to send, view and query counters, gauges and measurements sent from your applications within Sentry."
5+
sidebar_order: 5755
6+
---
7+
8+
<Alert>
9+
10+
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.
11+
12+
</Alert>
13+
14+
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.
15+
16+
## Requirements
17+
18+
<PlatformContent includePath="metrics/requirements" />
19+
20+
## Setup
21+
22+
<PlatformContent includePath="metrics/setup" />
23+
24+
## Usage
25+
26+
<PlatformContent includePath="metrics/usage" />
27+
28+
## Options
29+
30+
<PlatformContent includePath="metrics/options" />
31+
32+
## Default Attributes
33+
34+
<PlatformContent includePath="metrics/default-attributes" />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Core Attributes
2+
3+
- `environment`: The environment set in the SDK if defined. This is sent from the SDK as `sentry.environment`.
4+
- `release`: The release set in the SDK if defined. This is sent from the SDK as `sentry.release`.
5+
- `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`.
6+
- `sdk.name`: The name of the SDK that sent the metric. This is sent from the SDK as `sentry.sdk.name`.
7+
- `sdk.version`: The version of the SDK that sent the metric. This is sent from the SDK as `sentry.sdk.version`.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Server Attributes
2+
3+
- `server.address`: The address of the server that sent the metric. Equivalent to `server_name` that gets attached to Sentry errors.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### User Attributes
2+
3+
If user information is available in the current scope, the following attributes are added to the metric:
4+
5+
- `user.id`: The user ID.
6+
- `user.name`: The username.
7+
- `user.email`: The email address.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
The Python SDK automatically sets several default attributes on all metrics to provide context and improve debugging:
2+
3+
<Include name="metrics/default-attributes/core" />
4+
5+
<Include name="metrics/default-attributes/server" />
6+
7+
<Include name="metrics/default-attributes/user" />
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#### before_send_metric
2+
3+
To filter metrics, or update them before they are sent to Sentry, you can use the `before_send_metric` option.
4+
5+
```python
6+
import sentry_sdk
7+
from sentry_sdk.types import Metric, Hint
8+
from typing import Optional
9+
10+
def before_metric(metric: Metric, _hint: Hint) -> Optional[Metric]:
11+
# Filter out all failed checkouts on the acme tenant
12+
if metric["name"] == "checkout.failed" and metric["attributes"].get("tenant") == "acme":
13+
return None
14+
15+
return metric
16+
17+
sentry_sdk.init(
18+
dsn="___PUBLIC_DSN___",
19+
_experiments={
20+
"enable_metrics": True,
21+
"before_send_metric": before_metric,
22+
},
23+
)
24+
```
25+
26+
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.
27+
28+
The metric dict has the following keys:
29+
30+
- `name`: (`str`) The name of the metric.
31+
- `type`: (`str` - one of `counter`, `gauge`, `distribution`) The type of metric.
32+
- `value`: (`float`) The numeric value of the metric.
33+
- `unit`: (`int`) The unit of measurement for the metric value.
34+
- `attributes`: (`dict[str, str | bool | float | int]`) Additional attributes to be sent with the metric.
35+
- `timestamp`: (`float`) Timestamp in seconds (epoch time) indicating when the metric was recorded.
36+
- `trace_id`: (`Optional[str]`) The trace ID of the trace this metric belongs to.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Metrics for Python are supported in Sentry Python SDK version `2.43.0` and above.
2+
3+
```bash {tabTitle:pip}
4+
pip install "sentry-sdk"
5+
```
6+
7+
```bash {tabTitle:uv}
8+
uv add "sentry-sdk"
9+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
To enable metrics, you need to initialize the SDK with the `enable_metrics` option set to `True`.
2+
3+
```python
4+
sentry_sdk.init(
5+
dsn="___PUBLIC_DSN___",
6+
_experiments={
7+
enable_metrics: True,
8+
},
9+
)
10+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Once the feature is enabled on the SDK and the SDK is initialized, you can send metrics using the `sentry_sdk.metrics` APIs.
2+
3+
The `metrics` namespace exposes three methods that you can use to capture different types of metric information: `count`, `gauge`, and `distribution`.
4+
5+
```python
6+
from sentry_sdk import metrics
7+
8+
metrics.count("checkout.failed", 1)
9+
metrics.gauge("queue.depth", 42)
10+
metrics.distribution("cart.amount_usd", 187.5)
11+
```
12+
13+
You can also pass additional attributes directly to `count`, `gauge`, and `distribution` via the `attributes` kwarg.
14+
15+
```python
16+
from sentry_sdk import metrics
17+
18+
metrics.count(
19+
"checkout.failed",
20+
1,
21+
attributes={
22+
"route": "/checkout",
23+
"tenant": "acme",
24+
"provider": "stripe",
25+
},
26+
)
27+
```

src/middleware.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,10 +1766,6 @@ const USER_DOCS_REDIRECTS: Redirect[] = [
17661766
to: '/platforms/javascript/guides/:guide/opentelemetry/',
17671767
},
17681768
// START redirecting deprecated generic metrics docs to concepts
1769-
{
1770-
from: '/platforms/python/metrics/',
1771-
to: '/platforms/python/tracing/span-metrics/',
1772-
},
17731769
{
17741770
from: '/platforms/ruby/metrics/',
17751771
to: '/concepts/key-terms/tracing/span-metrics/',

0 commit comments

Comments
 (0)