Skip to content

Commit afb2fe7

Browse files
committed
feat(python): Add 3.x version of span metrics page
1 parent 58a5177 commit afb2fe7

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Sending Span Metrics
3+
description: "Learn how to add attributes to spans in Sentry to monitor performance and debug applications "
4+
sidebar_order: 20
5+
---
6+
7+
<Alert>
8+
9+
To use span metrics, you must first <PlatformLink to="/tracing/">configure tracing</PlatformLink> in your application.
10+
11+
</Alert>
12+
13+
Span metrics allow you to extend the default metrics that are collected by tracing and track custom performance data and debugging information within your application's traces. There are two main approaches to instrumenting metrics:
14+
15+
1. [Adding metrics to existing spans](#adding-metrics-to-existing-spans)
16+
2. [Creating dedicated spans with custom metrics](#creating-dedicated-metric-spans)
17+
18+
## Adding Metrics to Existing Spans
19+
20+
You can enhance existing spans with custom metrics by adding data. This is useful when you want to augment automatic instrumentation or add contextual data to spans you've already created.
21+
22+
```python
23+
span = sentry_sdk.get_current_span()
24+
if span:
25+
# Add individual metrics
26+
span.set_attribute("database.rows_affected", 42)
27+
span.set_attribute("cache.hit_rate", 0.85)
28+
span.set_attribute("memory.heap_used", 1024000)
29+
span.set_attribute("queue.length", 15)
30+
span.set_attribute("processing.duration_ms", 127)
31+
```
32+
33+
### Best Practices for Span Data
34+
35+
When adding metrics as span data:
36+
37+
- Use consistent naming conventions (for example, `category.metric_name`)
38+
- Keep attribute names concise but descriptive
39+
- Use appropriate data types (string, number, boolean, or an array containing only one of these types)
40+
41+
## Creating Dedicated Metric Spans
42+
43+
For more detailed operations, tasks, or process tracking, you can create custom dedicated spans that focus on specific metrics or attributes that you want to track. This approach provides better discoverability and more precise span configurations, however it can also create more noise in your trace waterfall.
44+
45+
```python
46+
with sentry_sdk.start_span(
47+
op="db.metrics",
48+
name="Database Query Metrics"
49+
) as span:
50+
# Set metrics after creating the span
51+
span.set_attribute("db.query_type", "SELECT")
52+
span.set_attribute("db.table", "users")
53+
span.set_attribute("db.execution_time_ms", 45)
54+
span.set_attribute("db.rows_returned", 100)
55+
span.set_attribute("db.connection_pool_size", 5)
56+
# Your database operation here
57+
pass
58+
```
59+
60+
For detailed examples of how to implement span metrics in common scenarios, see our <PlatformLink to="/tracing/span-metrics/examples/">Span Metrics Examples</PlatformLink> guide.
61+
62+
## Adding Metrics to All Spans
63+
64+
To consistently add metrics across all spans in your application, you can use the `before_send_transaction` callback:
65+
66+
```python
67+
import sentry_sdk
68+
from sentry_sdk.types import Event, Hint
69+
70+
def before_send_transaction(event: Event, hint: Hint) -> Event | None:
71+
# Add metrics to the root span
72+
if "trace" in event.get("contexts", {}):
73+
if "data" not in event["contexts"]["trace"]:
74+
event["contexts"]["trace"]["data"] = {}
75+
76+
event["contexts"]["trace"]["data"].update({
77+
"app.version": "1.2.3",
78+
"environment.region": "us-west-2"
79+
})
80+
81+
# Add metrics to all child spans
82+
for span in event.get("spans", []):
83+
if "data" not in span:
84+
span["data"] = {}
85+
86+
span["data"].update({
87+
"app.component_version": "2.0.0",
88+
"app.deployment_stage": "production"
89+
})
90+
91+
return event
92+
93+
sentry_sdk.init(
94+
# ...
95+
before_send_transaction=before_send_transaction
96+
)
97+
```
98+
99+
For detailed examples of how to implement span metrics in common scenarios, see our <PlatformLink to="/tracing/span-metrics/examples/">Span Metrics Examples</PlatformLink> guide.

0 commit comments

Comments
 (0)