Skip to content

Commit c852bc2

Browse files
committed
feat(native): transactions as automatic trace boundaries
1 parent ff34007 commit c852bc2

File tree

1 file changed

+45
-6
lines changed
  • platform-includes/performance/add-spans-example

1 file changed

+45
-6
lines changed

platform-includes/performance/add-spans-example/native.mdx

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,65 @@ void perform_checkout() {
3838

3939
This example will send a transaction named `checkout` to Sentry. The transaction will contain a `validation` span that measures how long `validate_shopping_cart()` took and a `process` span that measures `process_shopping_cart()`. Finally, the call to `sentry_transaction_finish()` will finish the transaction and send it to Sentry.
4040

41-
## Trace Boundaries
42-
By default, events, transactions, and spans will inherit the `trace_id` from the `propagation_context` as generated during SDK initialization. These values can be overridden by a downstream SDK that calls `sentry_set_trace(trace_id, parent_span_id)`, or by a user who can manually create a trace boundary using `sentry_generate_trace()`:
41+
## Automatic Trace Boundaries
4342

44-
```c
43+
By default, events, transactions, and spans will inherit the `trace_id` from the `propagation_context` as generated during SDK initialization.
44+
45+
Transactions act as automatic trace boundaries, meaning whenever you create a transaction, it will start a new trace, which the SDK will apply as soon as you scope it. Once you finish the transaction, the SDK will move the trace to the `propagation_context`, from which the trace will affect any event until you scope a new transaction:
46+
47+
```C
4548
// Upon init, generates a random trace_id and span_id in the propagation_context
4649
sentry_options_t *options = sentry_options_new();
4750
sentry_init(options);
48-
// Events, transactions and spans will inherit the trace data from this propagation_context
49-
initialize_store();
51+
52+
// Events, transactions, and spans will inherit the trace data from this propagation_context
53+
54+
// the transaction inside perform_checkout() will lead to a new trace in the propgation_context
55+
perform_checkout();
56+
57+
// After perform_checkout() events will inherit the trace created in perform_checkout() from the propagation_context
58+
```
59+
60+
## Manual Trace Boundaries
61+
62+
The SDK will turn off managing automatic trace boundaries via transactions once manual management of trace boundaries
63+
was requested by either a downstream SDK (using `sentry_set_trace(trace_id, parent_span_id)`) or a direct user of the
64+
Native SDK (via `sentry_regenerate_trace()`):
65+
66+
```C
67+
sentry_options_t *options = sentry_options_new();
68+
sentry_init(options);
5069
5170
// trace_id and parent_span_id usually originate from a downstream SDK
5271
// (which should be the one calling `sentry_set_trace`)
5372
const char *trace_id = "2674eb52d5874b13b560236d6c79ce8a";
5473
const char *parent_span_id = "a0f9fdf04f1a63df";
5574
56-
// Set the trace propagation_context with the given data
75+
// Set the trace propagation_context with the given data.
76+
// Downstream SDKs should do this as early as possible.
5777
sentry_set_trace(trace_id, parent_span_id);
78+
79+
// Events, transactions and spans inside authenticate_user() will be part
80+
// of a different trace than the one created during initialization.
5881
authenticate_user();
82+
```
83+
84+
```C
85+
sentry_options_t *options = sentry_options_new();
86+
sentry_init(options);
5987

6088
// Generates a new random trace and span id onto the propagation_context
6189
sentry_regenerate_trace();
90+
authenticate_user();
91+
92+
sentry_regenerate_trace();
93+
// Events, transactions, and spans inside perform_checkout() will be part of
94+
// a different trace than the ones inside authenticate_user()
6295
perform_checkout();
6396
```
97+
98+
After the client called either function, the following transactions inherit that trace from the `propagation_context` and no longer act as trace boundaries.
99+
100+
<Alert type="warning" title="Downstream SDK usage">
101+
If you interact with the Native SDK in the context of a downstream SDK (for instance Android, .NET, Unity, or Unreal), we urge you not to use `sentry_regenerate_trace()` since it would interfere with the traces managed from those SDKs.
102+
</Alert>

0 commit comments

Comments
 (0)