-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): Add spanToJSON()
method to get span properties
#10074
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import type { Span, SpanTimeInput, TraceContext } from '@sentry/types'; | ||
import type { Span, SpanJSON, SpanTimeInput, TraceContext } from '@sentry/types'; | ||
import { dropUndefinedKeys, generateSentryTraceHeader, timestampInSeconds } from '@sentry/utils'; | ||
import type { Span as SpanClass } from '../tracing/span'; | ||
|
||
/** | ||
* Convert a span to a trace context, which can be sent as the `trace` context in an event. | ||
*/ | ||
export function spanToTraceContext(span: Span): TraceContext { | ||
const { data, description, op, parent_span_id, span_id, status, tags, trace_id, origin } = span.toJSON(); | ||
const { spanId: span_id, traceId: trace_id } = span; | ||
const { data, description, op, parent_span_id, status, tags, origin } = spanToJSON(span); | ||
|
||
return dropUndefinedKeys({ | ||
data, | ||
|
@@ -54,3 +56,35 @@ function ensureTimestampInSeconds(timestamp: number): number { | |
const isMs = timestamp > 9999999999; | ||
return isMs ? timestamp / 1000 : timestamp; | ||
} | ||
|
||
/** | ||
* Convert a span to a JSON representation. | ||
* Note that all fields returned here are optional and need to be guarded against. | ||
* | ||
* Note: Because of this, we currently have a circular type dependency (which we opted out of in package.json). | ||
* This is not avoidable as we need `spanToJSON` in `spanUtils.ts`, which in turn is needed by `span.ts` for backwards compatibility. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lforst added this comment here for the future! |
||
* And `spanToJSON` needs the Span class from `span.ts` to check here. | ||
* TODO v8: When we remove the deprecated stuff from `span.ts`, we can remove the circular dependency again. | ||
*/ | ||
export function spanToJSON(span: Span): Partial<SpanJSON> { | ||
if (spanIsSpanClass(span)) { | ||
return span.getSpanJSON(); | ||
} | ||
|
||
// Fallback: We also check for `.toJSON()` here... | ||
// eslint-disable-next-line deprecation/deprecation | ||
if (typeof span.toJSON === 'function') { | ||
// eslint-disable-next-line deprecation/deprecation | ||
return span.toJSON(); | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
/** | ||
* Sadly, due to circular dependency checks we cannot actually import the Span class here and check for instanceof. | ||
* :( So instead we approximate this by checking if it has the `getSpanJSON` method. | ||
*/ | ||
function spanIsSpanClass(span: Span): span is SpanClass { | ||
return typeof (span as SpanClass).getSpanJSON === 'function'; | ||
} |
This file contains 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 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 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 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 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 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.
Circ deps (even types) are usually very smelly. Is this 100% necessary?
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.
yes, it is - as we need the util in
spanUtils
, and we needspanUtils
inspan
, and for the util we need the span class fromspan
馃槵 at least I couldn't think of a way to make this work properly :( we can remove it in v8 though, then we don't need to import fromspanUtils
inspan
anymore!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.
Alright then! Let's add a comment/todo somewhere then to remove this opt-out again (if you also think that it's worth keeping in general in the future).
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.
(or open for other ideas, of course)