Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Streamline sentry-trace, baggage and DSC handling #14364

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from

Conversation

mydea
Copy link
Member

@mydea mydea commented Nov 19, 2024

As a first step for #12385, I looked though our current implementations, and tried to streamline this. We have a bunch of somewhat duplicate code there that handles sentry-trace & baggage headers mostly the same but not fully the same, as well as relatedly the DSC.

To streamline this, I added some new methods in core:

  • getTraceData already exists, but was extended to also allow to pass a specific span to it. I updated some code to use this method that hasn't used it before, and also added some more tests - also around the ACS and the otel-specific implementation for this.
  • For this and edge cases, there are new primitives getDynamicSamplingContextFromScope and getTraceContextFromScope which handle picking these things from given scope etc.

While working on this, I also noticed that captureCheckIn in ServerRuntimeClient was actually not properly working in Node (OTEL), as it relied on the core way of scope-span coupling. So I also updated this to actually work as expected.

@mydea mydea self-assigned this Nov 19, 2024
};

const trace = evt.contexts && evt.contexts.trace;
if (!trace && propagationContext) {
Copy link
Member Author

Choose a reason for hiding this comment

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

These checks were not really necessary, I believe - first, propagationContext cannot be empty here. And second, we do not need to check for trace as this will be overwritten anyhow by ...evt.contexts if it already exists.

};

const sentryTraceHeader =
span && hasTracingEnabled() ? spanToTraceHeader(span) : generateSentryTraceHeader(traceId, spanId, sampled);
Copy link
Member Author

Choose a reason for hiding this comment

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

Here, we have checked for span && hasTracingEnabled(), while in fetch we did not check for hasTracingEnabled(). I opted to use this logic (just check span) everywhere now...

Copy link
Member

Choose a reason for hiding this comment

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

For browser, I think that's fine!

Copy link
Contributor

github-actions bot commented Nov 19, 2024

size-limit report 📦

Path Size % Change Change
@sentry/browser 22.86 KB -0.08% -18 B 🔽
@sentry/browser - with treeshaking flags 21.57 KB -0.01% -2 B 🔽
@sentry/browser (incl. Tracing) 35.68 KB +0.72% +261 B 🔺
@sentry/browser (incl. Tracing, Replay) 72.38 KB +0.38% +277 B 🔺
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 62.67 KB +0.41% +258 B 🔺
@sentry/browser (incl. Tracing, Replay with Canvas) 76.68 KB +0.36% +275 B 🔺
@sentry/browser (incl. Tracing, Replay, Feedback) 89.13 KB +0.28% +247 B 🔺
@sentry/browser (incl. Feedback) 39.61 KB -0.02% -6 B 🔽
@sentry/browser (incl. sendFeedback) 27.5 KB -0.05% -13 B 🔽
@sentry/browser (incl. FeedbackAsync) 32.3 KB -0.05% -16 B 🔽
@sentry/react 25.57 KB -0.05% -12 B 🔽
@sentry/react (incl. Tracing) 38.49 KB +0.54% +209 B 🔺
@sentry/vue 27.03 KB -0.03% -6 B 🔽
@sentry/vue (incl. Tracing) 37.5 KB +0.69% +263 B 🔺
@sentry/svelte 23.01 KB -0.09% -20 B 🔽
CDN Bundle 24.03 KB -0.07% -17 B 🔽
CDN Bundle (incl. Tracing) 37.24 KB +0.68% +255 B 🔺
CDN Bundle (incl. Tracing, Replay) 71.92 KB +0.32% +233 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 77.25 KB +0.27% +207 B 🔺
CDN Bundle - uncompressed 70.88 KB -0.04% -25 B 🔽
CDN Bundle (incl. Tracing) - uncompressed 110.62 KB +0.45% +507 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 223.15 KB +0.23% +507 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 236.36 KB +0.21% +507 B 🔺
@sentry/nextjs (client) 38.59 KB +0.52% +201 B 🔺
@sentry/sveltekit (client) 36.18 KB +0.7% +254 B 🔺
@sentry/node 134.53 KB +0.04% +47 B 🔺
@sentry/node - without tracing 96.37 KB +0.06% +51 B 🔺
@sentry/aws-serverless 106.61 KB +0.04% +34 B 🔺

View base workflow run

