Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

bitsandfoxes
Copy link
Contributor

@bitsandfoxes bitsandfoxes commented Jan 30, 2025

Motivation

Looking at it from one of the upstream SDKs (i.e. Android SDK, Unity SDK) there is currently no way to connect errors and events from their environment with the events sent by sentry-native.
The idea is to provide a way for those SDKs to propagate their trace to be used and applied to events. The trace ID can then be used to link those events across multiple SDKs, languages, and layers.

Copy link

github-actions bot commented Jan 30, 2025

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 2a9c423

@bitsandfoxes bitsandfoxes changed the title feat: Provide set_trace_id to continue trace feat: Provide set_trace_id Feb 6, 2025
@bitsandfoxes bitsandfoxes changed the title feat: Provide set_trace_id feat: Provide set_trace Feb 6, 2025
Copy link

codecov bot commented Feb 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.81%. Comparing base (2577cfc) to head (2a9c423).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1137      +/-   ##
==========================================
+ Coverage   82.66%   82.81%   +0.15%     
==========================================
  Files          53       53              
  Lines        7954     7973      +19     
  Branches     1246     1247       +1     
==========================================
+ Hits         6575     6603      +28     
+ Misses       1266     1256      -10     
- Partials      113      114       +1     

@bitsandfoxes bitsandfoxes requested review from supervacuus and markushi and removed request for supervacuus February 7, 2025 10:24
Copy link
Collaborator

@supervacuus supervacuus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides a changelog, I missed a basic test for the interaction with transactions. Something like this in test_tracing.c:

void
apply_scope_and_check_trace_context(
    sentry_options_t *options, const char *trace_id, const char *parent_span_id)
{
    // simulate scope application onto an event
    sentry_value_t event = sentry_value_new_object();
    SENTRY_WITH_SCOPE (scope) {
        sentry__scope_apply_to_event(scope, options, event, SENTRY_SCOPE_NONE);
    }

    // check that the event has a trace context
    sentry_value_t event_contexts = sentry_value_get_by_key(event, "contexts");
    TEST_CHECK(!sentry_value_is_null(event_contexts));
    TEST_CHECK(
        sentry_value_get_type(event_contexts) == SENTRY_VALUE_TYPE_OBJECT);

    sentry_value_t event_trace_context
        = sentry_value_get_by_key(event_contexts, "trace");
    TEST_CHECK(!sentry_value_is_null(event_trace_context));
    TEST_CHECK(
        sentry_value_get_type(event_trace_context) == SENTRY_VALUE_TYPE_OBJECT);

    // check trace context content
    const char *event_trace_id = sentry_value_as_string(
        sentry_value_get_by_key(event_trace_context, "trace_id"));
    TEST_CHECK_STRING_EQUAL(event_trace_id, trace_id);

    const char *event_trace_parent_span_id = sentry_value_as_string(
        sentry_value_get_by_key(event_trace_context, "parent_span_id"));
    TEST_CHECK_STRING_EQUAL(event_trace_parent_span_id, parent_span_id);

    sentry_uuid_t event_trace_span_id = sentry__value_as_uuid(
        sentry_value_get_by_key(event_trace_context, "span_id"));
    TEST_CHECK(!sentry_uuid_is_nil(&event_trace_span_id));

    sentry_value_decref(event);
}

SENTRY_TEST(set_trace_id_with_txn)
{
    // initialize SDK so we have a scope
    sentry_options_t *options = sentry_options_new();
    sentry_options_set_traces_sample_rate(options, 1.0);
    sentry_options_set_sample_rate(options, 1.0);
    sentry_init(options);

    // inject a trace via trace-header into a transaction
    const char *trace_header
        = "2674eb52d5874b13b560236d6c79ce8a-a0f9fdf04f1a63df-1";
    const char *txn_trace_id = "2674eb52d5874b13b560236d6c79ce8a";
    const char *txn_parent_span_id = "a0f9fdf04f1a63df";
    sentry_transaction_context_t *tx_ctx
        = sentry_transaction_context_new("wow!", NULL);
    sentry_transaction_context_update_from_header(
        tx_ctx, "sentry-trace", trace_header);
    sentry_transaction_t *tx
        = sentry_transaction_start(tx_ctx, sentry_value_new_null());

    // set the direct trace first
    const char *direct_trace_id = "aaaabbbbccccddddeeeeffff00001111";
    const char *direct_parent_span_id = "f0f0f0f0f0f0f0f0";
    sentry_set_trace(direct_trace_id, direct_parent_span_id);

    // events should get that trace applied
    apply_scope_and_check_trace_context(
        options, direct_trace_id, direct_parent_span_id);

    // now set a scoped transaction
    sentry_set_transaction_object(tx);

    // events should get the transaction's trace applied
    apply_scope_and_check_trace_context(
        options, txn_trace_id, txn_parent_span_id);

    sentry_transaction_finish(tx);

    // after finishing the transaction, the direct trace should hit again
    apply_scope_and_check_trace_context(
        options, direct_trace_id, direct_parent_span_id);

    sentry_close();
}

