Skip to content

Commit 443744f

Browse files
authored
feat: Add Python examples to Spans v2 API page (#15650)
1 parent 20a32b2 commit 443744f

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

develop-docs/sdk/telemetry/spans/span-api.mdx

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ SDKs MUST NOT expose names like "segment span" (e.g. in APIs) to users and SHOUL
2828

2929
SDKs' span implementations MUST at minimum implement the following span interface.
3030

31-
```ts
31+
```ts {tabTitle:TypeScript}
3232
interface Span {
3333
private _spanId: string;
3434

@@ -49,6 +49,17 @@ interface Span {
4949
}
5050
```
5151

52+
```Python {tabTitle:Python}
53+
class Span:
54+
span_id: str
55+
56+
def set_attribute(self, key: str, value: SpanAttributeValue) -> None: ...
57+
def set_status(self, status: Literal["ok", "error"]) -> None: ...
58+
def get_name(self) -> str: ...
59+
def set_name(self, name: str) -> None: ...
60+
def get_attributes(self) -> SpanAttributes: ...
61+
```
62+
5263
When implementing the span interface, consider the following guidelines:
5364

5465
- SDKs MAY implement additional APIs, such as getters/setters for properties (e.g. `span.getStatus()`), or additional methods for convenience (e.g. `Span::spanContext()`).
@@ -63,7 +74,7 @@ SDKs MUST expose at least one API to start a span. SDKs MAY expose additional AP
6374

6475
SDKs MUST expose a default `startSpan` API that takes options and returns a span:
6576

66-
```ts
77+
```ts {tabTitle:TypeScript}
6778
function startSpan(options: StartSpanOptions): Span;
6879

6980
interface StartSpanOptions {
@@ -74,6 +85,25 @@ interface StartSpanOptions {
7485
}
7586
```
7687

88+
```Python {tabTitle:Python}
89+
# The context-manager way of starting a span. The span will be finished
90+
# automatically when exiting the context manager.
91+
92+
with start_span(
93+
name, # type: str
94+
attributes, # type: SpanAttributes
95+
parent_span, # type: Span
96+
active, # type: bool
97+
) as span:
98+
...
99+
100+
# Alternative API without the use of a context manager, to allow for more
101+
# flexibility when to end the span:
102+
103+
span = start_span(name, attributes, parent_span, active)
104+
span.end()
105+
```
106+
77107
SDKs MUST allow specifying the following options to be passed to `startSpan`:
78108

79109
| Option | Required | Description |
@@ -113,7 +143,7 @@ SDKs MAY expose additional utility APIs for users, or internal usage to access c
113143

114144
## Example
115145

116-
```ts
146+
```ts {tabTitle:TypeScript}
117147

118148
const checkoutSpan = Sentry.startSpan({ name: 'on-checkout-click', attributes: { 'user.id': '123' } })
119149

@@ -140,4 +170,24 @@ unrelatedSpan.end();
140170
on('checkout-finished', ({ timestamp }) => {
141171
checkoutSpan.end(timestamp);
142172
})
143-
```
173+
```
174+
175+
```Python {tabTitle:Python}
176+
177+
with sentry_sdk.start_span(name="checkout", attributes={"user.id": "123"}) as checkout_span:
178+
with sentry_sdk.start_span(name="process-order") as process_order_span:
179+
result = process()
180+
process_order_span.set_attribute("order-status", result.message)
181+
182+
with sentry_sdk.start_span(name="process-payment") as process_payment_span:
183+
try:
184+
result = process_payment()
185+
process_payment_span.set_attribute("order-status", result.message)
186+
except:
187+
process_payment_span.set_status("error")
188+
process_payment_span.set_attribute("order-status", "error")
189+
190+
span = sentry_sdk.start_span(name="log-order", parent_span=None)
191+
log_order()
192+
span.end()
193+
```

0 commit comments

Comments
 (0)