Skip to content

Commit dba65dc

Browse files
authored
feat(js): Document new performance APIs (#7707)
1 parent ed088bb commit dba65dc

File tree

25 files changed

+218
-4
lines changed

25 files changed

+218
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
You can use the `Sentry.startActiveSpan` method to wrap a callback in a span to measure how long it will take. The span is automatically finished when the callback is finished. This will work with both sync and async callbacks.
2+
3+
```javascript
4+
const result = Sentry.startActiveSpan({ name: "Important Function" }, () => {
5+
return expensiveFunction();
6+
});
7+
8+
const result = await Sentry.startActiveSpan(
9+
{ name: "Important Function" },
10+
async () => {
11+
const res = Sentry.startActiveSpan({ name: "Child Span" }, () => {
12+
return expensiveFunction();
13+
});
14+
15+
return updateRes(res);
16+
}
17+
);
18+
19+
const result = Sentry.startActiveSpan(
20+
{ name: "Important Function" },
21+
(span) => {
22+
// Can access the span to add data or set specific status.
23+
// The span can be undefined if the span was not sampled or if performance monitoring is disabled.
24+
span?.setData("foo", "bar");
25+
return expensiveFunction();
26+
}
27+
);
28+
```
29+
30+
The span named `Important Function` will become the active span for the duration of the callback.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
You can use the `Sentry.startActiveSpan` method to wrap a callback in a span to measure how long it will take. The span is automatically finished when the callback is finished. This will work with both sync and async callbacks.
2+
3+
```javascript
4+
const result = Sentry.startActiveSpan({ name: "Important Function" }, () => {
5+
return expensiveFunction();
6+
});
7+
8+
const result = await Sentry.startActiveSpan(
9+
{ name: "Important Function" },
10+
async () => {
11+
const res = Sentry.startActiveSpan({ name: "Child Span" }, () => {
12+
return expensiveFunction();
13+
});
14+
15+
return updateRes(res);
16+
}
17+
);
18+
19+
const result = Sentry.startActiveSpan(
20+
{ name: "Important Function" },
21+
(span) => {
22+
// Can access the span to add data or set specific status.
23+
// The span can be undefined if the span was not sampled or if performance monitoring is disabled.
24+
span?.setData("foo", "bar");
25+
return expensiveFunction();
26+
}
27+
);
28+
```
29+
30+
The span named `Important Function` will become the active span for the duration of the callback.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
```javascript
2+
const span1 = Sentry.startSpan({ name: "first-step" });
3+
4+
firstStep();
5+
6+
const span2 = Sentry.startSpan({ name: "second-step" });
7+
8+
secondStep();
9+
10+
const span3 = Sentry.startSpan({ name: "third-step" });
11+
12+
thirdStep();
13+
14+
span1.finish();
15+
span2.finish();
16+
span3.finish();
17+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
```javascript
2+
const span1 = Sentry.startSpan({ name: "first-step" });
3+
4+
firstStep();
5+
6+
const span2 = Sentry.startSpan({ name: "second-step" });
7+
8+
secondStep();
9+
10+
const span3 = Sentry.startSpan({ name: "third-step" });
11+
12+
thirdStep();
13+
14+
span1.finish();
15+
span2.finish();
16+
span3.finish();
17+
```

src/platform-includes/performance/add-spans-example/javascript.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function shopCheckout() {
99
// If there's currently an unfinished transaction, it may be dropped
1010
Sentry.getCurrentHub().configureScope((scope) => scope.setSpan(transaction));
1111

12-
// Assume this function makes an xhr/fetch call
12+
// Assume this function makes a fetch call
1313
const result = validateShoppingCartOnServer();
1414

1515
const span = transaction.startChild({

src/platform-includes/performance/enable-manual-instrumentation/apple.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
```swift {tabTitle:Swift}
24
import Sentry;
35

src/platform-includes/performance/enable-manual-instrumentation/dart.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
The following example creates a transaction that contains an expensive operation (for example, `processOrderBatch`), and sends the result to Sentry:
24

35
```dart

src/platform-includes/performance/enable-manual-instrumentation/dotnet.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
```csharp
24
// Transaction can be started by providing, at minimum, the name and the operation
35
var transaction = SentrySdk.StartTransaction(

src/platform-includes/performance/enable-manual-instrumentation/go.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
The following example creates a transaction span to time runs of an expensive operation on items from a channel. Timing for each operation is sent to Sentry and grouped by transaction name:
24

35
```go

src/platform-includes/performance/enable-manual-instrumentation/java.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
The following example creates a transaction that contains an expensive operation (for example, `processOrderBatch`), and sends the result to Sentry:
24

35
```java {tabTitle:Java}

src/platform-includes/performance/enable-manual-instrumentation/java.spring-boot.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
## Capturing Bean Method Execution
24

35
Every Spring bean method execution can be turned into a transaction or a span.

src/platform-includes/performance/enable-manual-instrumentation/java.spring.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
## Capturing Bean Method Execution
24

35
Every Spring bean method execution can be turned into a transaction or a span.

src/platform-includes/performance/enable-manual-instrumentation/javascript.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
This is valid for all JavaScript SDKs (both backend and frontend) and works independently of the `Express`, `Http`, and `BrowserTracing` integrations.
24

35
```javascript

src/platform-includes/performance/enable-manual-instrumentation/native.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
```c
24
// Enable performance monitoring by setting a sample rate above 0
35
sentry_options_t *options = sentry_options_new();

src/platform-includes/performance/enable-manual-instrumentation/php.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
The following example creates a transaction for a scope that contains an expensive operation (for example, `expensive_operation`), and sends the result to Sentry:
24

35
```php

src/platform-includes/performance/enable-manual-instrumentation/python.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
The following example creates a transaction for a scope that contains an expensive operation (for example, `process_item`), and sends the result to Sentry:
24

35
```python

src/platform-includes/performance/enable-manual-instrumentation/ruby.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
The following example creates a transaction for a scope that contains an expensive operation (for example, `process_item`), and sends the result to Sentry:
24

35
```ruby

src/platform-includes/performance/enable-manual-instrumentation/rust.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
To instrument certain regions of your code, you can create transactions to capture them.
2+
13
```rust
24
// Transaction can be started by providing the name and the operation
35
let tx_ctx = sentry::TransactionContext::new(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
```JavaScript
2+
const span = Sentry.getActiveSpan();
3+
4+
span?.setData("key", "value");
5+
6+
// Start a child span that will be a child of the current span.
7+
// The child span will measure the time between span.startChild() and childSpan.finish().
8+
const childSpan = span?.startChild({ name: "Child Span" });
9+
10+
expensiveCalculation();
11+
12+
childSpan?.finish();
13+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
```JavaScript
2+
const span = Sentry.getActiveSpan();
3+
4+
span?.setData("key", "value");
5+
6+
// Start a child span that will be a child of the current span.
7+
// The child span will measure the time between span.startChild() and childSpan.finish().
8+
const childSpan = span?.startChild({ name: "Child Span" });
9+
10+
expensiveCalculation();
11+
12+
childSpan?.finish();
13+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Note>
2+
3+
The below span APIs (`startActiveSpan`, `startSpan`, and `getActiveSpan`) require SDK version `7.65.0` or higher. If you are using an older version of the SDK, you can use the [explicit transaction APIs](#start-transaction) for custom instrumentation.
4+
5+
</Note>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Note>
2+
3+
The below span APIs (`startActiveSpan`, `startSpan`, and `getActiveSpan`) require SDK version `7.65.0` or higher. If you are using an older version of the SDK, you can use the [explicit transaction APIs](#start-transaction) for custom instrumentation.
4+
5+
</Note>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
```JavaScript
2+
const result = Sentry.startActiveSpan({ name: 'GET /users', op: 'http.client' }, () => {
3+
return fetchUsers();
4+
})
5+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
```JavaScript
2+
const result = Sentry.startActiveSpan({ name: 'SELECT * FROM TABLE', op: 'db.query' }, () => {
3+
return execQuery();
4+
})
5+
```

src/platforms/common/performance/instrumentation/custom-instrumentation.mdx

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,71 @@ redirect_from:
3333

3434
<Note>
3535

36-
To capture transactions customized to your organization's needs, you must first <PlatformLink to="/performance/">set up performance monitoring.</PlatformLink>
36+
To capture transactions and spans customized to your organization's needs, you must first <PlatformLink to="/performance/">set up performance monitoring.</PlatformLink>
3737

3838
</Note>
3939

40-
To instrument certain regions of your code, you can create transactions to capture them.
40+
<PlatformSection notSupported={["javascript", "node"]}>
4141

4242
<PlatformContent includePath="performance/enable-manual-instrumentation" />
4343

4444
<PlatformContent includePath="performance/add-spans-example" />
4545

46+
</PlatformSection>
47+
48+
<PlatformSection supported={["javascript", "node"]}>
49+
50+
To add custom performance data to your application, you need to create and use spans. Spans are a way to measure the time it takes for a specific action to occur. For example, you can create a span to measure the time it takes for a function to execute.
51+
52+
To start measuring timing data, you first need to import the SDK.
53+
54+
<PlatformContent includePath="enriching-events/import" />
55+
56+
<PlatformContent includePath="performance/span-api-version" />
57+
58+
## Create Active Span
59+
60+
By default created spans are considered active, which means they are put on the Sentry scope. This allows child spans and Sentry errors to be associated with that span. This is the recommended way to create spans.
61+
62+
<PlatformContent includePath="performance/add-active-span" />
63+
64+
## Get Active Span
65+
66+
You can also get the current active span, which is useful for when you need to add new child spans.
67+
68+
<PlatformContent includePath="performance/get-span" />
69+
70+
## Start Independent Spans
71+
72+
If you want to add a span that is not active, you can create a independent spans. This is useful for when you have work that is grouped together under a single parent span, but is independent from the current active span. In most cases you'll want to create and use active spans.
73+
74+
<PlatformContent includePath="performance/add-independent-span" />
75+
76+
## Start Transaction
77+
78+
The root span (the span that is the parent of all other spans) is known as a transaction in Sentry. This can be accessed and created separately if you need more control over the timing data or if you use a version of the SDK that does not support the top level span APIs.
79+
80+
<PlatformContent includePath="performance/enable-manual-instrumentation" />
81+
82+
<PlatformContent includePath="performance/add-spans-example" />
83+
84+
## Adding Span operations
85+
86+
Spans can have an operation associated with them, which help activate Sentry identify additional context about the span. For example database related spans have the `db` span operation associated with them. The Sentry product offers additional controls, visualizations and filters for spans with known operations.
87+
88+
Sentry maintains a [list of well known span operations](https://develop.sentry.dev/sdk/performance/span-operations/#list-of-operations) and it is recommended that you use one of those operations if it is applicable to your span.
89+
90+
<PlatformContent includePath="performance/span-operations" />
91+
92+
</PlatformSection>
93+
4694
<PlatformSection supported={["java", "apple"]}>
4795

4896
<PlatformContent includePath="performance/create-transaction-bound-to-scope" />
4997

5098
</PlatformSection>
5199

52-
<PlatformSection notSupported={["java", "native", "apple"]}>
100+
<PlatformSection notSupported={["java", "native", "apple", "javascript", "node"]}>
53101

54102
<PlatformContent includePath="performance/retrieve-transaction" />
55103

0 commit comments

Comments
 (0)