Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions develop-docs/sdk/telemetry/spans/span-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SDKs MUST NOT expose names like "segment span" (e.g. in APIs) to users and SHOUL

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

```ts
```ts {tabTitle:TypeScript}
interface Span {
private _spanId: string;

Expand All @@ -49,6 +49,17 @@ interface Span {
}
```

```Python {tabTitle:Python}
class Span:
span_id: str

def set_attribute(self, key: str, value: SpanAttributeValue) -> None: ...
def set_status(self, status: Literal["ok", "error"]) -> None: ...
def get_name(self) -> str: ...
def set_name(self, name: str) -> None: ...
def get_attributes(self) -> SpanAttributes: ...
```

When implementing the span interface, consider the following guidelines:

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

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

```ts
```ts {tabTitle:TypeScript}
function startSpan(options: StartSpanOptions): Span;

interface StartSpanOptions {
Expand All @@ -74,6 +85,16 @@ interface StartSpanOptions {
}
```

```Python {tabTitle:Python}
with start_span(
name, # type: str
attributes, # type: SpanAttributes
parent_span, # type: Span
active, # type: bool
) as span:
...
```

SDKs MUST allow specifying the following options to be passed to `startSpan`:

| Option | Required | Description |
Expand Down Expand Up @@ -113,7 +134,7 @@ SDKs MAY expose additional utility APIs for users, or internal usage to access c

## Example

```ts
```ts {tabTitle:TypeScript}

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

Expand All @@ -140,4 +161,23 @@ unrelatedSpan.end();
on('checkout-finished', ({ timestamp }) => {
checkoutSpan.end(timestamp);
})
```
```

```Python {tabTitle:Python}

with sentry_sdk.start_span(name="checkout", attributes={"user.id": "123"}) as checkout_span:
Copy link
Member

@Lms24 Lms24 Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Can sentry_sdk.start_span be used without a with statement?

If so, could we write down an example for this? I think it's fully okay to support with (or callbacks or whatever the platform prefers) but ideally we can also document the most simple case which is that start_span returns an active span which users end manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ty, added in 33bad41.

with sentry_sdk.start_span(name="process-order") as process_order_span:
result = process()
process_order_span.set_attribute("order-status", result.message)

with sentry_sdk.start_span(name="process-payment") as process_payment_span:
try:
result = process_payment()
process_payment_span.set_attribute("order-status", result.message)
except:
process_payment_span.set_status("error")
process_payment_span.set_attribute("order-status", "error")

with sentry_sdk.start_span(name="log-order", parent_span=None) as unrelated_span:
log_order()
```
Loading