-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(node-core): Add POtel server-side span streaming implementation #19741
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
Lms24
wants to merge
9
commits into
lms/feat-span-first
Choose a base branch
from
lms/feat-span-first-node-otel
base: lms/feat-span-first
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
9 commits
Select commit
Hold shift + click to select a range
52c06cb
feat(core,node-core,node): Add server-side span streaming implementation
Lms24 0a989e3
add new files
Lms24 e3e9a86
use `getDefaultIntegrations` to add/not add span streaming integration
Lms24 17df959
bump size limits
Lms24 e8c9f04
fix missing exports
Lms24 3978348
fix unused import
Lms24 f280ff3
fix defaultIntegrations
Lms24 770fd4c
refactor: Extract shared spanStreamingIntegration logic to reduce dup…
cursoragent a912ccd
format
Lms24 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
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
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
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,53 @@ | ||
| import type { Client } from '../client'; | ||
| import type { IntegrationFn } from '../types-hoist/integration'; | ||
| import type { Span } from '../types-hoist/span'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| import { defineIntegration } from '../integration'; | ||
| import { isStreamedBeforeSendSpanCallback } from '../tracing/spans/beforeSendSpan'; | ||
| import { captureSpan } from '../tracing/spans/captureSpan'; | ||
| import { hasSpanStreamingEnabled } from '../tracing/spans/hasSpanStreamingEnabled'; | ||
| import { SpanBuffer } from '../tracing/spans/spanBuffer'; | ||
| import { debug } from '../utils/debug-logger'; | ||
| import { spanIsSampled } from '../utils/spanUtils'; | ||
|
|
||
| /** | ||
| * Shared setup logic for span streaming integrations. | ||
| * Returns a SpanBuffer if setup succeeds, or undefined if span streaming cannot be enabled. | ||
| */ | ||
| export function setupSpanStreaming(client: Client): SpanBuffer | undefined { | ||
| const initialMessage = 'SpanStreaming integration requires'; | ||
| const fallbackMsg = 'Falling back to static trace lifecycle.'; | ||
|
|
||
| if (!hasSpanStreamingEnabled(client)) { | ||
| DEBUG_BUILD && debug.warn(`${initialMessage} \`traceLifecycle\` to be set to "stream"! ${fallbackMsg}`); | ||
| return undefined; | ||
| } | ||
|
|
||
| const beforeSendSpan = client.getOptions().beforeSendSpan; | ||
| if (beforeSendSpan && !isStreamedBeforeSendSpanCallback(beforeSendSpan)) { | ||
| client.getOptions().traceLifecycle = 'static'; | ||
| DEBUG_BUILD && debug.warn(`${initialMessage} a beforeSendSpan callback using \`withStreamedSpan\`! ${fallbackMsg}`); | ||
| return undefined; | ||
| } | ||
|
|
||
| const buffer = new SpanBuffer(client); | ||
|
|
||
| client.on('afterSpanEnd', (span: Readonly<Span>) => { | ||
| if (!spanIsSampled(span)) { | ||
| return; | ||
| } | ||
| buffer.add(captureSpan(span, client)); | ||
| }); | ||
|
|
||
| return buffer; | ||
| } | ||
|
|
||
| export const spanStreamingIntegration = defineIntegration(() => { | ||
| return { | ||
| name: 'SpanStreaming', | ||
|
|
||
| setup(client) { | ||
| setupSpanStreaming(client); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,135 @@ | ||
| import * as SentryCore from '../../src'; | ||
| import { debug } from '../../src'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { spanStreamingIntegration } from '../../src/integrations/spanStreaming'; | ||
| import { TestClient, getDefaultTestClientOptions } from '../mocks/client'; | ||
|
|
||
| const mockSpanBufferInstance = vi.hoisted(() => ({ | ||
| flush: vi.fn(), | ||
| add: vi.fn(), | ||
| drain: vi.fn(), | ||
| })); | ||
|
|
||
| const MockSpanBuffer = vi.hoisted(() => { | ||
| return vi.fn(() => mockSpanBufferInstance); | ||
| }); | ||
|
|
||
| vi.mock('../../src/tracing/spans/spanBuffer', async () => { | ||
| const original = await vi.importActual('../../src/tracing/spans/spanBuffer'); | ||
| return { | ||
| ...original, | ||
| SpanBuffer: MockSpanBuffer, | ||
| }; | ||
| }); | ||
|
|
||
| describe('spanStreamingIntegration (core)', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('has the correct name and setup hook', () => { | ||
| const integration = spanStreamingIntegration(); | ||
| expect(integration.name).toBe('SpanStreaming'); | ||
| // eslint-disable-next-line @typescript-eslint/unbound-method | ||
| expect(integration.setup).toBeDefined(); | ||
| }); | ||
|
|
||
| it('logs a warning if traceLifecycle is not set to "stream"', () => { | ||
| const debugSpy = vi.spyOn(debug, 'warn').mockImplementation(() => {}); | ||
| const client = new TestClient({ | ||
| ...getDefaultTestClientOptions(), | ||
| dsn: 'https://username@domain/123', | ||
| integrations: [spanStreamingIntegration()], | ||
| traceLifecycle: 'static', | ||
| }); | ||
|
|
||
| SentryCore.setCurrentClient(client); | ||
| client.init(); | ||
|
|
||
| expect(debugSpy).toHaveBeenCalledWith( | ||
| 'SpanStreaming integration requires `traceLifecycle` to be set to "stream"! Falling back to static trace lifecycle.', | ||
| ); | ||
| debugSpy.mockRestore(); | ||
|
|
||
| expect(client.getOptions().traceLifecycle).toBe('static'); | ||
| }); | ||
|
|
||
| it('falls back to static trace lifecycle if beforeSendSpan is not compatible with span streaming', () => { | ||
| const debugSpy = vi.spyOn(debug, 'warn').mockImplementation(() => {}); | ||
| const client = new TestClient({ | ||
| ...getDefaultTestClientOptions(), | ||
| dsn: 'https://username@domain/123', | ||
| integrations: [spanStreamingIntegration()], | ||
| traceLifecycle: 'stream', | ||
| beforeSendSpan: (span: SentryCore.SpanJSON) => span, | ||
| }); | ||
|
|
||
| SentryCore.setCurrentClient(client); | ||
| client.init(); | ||
|
|
||
| expect(debugSpy).toHaveBeenCalledWith( | ||
| 'SpanStreaming integration requires a beforeSendSpan callback using `withStreamedSpan`! Falling back to static trace lifecycle.', | ||
| ); | ||
| debugSpy.mockRestore(); | ||
|
|
||
| expect(client.getOptions().traceLifecycle).toBe('static'); | ||
| }); | ||
|
|
||
| it('sets up buffer when traceLifecycle is "stream"', () => { | ||
| const client = new TestClient({ | ||
| ...getDefaultTestClientOptions(), | ||
| dsn: 'https://username@domain/123', | ||
| integrations: [spanStreamingIntegration()], | ||
| traceLifecycle: 'stream', | ||
| }); | ||
|
|
||
| SentryCore.setCurrentClient(client); | ||
| client.init(); | ||
|
|
||
| expect(MockSpanBuffer).toHaveBeenCalledWith(client); | ||
| expect(client.getOptions().traceLifecycle).toBe('stream'); | ||
| }); | ||
|
|
||
| it('enqueues a span into the buffer when the span ends', () => { | ||
| const client = new TestClient({ | ||
| ...getDefaultTestClientOptions(), | ||
| dsn: 'https://username@domain/123', | ||
| integrations: [spanStreamingIntegration()], | ||
| traceLifecycle: 'stream', | ||
| tracesSampleRate: 1, | ||
| }); | ||
|
|
||
| SentryCore.setCurrentClient(client); | ||
| client.init(); | ||
|
|
||
| const span = new SentryCore.SentrySpan({ name: 'test', sampled: true }); | ||
| client.emit('afterSpanEnd', span); | ||
|
|
||
| expect(mockSpanBufferInstance.add).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| _segmentSpan: span, | ||
| trace_id: span.spanContext().traceId, | ||
| span_id: span.spanContext().spanId, | ||
| name: 'test', | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('does not enqueue a span into the buffer when the span is not sampled', () => { | ||
| const client = new TestClient({ | ||
| ...getDefaultTestClientOptions(), | ||
| dsn: 'https://username@domain/123', | ||
| integrations: [spanStreamingIntegration()], | ||
| traceLifecycle: 'stream', | ||
| tracesSampleRate: 1, | ||
| }); | ||
|
|
||
| SentryCore.setCurrentClient(client); | ||
| client.init(); | ||
|
|
||
| const span = new SentryCore.SentrySpan({ name: 'test', sampled: false }); | ||
| client.emit('afterSpanEnd', span); | ||
|
|
||
| expect(mockSpanBufferInstance.add).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.