Skip to content

feat(core): Add OpenTelemetry-specific getTraceData implementation #13281

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

Merged
merged 7 commits into from
Aug 12, 2024

Conversation

Lms24
Copy link
Member

@Lms24 Lms24 commented Aug 8, 2024

This PR adds an Otel-specific version of getTraceData and adds the getTraceData function to the AsyncContextStrategy interface. This allows us to dynamically choose either the default implementation (which works correctly for browser/non-POTEL SDKs) and the Otel-specific version.

Our previous, universal implementation of getTraceData didn't propagate the correct trace data in POTEL SDKs in Tracing Without Performance mode. The reason is that even in TwP mode, we start a non-recording span which is available via getActiveSpan. The previous implementation would therefore incorrectly take trace data from this span and even more incorrectly add a negative sampling decision to the trace data instead of deferring it.

Another change in this PR is that I removed the optional parameters for getTraceData and getTraceMetaTags. This is breaking but the function wasn't documented publicly at all yet. The reason is that this was premature API expansion because we realized we didn't need this functionality in our internal use cases. Furthermore, it would have severely complicated obtaining the correct trace data in the Otel implementation.

@Lms24 Lms24 force-pushed the lms/fix-getTraceData-otel branch from e954a52 to 9729ece Compare August 8, 2024 14:54
@Lms24 Lms24 requested a review from mydea August 8, 2024 14:54
* Otel-specific implementation of `getTraceData`.
* @see `@sentry/core` version of `getTraceData` for more information
*/
export function getTraceData(span?: Span): TraceData {
Copy link
Member Author

Choose a reason for hiding this comment

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

Would appreciate a review @mydea - I might be far off here. Basically what I need to get is a Context to pass to propagation.inject. I followed the code in our node fetch integration to get it but I'm not sure if this is correct:

const ctx = context.active();
const addedHeaders: Record<string, string> = {};
// We generate a virtual span context from the active one,
// Where we attach the URL to the trace state, so the propagator can pick it up
const activeSpan = trace.getSpan(ctx);
const propagationContext = activeSpan
? getPropagationContextFromSpan(activeSpan)
: getCurrentScope().getPropagationContext();
const spanContext = generateSpanContextForPropagationContext(propagationContext);
// We know that in practice we'll _always_ haven a traceState here
spanContext.traceState = spanContext.traceState?.set('sentry.url', url);
const ctxWithUrlTraceState = trace.setSpanContext(ctx, spanContext);
propagation.inject(ctxWithUrlTraceState, addedHeaders);

Copy link
Member Author

@Lms24 Lms24 Aug 8, 2024

Choose a reason for hiding this comment

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

Or IOW, maybe we can get rid of most code here and instead just somehow get to a Context directly from the SpanContex of spanToUse.spanContext()?

Copy link
Member

Choose a reason for hiding this comment

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

The tricky thing is getting the context from the span, if one is passed in. but it should be possible, you can get the scope via getCapturedScopesOnSpan(span).scope, and from there use getContextFromScope(scope) to get the context.

Then, we can really just do this:

function getContextForSpan(span) {
  const scopes = getCapturedScopesOnSpan(span);
  if (scopes.scope) {
    return getContextFromScope(scopes.scope);
  }
  return undefined;
}

const ctx = (span && getContextForSpan(span)) || context.active();

const headers = {};
// this mutates the headers object
// this also handles it if there is no active span etc.
propagation.inject(ctx, headers, headerSetter);
return headers;

Copy link
Contributor

github-actions bot commented Aug 8, 2024

size-limit report 📦

Path Size
@sentry/browser 22.47 KB (0%)
@sentry/browser (incl. Tracing) 34.26 KB (0%)
@sentry/browser (incl. Tracing, Replay) 70.32 KB (0%)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 63.65 KB (0%)
@sentry/browser (incl. Tracing, Replay with Canvas) 74.71 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback) 87.33 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 89.17 KB (0%)
@sentry/browser (incl. metrics) 26.77 KB (0%)
@sentry/browser (incl. Feedback) 39.42 KB (0%)
@sentry/browser (incl. sendFeedback) 27.09 KB (0%)
@sentry/browser (incl. FeedbackAsync) 31.75 KB (0%)
@sentry/react 25.24 KB (0%)
@sentry/react (incl. Tracing) 37.25 KB (0%)
@sentry/vue 26.62 KB (0%)
@sentry/vue (incl. Tracing) 36.1 KB (0%)
@sentry/svelte 22.61 KB (0%)
CDN Bundle 23.69 KB (0%)
CDN Bundle (incl. Tracing) 35.94 KB (0%)
CDN Bundle (incl. Tracing, Replay) 70.36 KB (0%)
CDN Bundle (incl. Tracing, Replay, Feedback) 75.62 KB (0%)
CDN Bundle - uncompressed 69.52 KB (0%)
CDN Bundle (incl. Tracing) - uncompressed 106.45 KB (0%)
CDN Bundle (incl. Tracing, Replay) - uncompressed 218.29 KB (0%)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 231.19 KB (0%)
@sentry/nextjs (client) 37.11 KB (0%)
@sentry/sveltekit (client) 34.82 KB (0%)
@sentry/node 115.58 KB (+0.06% 🔺)
@sentry/node - without tracing 89.95 KB (+0.05% 🔺)
@sentry/aws-serverless 99.38 KB (+0.05% 🔺)

@Lms24 Lms24 self-assigned this Aug 9, 2024
@Lms24 Lms24 changed the title feat(core): Add OpenTelemetry-specific getTraceData version feat(core): Add OpenTelemetry-specific getTraceData implementation Aug 9, 2024
@Lms24 Lms24 force-pushed the lms/fix-getTraceData-otel branch from 92353fd to 962962d Compare August 9, 2024 09:25
const context = api.context.active();

// This should never happen, given we always create an ambient non-recording span if there's no active span.
if (!context) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this cannot be empty? 🤔 What does the type say?

Copy link
Member Author

Choose a reason for hiding this comment

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

I could have sworn TS screamed at me previously that the type was Context | undefined but after re-checking you're right, we always get back a context. Adjusted it

@Lms24 Lms24 marked this pull request as ready for review August 9, 2024 12:31
@Lms24 Lms24 requested review from mydea and andreiborza August 9, 2024 12:31
@@ -1,4 +1,4 @@
export type { ClientClass } from './sdk';
export type { ClientClass as SentryCoreCurrentScopes } from './sdk';
Copy link
Member

Choose a reason for hiding this comment

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

m: is this on purpose? 😅 seems unintentional (and probably breaking, technically...)

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

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

nice, IMHO this is much better and simpler/clearer - reduced complexity! 🚀

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.

2 participants