-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Add traceLifecycle option and beforeSendSpan compatibility utilities
#19120
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
3
commits into
lms/feat-span-first
Choose a base branch
from
lms/span-first-pr2-trace-lifecycle
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.
+107
−11
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
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,41 @@ | ||
| import type { BeforeSendStramedSpanCallback, ClientOptions } from '../types-hoist/options'; | ||
| import type { StreamedSpanJSON } from '../types-hoist/span'; | ||
| import { addNonEnumerableProperty } from './object'; | ||
|
|
||
| /** | ||
| * A wrapper to use the new span format in your `beforeSendSpan` callback. | ||
| * | ||
| * When using `traceLifecycle: 'stream'`, wrap your callback with this function | ||
| * to receive and return {@link StreamedSpanJSON} instead of the standard {@link SpanJSON}. | ||
| * | ||
| * @example | ||
| * | ||
| * Sentry.init({ | ||
| * traceLifecycle: 'stream', | ||
| * beforeSendSpan: withStreamedSpan((span) => { | ||
| * // span is of type StreamedSpanJSON | ||
| * return span; | ||
| * }), | ||
| * }); | ||
| * | ||
| * @param callback - The callback function that receives and returns a {@link StreamedSpanJSON}. | ||
| * @returns A callback that is compatible with the `beforeSendSpan` option when using `traceLifecycle: 'stream'`. | ||
| */ | ||
| export function withStreamedSpan( | ||
| callback: (span: StreamedSpanJSON) => StreamedSpanJSON, | ||
| ): BeforeSendStramedSpanCallback { | ||
| addNonEnumerableProperty(callback, '_streamed', true); | ||
| return callback; | ||
| } | ||
|
|
||
| /** | ||
| * Typesafe check to identify if a `beforeSendSpan` callback expects the streamed span JSON format. | ||
| * | ||
| * @param callback - The `beforeSendSpan` callback to check. | ||
| * @returns `true` if the callback was wrapped with {@link withStreamedSpan}. | ||
| */ | ||
| export function isStreamedBeforeSendSpanCallback( | ||
| callback: ClientOptions['beforeSendSpan'], | ||
| ): callback is BeforeSendStramedSpanCallback { | ||
| return !!callback && '_streamed' in callback && !!callback._streamed; | ||
| } |
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,26 @@ | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { withStreamedSpan } from '../../../src'; | ||
| import { isStreamedBeforeSendSpanCallback } from '../../../src/utils/beforeSendSpan'; | ||
|
|
||
| describe('beforeSendSpan for span streaming', () => { | ||
| describe('withStreamedSpan', () => { | ||
| it('should be able to modify the span', () => { | ||
| const beforeSendSpan = vi.fn(); | ||
| const wrapped = withStreamedSpan(beforeSendSpan); | ||
| expect(wrapped._streamed).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isStreamedBeforeSendSpanCallback', () => { | ||
| it('returns true if the callback is wrapped with withStreamedSpan', () => { | ||
| const beforeSendSpan = vi.fn(); | ||
| const wrapped = withStreamedSpan(beforeSendSpan); | ||
| expect(isStreamedBeforeSendSpanCallback(wrapped)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false if the callback is not wrapped with withStreamedSpan', () => { | ||
| const beforeSendSpan = vi.fn(); | ||
| expect(isStreamedBeforeSendSpanCallback(beforeSendSpan)).toBe(false); | ||
| }); | ||
| }); | ||
| }); | ||
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.
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.
M: As only true is allowed here, we might need to set this type to
true | undefinedin case someone enabled exactOptionalPropertyTypes.Uh oh!
There was an error while loading. Please reload this page.
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.
You're right. However, users shouldn't ever set this but just use the
withStreamedSpanhelper instead. That being said, it is public API, so we could widen the type enough to make this possible.Tbh, this
withStreamedSpanthing isn't ideal but maybe I could pick your brain a bit for a nicer solution? I was contemplating discrimanted union typing viatraceLifecyclebut couldn't get it to work without breaking changes for existing users. Maybe you have some ideas how to fix that.A third option would be to introduce a
beforeSendStreamedSpanAPI for the time span streaming is opt in. Also not ideal because it's again additional API with no great migration path.Def open for other ideas!