Skip to content

Commit 8bb3a08

Browse files
committed
Update Span API
1 parent 83a4125 commit 8bb3a08

File tree

1 file changed

+37
-146
lines changed

1 file changed

+37
-146
lines changed

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

Lines changed: 37 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -4,180 +4,71 @@ title: Span API
44

55
# Span API
66

7-
### `startSpan`
7+
Spans are measuring the duration of certain operations in an application.
8+
The topmost member of a span tree is called the root span. This span has no parent span and groups together its children with a representative name for the entire operation, such as `GET /` in case of a request to a backend application.
89

9-
Calling the global `startSpan` method always creates a root span.
10+
### Creating a root span
1011

11-
```jsx
12-
span = sentry.tracing.startSpan({
13-
name: 'GET /',
14-
op: 'http.server'
15-
})
16-
```
12+
The SDK must expose a method for creating a root span. The user must be able to set certain properties on this root span, such as its name, the type of operation (`op`) and others.
1713

18-
```jsx
19-
span = sentry().tracing().startSpan()
14+
```js
15+
span = sentry.tracing.startSpan()
2016
->setName('GET /')
2117
->setOp('http.server')
22-
```
23-
24-
### `end`
2518

26-
This method sets the end timestamp on the span. If the span is a root span, it emits the entire span tree to Sentry.
27-
28-
```jsx
29-
span = sentry.tracing.startSpan({
30-
name: 'GET /',
31-
op: 'http.server'
32-
})
33-
34-
span.end()
35-
```
36-
37-
```jsx
38-
span = sentry().tracing().startSpan()
39-
->setName('GET /')
40-
->setOp('http.server')
41-
4219
span.end()
4320
```
4421

45-
### `continueTraceFrom`
46-
47-
Creates a new root span which continues the trace based on the provided trace and parent span id.
48-
49-
```jsx
50-
span = sentry.tracing.startSpan({
51-
traceId: '',
52-
parentSpanId: '',
53-
sampled: true,
54-
name: 'GET /'
55-
op: 'http.server'
56-
})
57-
```
58-
59-
```jsx
60-
span = sentry.tracing.startSpan({
61-
continueTraceFrom: span,
62-
name: 'GET /'
63-
op: 'http.server'
64-
})
65-
```
66-
67-
```jsx
68-
span = sentry().tracing().continueTraceFrom({
69-
traceId: '',
70-
parentSpanId: '',
71-
sampled: true,
72-
})
73-
->setName('GET /')
74-
->setOp('http.server')
75-
```
76-
77-
```jsx
78-
span = sentry().tracing().continueTraceFrom(span)
79-
->setName('GET /')
80-
->setOp('http.server')
81-
```
22+
### Creating nested spans
8223

83-
### Start a new root span
24+
To create nested spans, the SDK must expose an explicit way for a user to perform this task.
8425

85-
Calling the global `startSpan` method always creates a root span.
26+
Additionally, the SDK may expose alternative APIs to create nested spans, such as allowing a user to wrap an operation into a callback or apply a decorator to certain blocks. These alternative APIs must never create a root span and no-op if no parent span is present.
8627

87-
```jsx
88-
span = sentry.tracing.startSpan({
89-
name: 'GET /',
90-
op: 'http.server',
91-
})
28+
```js
29+
childSpan = span.startChild()
30+
->setName('authentication middleware')
31+
->setOp('middleware.handle')
32+
33+
childSpan.end()
9234
```
9335

94-
```jsx
95-
span = sentry().tracing().startSpan()
96-
->setName('GET /')
97-
->setOp('http.server')
98-
```
36+
### Setting the span status
9937

100-
### `setAttribute`
101-
102-
```jsx
103-
span = sentry.tracing.startSpan({
104-
name: 'GET /',
105-
op: 'http.server',
106-
attributes: {
107-
attr1: "value1",
108-
attr2: 42,
109-
attr3: true,
110-
},
111-
})
112-
```
38+
A span has two statuses, `ok` and `error`. By default, the status of a span is set to `ok`.
39+
The SDK must allow a user to modify the status of a span.
11340

114-
```jsx
115-
span = sentry().tracing().startSpan()
116-
->setName('GET /')
117-
->setOp('http.server')
118-
->setAtttribute(
119-
key: 'attr1',
120-
value: 'value1',
121-
)
41+
```js
42+
span.setStatus('error')
12243
```
12344

124-
### Starting a child span
45+
### Setting span attributes
12546

126-
To create nested spans, you must call `startSpan` on the parent span.
47+
The SDK must expose a method to allow a user to set data attributes onto a span.
48+
These attributes should use pre-defined keys whenever possible.
12749

128-
```jsx
129-
span = sentry.tracing.startSpan({ ... })
130-
childSpan = span.startSpan({ ... })
50+
```js
51+
span.setAttribute(SpanAttributes.HTTP_METHOD, 'GET')
52+
span.setAttribute(SpanAttributes.HTTP_RESPONSE_STATUS_CODE, 200)
13153
```
13254

133-
```jsx
134-
span = sentry().tracing().startSpan()
135-
childSpan = span.startSpan()
136-
```
55+
### Receiving the trace parent
13756

138-
### Get the current span
57+
The SDK must expose a method to receive the baggage string.
13958

140-
```jsx
141-
span = scope.getSpan()
59+
```js
60+
traceparent = span.getTraceparent()
14261
```
14362

144-
### Set the current span
145-
146-
```jsx
147-
scope.setSpan(span)
148-
```
63+
### Receiving the baggage
14964

150-
### Get the root span
65+
The SDK must expose a method to receive the baggage string.
15166

152-
```jsx
153-
rootSpan = scope.getRootSpan()
67+
```js
68+
baggage = span.getBaggage()
15469
```
15570

156-
### Additional APIs
157-
158-
span.`setStartTimestamp()` - overwrite the span’s start time
159-
160-
span.`setEndTimestamp()` - overwrites the span’s end time
161-
162-
span.`setStatus()` - success, error, unknown
163-
164-
span.`getBaggage()` - returns the baggage string
165-
166-
span.`getTraceparent()` - returns the sentry trace parent string
71+
### Additional, optional span APIs
16772

168-
### Examples
169-
170-
```jsx
171-
rootSpan = sentry.tracing.startSpan({
172-
name: 'GET /',
173-
op: 'http.server',
174-
})
175-
176-
span = rootSpan.startSpan({
177-
name: 'middleware.handle',
178-
op: 'http.middleware',
179-
})
180-
181-
span.end()
182-
rootSpan.end()
183-
```
73+
`span.setStartTimestamp()` - overwrite the span's start time
74+
`span.setEndTimestamp()` - overwrites the span's end time

0 commit comments

Comments
 (0)