Skip to content

Commit 9b542e5

Browse files
cleptricLms24
authored andcommitted
docs(sdks): New Span API
1 parent 9a931fb commit 9b542e5

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: Span API
3+
---
4+
5+
<Alert level="info">
6+
This document uses key words such as "MUST", "SHOULD", and "MAY" as defined in [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt) to indicate requirement levels.
7+
</Alert>
8+
9+
Spans are measuring the duration of certain operations in an application.
10+
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.
11+
12+
## Creating a root span
13+
14+
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.
15+
16+
```js
17+
span = sentry.tracing.startSpan()
18+
->setName('GET /')
19+
->setOp('http.server')
20+
21+
span.end()
22+
```
23+
24+
## Creating nested spans
25+
26+
To create nested spans, the SDK must expose an explicit way for a user to perform this task.
27+
28+
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.
29+
30+
```js
31+
childSpan = span.startChild()
32+
->setName('authentication middleware')
33+
->setOp('middleware.handle')
34+
35+
childSpan.end()
36+
```
37+
38+
## Setting the span status
39+
40+
A span has two statuses, `ok` and `error`. By default, the status of a span is set to `ok`.
41+
The SDK must allow a user to modify the status of a span.
42+
43+
```js
44+
span.setStatus('error')
45+
```
46+
47+
## Setting span attributes
48+
49+
The SDK must expose a method to allow a user to set data attributes onto a span.
50+
These attributes should use pre-defined keys whenever possible.
51+
52+
```js
53+
span.setAttribute(SpanAttributes.HTTP_METHOD, 'GET')
54+
span.setAttribute(SpanAttributes.HTTP_RESPONSE_STATUS_CODE, 200)
55+
```
56+
57+
## Additional, optional span APIs
58+
59+
`span.setStartTimestamp()` - overwrite the span's start time
60+
61+
`span.setEndTimestamp()` - overwrites the span's end time
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Span Trace Propagation
3+
---
4+
5+
<Alert level="info">
6+
This document uses key words such as "MUST", "SHOULD", and "MAY" as defined in [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt) to indicate requirement levels.
7+
</Alert>
8+
9+
## Continue an incoming trace
10+
11+
To continue a trace from an upstream service, the SDK must expose a method to extract the traceparent and baggage information and apply these to the applicable scope.
12+
13+
```js
14+
scope.setPropagationContext({
15+
traceparent: request.headers.SENTRY_TRACE,
16+
baggage: request.headers.SENTRY_BAGGAGE,
17+
})
18+
```
19+
Newly created root spans should now contain these properties, such as `trace_id` and `parent_span_id`.
20+
21+
## Continue an outgoing trace
22+
23+
To propagate a trace to a downstream service, the SDK must expose methods to fetch the required information to allow the next service to continue the trace.
24+
25+
```js
26+
traceparent = scope.getTraceparent()
27+
baggage = scope.getBaggage()
28+
```

0 commit comments

Comments
 (0)