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}

0 commit comments

Comments
 (0)