@bitsandfoxes bitsandfoxes marked this pull request as ready for review February 26, 2025 17:15
@bitsandfoxes bitsandfoxes changed the title feat: Provide set_trace feat: Added sentry_set_trace Feb 26, 2025
Copy link
Member

@markushi markushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, especially the Java part 🤓
You probably need to apply the code formatter + .api file generator using
./gradlew spotlessApply + ./gradlew :sentry-native-ndk:apiDump to make CI happier.

Copy link
Collaborator

@supervacuus supervacuus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If downstream, y'all are also happy, let's do this.

@bitsandfoxes bitsandfoxes merged commit 3dd0966 into master Feb 27, 2025
25 checks passed
@bitsandfoxes bitsandfoxes deleted the feat/set-trace-id branch February 27, 2025 18:31
@zsd4yr
Copy link
Contributor

zsd4yr commented Apr 9, 2025

@supervacuus @bitsandfoxes Question on usage -- where do the trace and parent span ids originate from?

@zsd4yr
Copy link
Contributor

zsd4yr commented Apr 9, 2025

Like are you just meant to make one? with sentry_uuid_new_v4 or something? that does not seem right

@supervacuus
Copy link
Collaborator

As explained in the inline docs and changelog, this function should primarily be used by downstream SDKs. Do you plan to write one?

@zsd4yr
Copy link
Contributor

zsd4yr commented Apr 9, 2025

Hey @supervacuus yes, I am following that -- but where does the originating trace and parent span id come from? Like what API creates them?

Let's say I own an upstream SDK. How do I create the necessary parameters to this method?

@zsd4yr
Copy link
Contributor

zsd4yr commented Apr 9, 2025

For example, imagine I have 2 applications, one is my crash reporter and other is the application it is observing. My hope is to use this new API to "share a trace" between the two apps so that I can create breadcrumbs in the primary application which get married to the crash event when a crash is reported to sentry by the other out-of-proc crash reporter application

For that to work, I have to have some means of creating the traceid and parent span id from the primary application

@zsd4yr
Copy link
Contributor

zsd4yr commented Apr 9, 2025

Ah perhaps sentry_add_breadcrumb won't work for this at all as it needs a backend. Ours is null as we generate the dump ourselves by calling breakpad directly. :(

@zsd4yr
Copy link
Contributor

zsd4yr commented Apr 11, 2025

sorry for the noise, going to just start my own dedicated issue on this topic #1197

JoshuaMoelans added a commit to getsentry/sentry-docs that referenced this pull request May 13, 2025
<!-- Use this checklist to make sure your PR is ready for merge. You may
delete any sections you don't need. -->

[Vercel
Preview](https://sentry-docs-htn2uy3fb.sentry.dev/platforms/native/tracing/instrumentation/custom-instrumentation/#:~:text=it%20to%20Sentry.-,By%20default%2C,-transactions%20will%20inherit)

## DESCRIBE YOUR PR
Related to the feature added in
getsentry/sentry-native#1137 & the update to
native tracing in getsentry/sentry-native#1200

Since transactions no longer create a new `trace_id`, we want to
document how to set up a trace boundary with `sentry_set_trace()`.

## 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)
bitsandfoxes pushed a commit to getsentry/sentry-docs that referenced this pull request Jul 3, 2025
<!-- Use this checklist to make sure your PR is ready for merge. You may
delete any sections you don't need. -->

[Vercel
Preview](https://sentry-docs-htn2uy3fb.sentry.dev/platforms/native/tracing/instrumentation/custom-instrumentation/#:~:text=it%20to%20Sentry.-,By%20default%2C,-transactions%20will%20inherit)

## DESCRIBE YOUR PR
Related to the feature added in
getsentry/sentry-native#1137 & the update to
native tracing in getsentry/sentry-native#1200

Since transactions no longer create a new `trace_id`, we want to
document how to set up a trace boundary with `sentry_set_trace()`.

## 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants