Skip to content

Commit 2c02da2

Browse files
feat(native): transactions as automatic trace boundaries (#14586)
This PR adds documentation for the behavior introduced in getsentry/sentry-native#1270. This PR should be merged/deployed after the Native SDK release `0.10.0`. ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [x] None: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've added an urgent due date to it. Thanks in advance for your help! ## PRE-MERGE CHECKLIST *Make sure you've checked the following before merging your changes:* - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/) --------- Co-authored-by: JoshuaMoelans <[email protected]>
1 parent 008a160 commit 2c02da2

File tree

1 file changed

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

1 file changed

+48
-6
lines changed

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

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,68 @@ 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 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 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
102+
instance Android, .NET, Unity, or Unreal), we urge you not to use
103+
`sentry_regenerate_trace()` since it would interfere with the traces managed
104+
from those SDKs.
105+
</Alert>

0 commit comments

Comments
 (0)