Skip to content

Commit 8dbab0f

Browse files
feat(python): Illustrate types of metrics (#15556)
Improve Python metrics documentation by: - adding illustrative use cases to the introduction; and - explaining the `before_send_metric()` callback; and - adding individual examples for each metric type.
1 parent 2e21bb5 commit 8dbab0f

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

docs/platforms/python/metrics/index.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ This feature is currently in open beta. Please reach out on [GitHub](https://git
1111

1212
</Alert>
1313

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.
14+
Sentry metrics help you pinpoint and solve issues that impact user experience and app performance by measuring the data points that are important to you. You can track things like processing time, event size, user signups, and conversion rates, then correlate them back to tracing data in order to get deeper insights and solve issues faster.
15+
16+
Once in Sentry, these metrics can be viewed alongside relevant errors, and searched using their individual attributes.
1517

1618
## Requirements
1719

platform-includes/metrics/options/python.mdx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#### before_send_metric
22

3-
To filter metrics, or update them before they are sent to Sentry, you can use the `before_send_metric` option.
3+
To filter metrics, or update them before they are sent to Sentry, you can use the `before_send_metric` option. If the callback returns `None`, the metric is not emitted. Attributes can also be updated in the callback function.
44

55
```python
66
import sentry_sdk
77
from sentry_sdk.types import Metric, Hint
88
from typing import Optional
99

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":
10+
def before_metric(metric: Metric, hint: Hint) -> Optional[Metric]:
11+
if metric["name"] == "removed-metric":
1312
return None
1413

14+
metric["attributes"]["extra"] = "foo"
15+
16+
if "browser" in metric["attributes"]:
17+
del metric["attributes"]["browser"]
18+
1519
return metric
1620

1721
sentry_sdk.init(

platform-includes/metrics/usage/python.mdx

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,62 @@ Once the feature is enabled on the SDK and the SDK is initialized, you can send
22

33
The `metrics` namespace exposes three methods that you can use to capture different types of metric information: `count`, `gauge`, and `distribution`.
44

5+
## Emit a Counter
6+
7+
Counters are one of the more basic types of metrics and can be used to count certain event occurrences.
8+
9+
To emit a counter, do the following:
10+
511
```python
6-
from sentry_sdk import metrics
12+
import sentry_sdk
713

8-
metrics.count("checkout.failed", 1)
9-
metrics.gauge("queue.depth", 42)
10-
metrics.distribution("cart.amount_usd", 187.5)
14+
# Record five total button clicks
15+
sentry_sdk.metrics.count(
16+
"button_click",
17+
5,
18+
attributes={
19+
"browser": "Firefox",
20+
"app_version": "1.0.0"
21+
},
22+
)
1123
```
1224

13-
You can also pass additional attributes directly to `count`, `gauge`, and `distribution` via the `attributes` kwarg.
25+
## Emit a Distribution
26+
27+
Distributions help you get the most insights from your data by allowing you to obtain aggregations such as `p90`, `min`, `max`, and `avg`.
28+
29+
To emit a distribution, do the following:
1430

1531
```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-
},
32+
import sentry_sdk
33+
34+
# Add '15.0' to a distribution used for tracking the loading times per page.
35+
sentry_sdk.metrics.distribution(
36+
"page_load",
37+
15.0,
38+
unit="millisecond",
39+
attributes={
40+
"page": "/home"
41+
},
42+
)
43+
```
44+
45+
## Emit a Gauge
46+
47+
Gauges let you obtain aggregates like `min`, `max`, `avg`, `sum`, and `count`. They can be represented in a more space-efficient way than distributions, but they can't be used to get percentiles. If percentiles aren't important to you, we recommend using gauges.
48+
49+
To emit a gauge, do the following:
50+
51+
```python
52+
import sentry_sdk
53+
54+
# Add '15.0' to a gauge used for tracking the loading times for a page.
55+
sentry_sdk.metrics.gauge(
56+
"page_load",
57+
15.0,
58+
unit="millisecond",
59+
attributes={
60+
"page": "/home"
61+
},
2662
)
2763
```

0 commit comments

Comments
 (0)