Skip to content

Commit 0c58a2f

Browse files
feat(native): add propagate_traceparent option (#15162)
Vercel previews: - [options](https://sentry-docs-git-joshua-featpropagatetraceparentoption.sentry.dev/platforms/native/configuration/options/#propagate-traceparent) - [trace propagation example](https://sentry-docs-git-joshua-featpropagatetraceparentoption.sentry.dev/platforms/native/tracing/trace-propagation/custom-instrumentation/) ## DESCRIBE YOUR PR Documents the changes from getsentry/sentry-native#1394 ## 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)
1 parent 3fbc47b commit 0c58a2f

File tree

2 files changed

+40
-2
lines changed
  • docs/platforms/native/common/configuration
  • platform-includes/distributed-tracing/custom-instrumentation

2 files changed

+40
-2
lines changed

docs/platforms/native/common/configuration/options.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ Whether Crashpad should delay application shutdown until the upload of the crash
6464

6565
</ConfigKey>
6666

67+
<ConfigKey name="propagate-traceparent">
68+
69+
Controls whether the SDK should propagate the W3C `traceparent` HTTP header alongside the `sentry-trace` header for [distributed tracing](https://docs.sentry.io/platforms/native/tracing/trace-propagation/custom-instrumentation/). This option defaults to `false`.
70+
71+
</ConfigKey>
72+
6773
## Hooks
6874

6975
These options can be used to hook the SDK in various ways to customize the reporting of events.

platform-includes/distributed-tracing/custom-instrumentation/native.mdx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ On this page you will learn how to manually propagate trace information into and
22

33
To obtain trace headers from a transaction, use `sentry_transaction_iter_headers()`. For a span, use `sentry_span_iter_headers()`. Pass the returned value to the downstream service. If communication happens over HTTP, we recommend you attach all headers to the outgoing HTTP request.
44

5+
By default, only the `sentry-trace` header is generated. To also generate W3C `traceparent` headers for interoperability with other tracing systems, enable traceparent propagation:
6+
7+
```c
8+
sentry_options_set_propagate_traceparent(options, 1);
9+
```
10+
11+
When enabled, both `sentry-trace` and `traceparent` headers will be generated with the same trace and span information.
12+
513
Continuing a trace from an upstream service requires using `sentry_transaction_context_update_from_header()`. Before starting a transaction, pass its transaction context into the previous function along with the `sentry-trace` header. The transaction started with the transaction context will contain everything needed to continue the trace.
614
715
To obtain headers from a transaction so it can be continued from a downstream service, define a function which merges the headers into some aggregate object. Use the function in `sentry_transaction_iter_headers()` as a callback. The following example uses `sentry_value_t` as the aggregate object:
@@ -14,18 +22,42 @@ copy_headers_to(const char *key, const char *value, void *userdata) {
1422
}
1523
1624
int main(int argc, char **argv) {
25+
sentry_options_t *options = sentry_options_new();
26+
sentry_options_set_dsn(options, "___PUBLIC_DSN___");
27+
28+
// Enable traceparent propagation for W3C compatibility
29+
sentry_options_set_propagate_traceparent(options, 1);
30+
31+
sentry_init(options);
32+
1733
// Transaction to continue off of
18-
sentry_transaction_context_t *tx_ctx = sentry_transaction_context_new(
34+
sentry_transaction_context_t *tx_ctx = sentry_transaction_context_new(
1935
"honk",
2036
NULL
2137
);
2238
sentry_transaction_t *tx = sentry_transaction_start(tx_ctx, sentry_value_new_null());
2339
24-
sentry_value_t headers = sentry_value_new_object();
40+
sentry_value_t headers = sentry_value_new_object();
2541
sentry_transaction_iter_headers(tx, copy_headers_to, (void *) &headers);
42+
43+
// The headers object now contains both "sentry-trace" and "traceparent"
44+
// (if traceparent propagation is enabled)
45+
// Example values:
46+
// - "sentry-trace": "2674eb52d5874b13b560236d6c79ce8a-a0f9fdf04f1a63df-1"
47+
// - "traceparent": "00-2674eb52d5874b13b560236d6c79ce8a-a0f9fdf04f1a63df-01"
48+
49+
sentry_transaction_finish(tx);
50+
sentry_close();
2651
}
2752
```
2853

54+
The key differences in the generated headers are:
55+
- `sentry-trace`: Uses Sentry's format with sampling flag as "0" or "1"
56+
- `traceparent`: Uses W3C format with version "00" and sampling flag as "00" or "01"
57+
58+
Both headers contain the same trace ID and span ID, ensuring compatibility across different tracing systems.
59+
60+
2961
To create a transaction as a continuation of a trace retrieved from an upstream service, pass an iterator of the incoming headers to the transaction context:
3062

3163
```c

0 commit comments

Comments
 (0)