Skip to content

feat(core): Add span.spanContext()#10037

Merged
mydea merged 3 commits intodevelopfrom
fn/spanContext
Jan 8, 2024
Merged

feat(core): Add span.spanContext()#10037
mydea merged 3 commits intodevelopfrom
fn/spanContext

Conversation

@mydea
Copy link
Copy Markdown
Member

@mydea mydea commented Jan 3, 2024

This will replace spanId, traceId and sampled lookups.
Because the bitwise format of OTEL is a bit weird IMHO to check if a span is sampled, I also added a utility spanIsSampled(span) method to abstract this away.

@mydea mydea requested review from AbhiPrasad, Lms24 and lforst January 3, 2024 13:29
@mydea mydea self-assigned this Jan 3, 2024
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jan 3, 2024

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped) 76.47 KB (+0.34% 🔺)
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 67.88 KB (+0.43% 🔺)
@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped) 61.5 KB (+0.47% 🔺)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 32.13 KB (+0.92% 🔺)
@sentry/browser (incl. Feedback) - Webpack (gzipped) 30.7 KB (+0.49% 🔺)
@sentry/browser - Webpack (gzipped) 22.07 KB (+0.7% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback) - ES6 CDN Bundle (gzipped) 74.11 KB (+0.4% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 65.76 KB (+0.44% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 31.95 KB (+0.94% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped) 23.77 KB (+0.85% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 206.62 KB (+0.65% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 96.51 KB (+1.4% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 70.98 KB (+1.38% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 34.9 KB (+0.93% 🔺)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 68.26 KB (+0.44% 🔺)
@sentry/react - Webpack (gzipped) 22.1 KB (+0.67% 🔺)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 84.98 KB (+0.35% 🔺)
@sentry/nextjs Client - Webpack (gzipped) 49.33 KB (+0.61% 🔺)
@sentry-internal/feedback - Webpack (gzipped) 16.74 KB (+0.94% 🔺)

@mydea mydea force-pushed the fn/spanContext branch 4 times, most recently from 3ce59d8 to 25027f9 Compare January 4, 2024 11:38
Comment on lines +70 to +110
export function spanIsSampled(span: Span): boolean {
const { traceFlags } = span.spanContext();
// eslint-disable-next-line no-bitwise
return Boolean(traceFlags & TraceFlagSampled);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add a reference to OTel for this bit operation? I assume there's some documentation around the bit fields or masks?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I'll reference where this comes from 😅

Comment on lines +4 to +6
export const TraceFlagNone = 0x0;
// eslint-disable-next-line no-bitwise
export const TraceFlagSampled = 0x1 << 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

l: Should these be all uppercase?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good point, makes more sense!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

also, 0x1 << 0 is just 0x1, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I think so, I copied this from OTEL, not sure why they have this weird syntax - it is the same, right? 🤔

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think so. Maybe they do it just for clarity?

0x1 << 0 // sampled flag
0x1 << 1 // another flag

but yes, it's identical

> 0x1 === 0x1 << 0
true

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I updated it and added a comment that this is aligned to how this is in OTEL!

@mydea mydea merged commit 73a8314 into develop Jan 8, 2024
@mydea mydea deleted the fn/spanContext branch January 8, 2024 13:40
@mustafa0x
Copy link
Copy Markdown

I get this error after upgrading

An error occurred on the server side: TypeError: span.spanContext is not a function
    at Object.spanToTraceContext (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/utils/spanUtils.js:13:55)
    at applySpanToEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/utils/applyScopeDataToEvent.js:146:39)
    at Object.applyScopeDataToEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/utils/applyScopeDataToEvent.js:20:5)
    at Object.prepareEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/utils/prepareEvent.js:87:25)
    at NodeClient._prepareEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/baseclient.js:489:25)
    at NodeClient._prepareEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/server-runtime-client.js:228:18)
    at NodeClient._processEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/baseclient.js:585:17)
    at NodeClient._captureEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/baseclient.js:528:17)
    at NodeClient.captureEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/baseclient.js:165:12)
    at NodeClient.captureEvent (/.../node_modules/.pnpm/@sentry+core@7.93.0/node_modules/@sentry/core/cjs/server-runtime-client.js:103:18) {

@mydea
Copy link
Copy Markdown
Member Author

mydea commented Jan 17, 2024

@mustafa0x can you make sure you have no @sentry/xxx packages installed with a differing/older version? That's the by far most common reason for getting an error like this!

@mustafa0x
Copy link
Copy Markdown

mustafa0x commented Jan 17, 2024

@mydea deleting node_modules and pnpm-lock.yaml did indeed fix! In pnpm-lock.yaml I noticed /@sentry/core@7.93.0 and /@sentry/core@7.91.0. Quite strange.

Thanks!

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