|
| 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 |
0 commit comments