@mydea mydea force-pushed the fn/headers-unify branch 2 times, most recently from 63ab398 to 0d74e78 Compare November 19, 2024 14:24
@mydea mydea requested review from Lms24 and lforst November 19, 2024 15:47
@mydea mydea marked this pull request as ready for review November 19, 2024 15:47
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

maybe I'm missing something obvious but why do we need getSentryHeaders? Doesn't getTraceData already do this? I'd prefer we avoid adding another utility function if we don't strictly have to do it.

@mydea
Copy link
Member Author

mydea commented Nov 20, 2024

maybe I'm missing something obvious but why do we need getSentryHeaders? Doesn't getTraceData already do this? I'd prefer we avoid adding another utility function if we don't strictly have to do it.

Hmm, you are right that these are kind of similar 🤔 but getTraceData uses the new getTraceHeaders under the hood, but probably we can/should unify this some more. I'll do another pass and see if/how that makes sense! 👍

request,
client,
scope,
undefined,
Copy link
Member Author

Choose a reason for hiding this comment

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

these just remain for backwards compatibility, because addTracingHeadersToFetchRequest is exported :/

client.init();

return client;
}

describe('getTraceData', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

these tests relied a lot on mocks, and since the underlying implementation was changed a bunch of this broke. So I rewrote these tests to use "real" stuff as much as possible, but tried to keep the scenarios we want to cover intact.

Copy link

codecov bot commented Nov 20, 2024

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
650 1 649 33
View the top 1 failed tests by shortest run time
tracing.test.ts Initially loaded page contains trace meta tags from backend trace
Stack Traces | 0.158s run time
tracing.test.ts:3:1 Initially loaded page contains trace meta tags from backend trace

To view more test analytics, go to the Test Analytics Dashboard
Got feedback? Let us know on Github

@@ -91,9 +88,14 @@ export class SentryPropagator extends W3CBaggagePropagator {

const activeSpan = trace.getSpan(context);
const url = activeSpan && getCurrentURL(activeSpan);
// We only want to check the URL against tracePropagationTargets for outgoing request spans
// In other cases, we always inject the trace data
const shouldCheckUrl =
Copy link
Member Author

Choose a reason for hiding this comment

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

Without this, injecting metadata e.g. in remix (but possibly other places!) would fail, because in this scenario the active span may be e.g. the http.server span, which has a http.url attribute that we would check and which may fail to check against tracePropagationTargets, disabling injection and thus not generating any meta tags.

Now, we only check against the http.url if the span is a http.client span.

Copy link
Member Author

Choose a reason for hiding this comment

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

sadly span kind is not set in a super consistent way, so to be safe we also check the op to ensure out internal use-cases work.

Just to be clear, for all other spans trace data will always be injected.

@mydea
Copy link
Member Author

mydea commented Nov 20, 2024

OK @Lms24 and @lforst I have updated this to now use getTraceData everywhere & consistently.
This uncovered a bunch of edge cases and problems, which I hopefully all fixed now. Sadly bundle size increased instead of decreased :( but IMHO it's worth it because at least behavior should be consistent now...!

@@ -7,6 +7,7 @@ sentryTest('should not add source context lines to errors from script files', as
const url = await getLocalTestUrl({ testDir: __dirname });

const eventReqPromise = waitForErrorRequestOnUrl(page, url);
await page.waitForFunction('window.Sentry');
Copy link
Member Author

Choose a reason for hiding this comment

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

kind of unrelated but this kept flaking every now and then, just fixing this here...

@mydea
Copy link
Member Author

mydea commented Nov 20, 2024

Nevermind, still not working - need another round 😬

@mydea
Copy link
Member Author

mydea commented Nov 21, 2024

@lforst & @Lms24 OK, Now for real I think it should work.

I had to change the custom getTraceData implementation in OTEL to not use the propagator directly, because this also ensured that we checked the active span's url for trace propagation targets and possibly skip the sentry headers 😬 So instead, we now call the getInjectionData method from the propagator directly in getTraceData, which should ensure we have the same implementation but without the checks.

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

I think this is a good refactor! A bit concerned that bundle size is increasing although it looks like we removed more code than we added 😅

};

const sentryTraceHeader =
span && hasTracingEnabled() ? spanToTraceHeader(span) : generateSentryTraceHeader(traceId, spanId, sampled);
Copy link
Member

Choose a reason for hiding this comment

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

For browser, I think that's fine!

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.

3 participants