-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Convert scope contexts to segment span attributes in span streaming #20828
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
Open
chargome
wants to merge
3
commits into
develop
Choose a base branch
from
cg-applyScopeToSegmentSpan
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
dev-packages/node-integration-tests/suites/context-streamed/scope-contexts/scenario.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| tracesSampleRate: 1.0, | ||
| traceLifecycle: 'stream', | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| Sentry.withIsolationScope(isolationScope => { | ||
| isolationScope.setContext('response', { status_code: 200 }); | ||
| isolationScope.setContext('cloud_resource', { 'cloud.provider': 'aws', 'cloud.region': 'us-east-1' }); | ||
| isolationScope.setContext('profile', { profile_id: 'abc123' }); | ||
| isolationScope.setContext('react', { version: '18.2.0' }); | ||
|
|
||
| Sentry.startSpan({ name: 'test-span' }, () => { | ||
| // noop | ||
| }); | ||
| }); | ||
|
|
||
| void Sentry.flush(); |
33 changes: 33 additions & 0 deletions
33
dev-packages/node-integration-tests/suites/context-streamed/scope-contexts/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { afterAll, expect, test } from 'vitest'; | ||
| import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
|
||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| test('scope contexts are converted to segment span attributes in span streaming', async () => { | ||
| await createRunner(__dirname, 'scenario.ts') | ||
| .expect({ | ||
| span: container => { | ||
| const segmentSpan = container.items.find(s => !!s.is_segment); | ||
| expect(segmentSpan).toBeDefined(); | ||
|
|
||
| const attrs = segmentSpan!.attributes!; | ||
|
|
||
| // response context -> http.response.* attributes | ||
| expect(attrs['http.response.status_code']).toEqual({ type: 'integer', value: 200 }); | ||
|
|
||
| // cloud_resource context (dot-notation passthrough) | ||
| expect(attrs['cloud.provider']).toEqual({ type: 'string', value: 'aws' }); | ||
| expect(attrs['cloud.region']).toEqual({ type: 'string', value: 'us-east-1' }); | ||
|
|
||
| // profile context | ||
| expect(attrs['sentry.profile_id']).toEqual({ type: 'string', value: 'abc123' }); | ||
|
|
||
| // framework version context | ||
| expect(attrs['react.version']).toEqual({ type: 'string', value: '18.2.0' }); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import type { Contexts } from '../../types-hoist/context'; | ||
|
|
||
| /** | ||
| * Convert known scope contexts set by SDK integrations to span attributes. | ||
| * Only maps context keys that are relevant to browser SDKs. | ||
| * Server-only contexts (aws, gcp, missing_instrumentation, trpc) are handled | ||
| * by processSegmentSpan hooks in their respective packages. | ||
| */ | ||
| export function scopeContextsToSpanAttributes(contexts: Contexts): Record<string, unknown> { | ||
| const attrs: Record<string, unknown> = {}; | ||
|
|
||
| const { response, profile, cloud_resource, culture, state } = contexts; | ||
|
|
||
| if (response) { | ||
| if (response.status_code != null) { | ||
| attrs['http.response.status_code'] = response.status_code; | ||
| } | ||
| if (response.body_size != null) { | ||
| attrs['http.response.body.size'] = response.body_size; | ||
| } | ||
| } | ||
|
|
||
| if (profile) { | ||
| if (profile.profile_id) { | ||
| attrs['sentry.profile_id'] = profile.profile_id; | ||
| } | ||
| if (profile.profiler_id) { | ||
| attrs['sentry.profiler_id'] = profile.profiler_id; | ||
| } | ||
| if (profile.start_timestamp != null) { | ||
| attrs['profile.start_timestamp'] = profile.start_timestamp; | ||
| } | ||
| } | ||
|
|
||
| // CloudResourceContext keys are already in dot-notation (OTel resource conventions) | ||
| if (cloud_resource) { | ||
| for (const [key, value] of Object.entries(cloud_resource)) { | ||
| if (value != null) { | ||
| attrs[key] = value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (culture) { | ||
| if (culture.locale) { | ||
| attrs['culture.locale'] = culture.locale; | ||
| } | ||
| if (culture.timezone) { | ||
| attrs['culture.timezone'] = culture.timezone; | ||
| } | ||
| } | ||
|
|
||
| if (state?.state && typeof state.state.type === 'string') { | ||
| attrs['state.type'] = state.state.type; | ||
| } | ||
|
|
||
| // Framework version contexts | ||
| const angular = contexts['angular']; | ||
| if (angular) { | ||
| const version = angular['version']; | ||
| if (typeof version === 'string' || typeof version === 'number') { | ||
| attrs['angular.version'] = version; | ||
| } | ||
| } | ||
|
|
||
| const react = contexts['react']; | ||
| if (react) { | ||
| const version = react['version']; | ||
| if (typeof version === 'string' || typeof version === 'number') { | ||
| attrs['react.version'] = version; | ||
| } | ||
| } | ||
|
|
||
| return attrs; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: Does it make sense to have framework specific code in
core? I'm fine with it, just challenging if we really want to do this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It basically syncs all context centrally that can make its way into browser sdks. But I do see your point obv