You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Add documentation for manual span creation (without context manager)
- Clean up code snippets
- Made all callout boxes the same color, to make page more calm.
---------
Co-authored-by: Ivana Kellyer <[email protected]>
Co-authored-by: Daniel Szoke <[email protected]>
Copy file name to clipboardExpand all lines: docs/platforms/python/tracing/instrumentation/custom-instrumentation/index.mdx
+49-23Lines changed: 49 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,11 +9,11 @@ The Sentry SDK for Python does a very good job of auto instrumenting your applic
9
9
10
10
Adding transactions will allow you to instrument and capture certain regions of your code.
11
11
12
-
<Note>
12
+
<Alertlevel="info">
13
13
14
14
If you're using one of Sentry's SDK integrations, transactions will be created for you automatically.
15
15
16
-
</Note>
16
+
</Alert>
17
17
18
18
The following example creates a transaction for an expensive operation (in this case, `eat_pizza`), and then sends the result to Sentry:
19
19
@@ -35,8 +35,9 @@ The [API reference](https://getsentry.github.io/sentry-python/api.html#sentry_sd
35
35
36
36
If you want to have more fine-grained performance monitoring, you can add child spans to your transaction, which can be done by either:
37
37
38
-
- Using a context manager or
39
-
- Using a decorator, (this works on sync and `async` functions)
38
+
- Using a context manager
39
+
- Using a decorator (this works on sync and async functions)
40
+
- Manually starting and finishing a span
40
41
41
42
Calling a `sentry_sdk.start_span()` will find the current active transaction and attach the span to it.
42
43
@@ -53,7 +54,6 @@ def eat_pizza(pizza):
53
54
while pizza.slices >0:
54
55
with sentry_sdk.start_span(description="Eat Slice"):
55
56
eat_slice(pizza.slices.pop())
56
-
57
57
```
58
58
59
59
### Using a Decorator
@@ -69,15 +69,32 @@ def eat_pizza(pizza):
69
69
with sentry_sdk.start_transaction(op="task", name="Eat Pizza"):
70
70
while pizza.slices >0:
71
71
eat_slice(pizza.slices.pop())
72
-
73
72
```
74
73
75
-
<Alerttitle="Static & Class Methods"level="warning">
74
+
<Alerttitle="Static & Class Methods"level="info">
76
75
77
76
When tracing a static or class method, you **must** add the `@sentry_sdk.trace` decorator **after** the `@staticmethod` or `@classmethod` decorator (i.e., **closer** to the function definition). Otherwise, your function will break!
78
77
79
78
</Alert>
80
79
80
+
### Manually Starting and Finishing a Span
81
+
82
+
```python
83
+
import sentry_sdk
84
+
85
+
defeat_slice(slice):
86
+
...
87
+
88
+
defeat_pizza(pizza):
89
+
with sentry_sdk.start_transaction(op="task", name="Eat Pizza"):
When you create your span manually, make sure to call `span.finish()` after the block of code you want to wrap in a span to finish the span. If you do not finish the span it will not be sent to Sentry.
97
+
81
98
## Nested Spans
82
99
83
100
Spans can be nested to form a span tree. If you'd like to learn more, read our [distributed tracing](/product/sentry-basics/tracing/distributed-tracing/) documentation.
@@ -90,16 +107,10 @@ import sentry_sdk
90
107
defchew():
91
108
...
92
109
93
-
defswallow():
94
-
...
95
-
96
110
defeat_slice(slice):
97
111
with sentry_sdk.start_span(description="Eat Slice"):
98
112
with sentry_sdk.start_span(description="Chew"):
99
113
chew()
100
-
with sentry_sdk.start_span(description="Swallow"):
The parameters of `start_span()` and `start_child()` are the same. See the [API reference](https://getsentry.github.io/sentry-python/api.html#sentry_sdk.api.start_span) for more details.
149
+
150
+
When you create your span manually, make sure to call `span.finish()` after the block of code you want to wrap in a span to finish the span. If you do not finish the span it will not be sent to Sentry.
151
+
124
152
## Define Span Creation in a Central Place
125
153
126
154
To avoid having custom performance instrumentation code scattered all over your code base, pass a parameter <PlatformIdentifiername="functions-to-trace" /> to your `sentry_sdk.init()` call.
@@ -145,21 +173,21 @@ sentry_sdk.init(
145
173
146
174
Now, whenever a function specified in `functions_to_trace` will be executed, a span will be created and attached as a child to the currently running span.
147
175
148
-
<Alertlevel="warning"title="Important">
176
+
<Alerttitle="Important"level="info">
149
177
150
178
To enable performance monitoring for the functions specified in `functions_to_trace`, the SDK needs to load the function modules. Be aware, there may be code being executed in modules during module loading. To avoid this, use the method described above to trace your functions.
151
179
152
180
</Alert>
153
181
154
182
## Accessing the Current Transaction
155
183
156
-
To change data in an already ongoing transaction, use `Hub.current.scope.transaction`. This property will return a transaction if there's one running, otherwise it will return `None`.
184
+
The `sentry_sdk.get_current_scope().transaction` property returns the active transaction or `None` if no transaction is active. You can use this property to modify data on the transaction.
To change data in the current span, use `sentry_sdk.Hub.current.scope.span`. This property will return a span if there's one running, otherwise it will return `None`.
201
+
To change data in the current span, use `sentry_sdk.get_current_span()`. This function will return a span if there's one running, otherwise it will return `None`.
174
202
175
203
In this example, we'll set a tag in the span created by the `@sentry_sdk.trace` decorator.
0 commit comments