-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Add StreamedSpanEnvelope creation function
#19153
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
+280
−12
Merged
Changes from all commits
Commits
Show all changes
5 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
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,2 @@ | ||
| For now, all span streaming related tracing code is in this sub directory. | ||
| Once we get rid of transaction-based tracing, we can clean up and flatten the entire tracing directory. |
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,36 @@ | ||
| import type { Client } from '../../client'; | ||
| import type { DynamicSamplingContext, SpanContainerItem, StreamedSpanEnvelope } from '../../types-hoist/envelope'; | ||
| import type { SerializedStreamedSpan } from '../../types-hoist/span'; | ||
| import { dsnToString } from '../../utils/dsn'; | ||
| import { createEnvelope, getSdkMetadataForEnvelopeHeader } from '../../utils/envelope'; | ||
|
|
||
| /** | ||
| * Creates a span v2 span streaming envelope | ||
| */ | ||
| export function createStreamedSpanEnvelope( | ||
| serializedSpans: Array<SerializedStreamedSpan>, | ||
| dsc: Partial<DynamicSamplingContext>, | ||
| client: Client, | ||
| ): StreamedSpanEnvelope { | ||
| const dsn = client.getDsn(); | ||
| const tunnel = client.getOptions().tunnel; | ||
| const sdk = getSdkMetadataForEnvelopeHeader(client.getOptions()._metadata); | ||
|
|
||
| const headers: StreamedSpanEnvelope[0] = { | ||
| sent_at: new Date().toISOString(), | ||
| ...(dscHasRequiredProps(dsc) && { trace: dsc }), | ||
| ...(sdk && { sdk }), | ||
| ...(!!tunnel && dsn && { dsn: dsnToString(dsn) }), | ||
| }; | ||
|
|
||
| const spanContainer: SpanContainerItem = [ | ||
| { type: 'span', item_count: serializedSpans.length, content_type: 'application/vnd.sentry.items.span.v2+json' }, | ||
| { items: serializedSpans }, | ||
| ]; | ||
|
|
||
| return createEnvelope<StreamedSpanEnvelope>(headers, [spanContainer]); | ||
| } | ||
|
|
||
| function dscHasRequiredProps(dsc: Partial<DynamicSamplingContext>): dsc is DynamicSamplingContext { | ||
| return !!dsc.trace_id && !!dsc.public_key; | ||
| } |
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
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,232 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createStreamedSpanEnvelope } from '../../../../src/tracing/spans/envelope'; | ||
| import type { DynamicSamplingContext } from '../../../../src/types-hoist/envelope'; | ||
| import type { SerializedStreamedSpan } from '../../../../src/types-hoist/span'; | ||
| import { getDefaultTestClientOptions, TestClient } from '../../../mocks/client'; | ||
|
|
||
| function createMockSerializedSpan(overrides: Partial<SerializedStreamedSpan> = {}): SerializedStreamedSpan { | ||
| return { | ||
| trace_id: 'abc123', | ||
| span_id: 'def456', | ||
| name: 'test-span', | ||
| start_timestamp: 1713859200, | ||
| end_timestamp: 1713859201, | ||
| status: 'ok', | ||
| is_segment: false, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe('createStreamedSpanEnvelope', () => { | ||
| describe('envelope headers', () => { | ||
| it('creates an envelope with sent_at header', () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient(getDefaultTestClientOptions()); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).toHaveProperty('sent_at', expect.any(String)); | ||
| }); | ||
|
|
||
| it('includes trace header when DSC has required props (trace_id and public_key)', () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient(getDefaultTestClientOptions()); | ||
| const dsc: DynamicSamplingContext = { | ||
| trace_id: 'trace-123', | ||
| public_key: 'public-key-abc', | ||
| sample_rate: '1.0', | ||
| release: 'v1.0.0', | ||
| }; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).toHaveProperty('trace', dsc); | ||
| }); | ||
|
|
||
| it("does't include trace header when DSC is missing trace_id", () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient(getDefaultTestClientOptions()); | ||
| const dsc: Partial<DynamicSamplingContext> = { | ||
| public_key: 'public-key-abc', | ||
| }; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).not.toHaveProperty('trace'); | ||
| }); | ||
|
|
||
| it("does't include trace header when DSC is missing public_key", () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient(getDefaultTestClientOptions()); | ||
| const dsc: Partial<DynamicSamplingContext> = { | ||
| trace_id: 'trace-123', | ||
| }; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).not.toHaveProperty('trace'); | ||
| }); | ||
|
|
||
| it('includes SDK info when available in client options', () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient( | ||
| getDefaultTestClientOptions({ | ||
| _metadata: { | ||
| sdk: { name: 'sentry.javascript.browser', version: '8.0.0' }, | ||
| }, | ||
| }), | ||
| ); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).toHaveProperty('sdk', { name: 'sentry.javascript.browser', version: '8.0.0' }); | ||
| }); | ||
|
|
||
| it("does't include SDK info when not available", () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient(getDefaultTestClientOptions()); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).not.toHaveProperty('sdk'); | ||
| }); | ||
|
|
||
| it('includes DSN when tunnel and DSN are configured', () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient( | ||
| getDefaultTestClientOptions({ | ||
| dsn: 'https://abc123@example.sentry.io/456', | ||
| tunnel: 'https://tunnel.example.com', | ||
| }), | ||
| ); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).toHaveProperty('dsn', 'https://abc123@example.sentry.io/456'); | ||
| }); | ||
|
|
||
| it("does't include DSN when tunnel is not configured", () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient( | ||
| getDefaultTestClientOptions({ | ||
| dsn: 'https://abc123@example.sentry.io/456', | ||
| }), | ||
| ); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).not.toHaveProperty('dsn'); | ||
| }); | ||
|
|
||
| it("does't include DSN when DSN is not available", () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient( | ||
| getDefaultTestClientOptions({ | ||
| tunnel: 'https://tunnel.example.com', | ||
| }), | ||
| ); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).not.toHaveProperty('dsn'); | ||
| }); | ||
|
|
||
| it('includes all headers when all options are provided', () => { | ||
| const mockSpan = createMockSerializedSpan(); | ||
| const mockClient = new TestClient( | ||
| getDefaultTestClientOptions({ | ||
| dsn: 'https://abc123@example.sentry.io/456', | ||
| tunnel: 'https://tunnel.example.com', | ||
| _metadata: { | ||
| sdk: { name: 'sentry.javascript.node', version: '10.38.0' }, | ||
| }, | ||
| }), | ||
| ); | ||
| const dsc: DynamicSamplingContext = { | ||
| trace_id: 'trace-123', | ||
| public_key: 'public-key-abc', | ||
| environment: 'production', | ||
| }; | ||
|
|
||
| const result = createStreamedSpanEnvelope([mockSpan], dsc, mockClient); | ||
|
|
||
| expect(result[0]).toEqual({ | ||
| sent_at: expect.any(String), | ||
| trace: dsc, | ||
| sdk: { name: 'sentry.javascript.node', version: '10.38.0' }, | ||
| dsn: 'https://abc123@example.sentry.io/456', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('envelope item', () => { | ||
| it('creates a span container item with correct structure', () => { | ||
| const mockSpan = createMockSerializedSpan({ name: 'span-1' }); | ||
| const mockClient = new TestClient(getDefaultTestClientOptions()); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const envelopeItems = createStreamedSpanEnvelope([mockSpan], dsc, mockClient)[1]; | ||
|
|
||
| expect(envelopeItems).toEqual([ | ||
| [ | ||
| { | ||
| content_type: 'application/vnd.sentry.items.span.v2+json', | ||
| item_count: 1, | ||
| type: 'span', | ||
| }, | ||
| { | ||
| items: [mockSpan], | ||
| }, | ||
| ], | ||
| ]); | ||
| }); | ||
|
|
||
| it('sets correct item_count for multiple spans', () => { | ||
| const mockSpan1 = createMockSerializedSpan({ span_id: 'span-1' }); | ||
| const mockSpan2 = createMockSerializedSpan({ span_id: 'span-2' }); | ||
| const mockSpan3 = createMockSerializedSpan({ span_id: 'span-3' }); | ||
| const mockClient = new TestClient(getDefaultTestClientOptions()); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const envelopeItems = createStreamedSpanEnvelope([mockSpan1, mockSpan2, mockSpan3], dsc, mockClient)[1]; | ||
|
|
||
| expect(envelopeItems).toEqual([ | ||
| [ | ||
| { type: 'span', item_count: 3, content_type: 'application/vnd.sentry.items.span.v2+json' }, | ||
| { items: [mockSpan1, mockSpan2, mockSpan3] }, | ||
| ], | ||
| ]); | ||
| }); | ||
|
|
||
| it('handles empty spans array', () => { | ||
| const mockClient = new TestClient(getDefaultTestClientOptions()); | ||
| const dsc: Partial<DynamicSamplingContext> = {}; | ||
|
|
||
| const result = createStreamedSpanEnvelope([], dsc, mockClient); | ||
|
|
||
| expect(result).toEqual([ | ||
| { | ||
| sent_at: expect.any(String), | ||
| }, | ||
| [ | ||
| [ | ||
| { | ||
| content_type: 'application/vnd.sentry.items.span.v2+json', | ||
| item_count: 0, | ||
| type: 'span', | ||
| }, | ||
| { | ||
| items: [], | ||
| }, | ||
| ], | ||
| ], | ||
| ]); | ||
| }); | ||
| }); | ||
| }); | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.