Skip to content

Commit 2eaf71a

Browse files
feat(python): Document metrics
1 parent bf1f6ee commit 2eaf71a

File tree

9 files changed

+132
-0
lines changed

9 files changed

+132
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Set Up Metrics
3+
sidebar_title: Metrics
4+
description: "Metrics allow you to send, view and query counts and measurements sent from your applications within Sentry."
5+
sidebar_order: 5755
6+
---
7+
8+
With Sentry Metrics, you can send numeric counts and measurements from your applications to Sentry. Once in Sentry, these metrics can be viewed alongside relevant errors.
9+
10+
## Requirements
11+
12+
<PlatformContent includePath="metrics/requirements" />
13+
14+
## Setup
15+
16+
<PlatformContent includePath="metrics/setup" />
17+
18+
## Usage
19+
20+
<PlatformContent includePath="metrics/usage" />
21+
22+
## Options
23+
24+
<PlatformContent includePath="metrics/options" />
25+
26+
## Default Attributes
27+
28+
<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="logs/default-attributes/core" />
4+
5+
<Include name="logs/default-attributes/server" />
6+
7+
<Include name="logs/default-attributes/user" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#### before_send_log
2+
3+
To filter logs, 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 info level logs
12+
if log["severity_text"] == "info":
13+
return None
14+
return log
15+
16+
sentry_sdk.init(
17+
dsn="___PUBLIC_DSN___",
18+
_experiments={
19+
"enable_metrics": True,
20+
"before_send_metric": _before_metric,
21+
},
22+
)
23+
```
24+
25+
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.
26+
27+
The metric dict has the following keys:
28+
29+
- `name`: (`str`) The name of the metric.
30+
- `type`: (`str` - one of `counter`, `gauge`, `distribution`) The type of metric.
31+
- `value`: (`float`) The numeric value of the metric.
32+
- `unit`: (`int`) The unit of measurement for the metric value.
33+
- `attributes`: (`dict[str, str | bool | float | int]`) Additional attributes to be sent with the log.
34+
- `timestamp`: (`float`) Timestamp in seconds (epoch time) indicating when the metric was recorded.
35+
- `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 `-.-.-` 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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("test.counter", 1)
9+
metrics.gauge("test.gauge", 42)
10+
metrics.distribution("test.distribution", 200)
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+
"test.counter",
20+
1,
21+
attributes={
22+
"endpoint": "/api/test",
23+
"status": "success"
24+
},
25+
)
26+
```

0 commit comments

Comments
 (0)