Skip to content

Commit b635fca

Browse files
mydeasentrivana
authored andcommitted
feat(js): Add docs for creating a child span (#10729)
1 parent ab5315e commit b635fca

File tree

1 file changed

+33
-8
lines changed
  • docs/platforms/javascript/common/tracing/instrumentation/custom-instrumentation

1 file changed

+33
-8
lines changed

docs/platforms/javascript/common/tracing/instrumentation/custom-instrumentation/index.mdx

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ This will revert to use the full hierarchy behavior, where spans are children of
7272

7373
The following options can be used for all span starting functions:
7474

75-
| Option | Type | Description |
76-
| ------------------ | --------------------------- | ------------------------------------------------------------------- |
77-
| `name` | `string` | The name of the span. |
78-
| `op` | `string` | The operation of the span. |
79-
| `startTime` | `number` | The start time of the span. |
80-
| `attributes` | `Record<string, Primitive>` | Attributes to attach to the span. |
81-
| `onlyIfParent` | `boolean` | If true, ignore the span if there is no active parent span. |
82-
| `forceTransaction` | `boolean` | If true, ensure this span shows up as transaction in the Sentry UI. |
75+
| Option | Type | Description |
76+
| ------------------ | --------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
77+
| `name` | `string` | The name of the span. |
78+
| `op` | `string` | The operation of the span. |
79+
| `startTime` | `number` | The start time of the span. |
80+
| `attributes` | `Record<string, Primitive>` | Attributes to attach to the span. |
81+
| `parentSpan` | `Span` | If set, make the span a child of the specified span. Otherwise, the span will be a child of the currently active span. |
82+
| `onlyIfParent` | `boolean` | If true, ignore the span if there is no active parent span. |
83+
| `forceTransaction` | `boolean` | If true, ensure this span shows up as transaction in the Sentry UI. |
8384

8485
Only `name` is required, all other options are optional.
8586

@@ -101,6 +102,20 @@ To add spans that aren't active, you can create independent spans. This is usefu
101102

102103
<PlatformContent includePath="performance/start-inactive-span" />
103104

105+
## Starting Spans as Children of a Specific Span
106+
107+
By default, any span that is started will be the child of the currently active span. If you want to have a different behavior, you can force spans to be the children of a specific span with the `parentSpan` option:
108+
109+
```js
110+
const parentSpan = Sentry.startInactiveSpan({ name: "Parent Span" });
111+
const childSpan = Sentry.startInactiveSpan({ name: "Child Span", parentSpan });
112+
113+
childSpan.end();
114+
parentSpan.end();
115+
```
116+
117+
This option is also available for `startSpan` and `startSpanManual`.
118+
104119
## Utilities to work with Spans
105120

106121
We expose some helpful utilities that can help you with custom instrumentation.
@@ -148,6 +163,16 @@ Sentry.withActiveSpan(null, () => {
148163
});
149164
```
150165

166+
Alternatively you can also use the `parentSpan` option to achieve the same:
167+
168+
```javascript
169+
const span = Sentry.startInactiveSpan({ name: "Parent Span" });
170+
const childSpan = Sentry.startInactiveSpan({
171+
name: "Child Span",
172+
parentSpan: span,
173+
});
174+
```
175+
151176
### `suppressTracing`
152177

153178
Suppress the creation of sampled spans for the duration of the callback. This is useful when you want to prevent certain spans from being captured. For example, if you do not want to create spans for a given fetch request, you can do:

0 commit comments

Comments
 (0)