-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudflare): propagate tracing context through Queue messages #22303
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
arthurfiorette
wants to merge
4
commits into
getsentry:develop
Choose a base branch
from
arthurfiorette:arthurfiorette/queue-links
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
4 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 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,7 @@ | ||||||||||||||||||||||||||||||||||
| import type { MessageSendRequest, Queue, QueueSendBatchOptions, QueueSendOptions } from '@cloudflare/workers-types'; | ||||||||||||||||||||||||||||||||||
| import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/core'; | ||||||||||||||||||||||||||||||||||
| import type { Span } from '@sentry/core'; | ||||||||||||||||||||||||||||||||||
| import { getTraceData, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/core'; | ||||||||||||||||||||||||||||||||||
| import { addQueueTraceContext } from '../../utils/queueTraceMeta'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const ORIGIN = 'auto.faas.cloudflare.queue'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -9,7 +11,7 @@ function startPublishSpan<T>( | |||||||||||||||||||||||||||||||||
| bodySize: number | undefined; | ||||||||||||||||||||||||||||||||||
| messageCount?: number; | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| callback: () => T, | ||||||||||||||||||||||||||||||||||
| callback: (span: Span) => T, | ||||||||||||||||||||||||||||||||||
| ): T { | ||||||||||||||||||||||||||||||||||
| const { bindingName, bodySize, messageCount } = options; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -56,23 +58,46 @@ function getBodySize(body: unknown): number | undefined { | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function getBatchBodySize(messages: MessageSendRequest[]): number | undefined { | ||||||||||||||||||||||||||||||||||
| return messages.reduce<number | undefined>((acc, message) => { | ||||||||||||||||||||||||||||||||||
| const size = getBodySize(message.body); | ||||||||||||||||||||||||||||||||||
| if (size === undefined) { | ||||||||||||||||||||||||||||||||||
| return acc; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return (acc ?? 0) + size; | ||||||||||||||||||||||||||||||||||
| }, undefined); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Wraps a Queue producer binding to create `queue.publish` spans on | ||||||||||||||||||||||||||||||||||
| * `send` and `sendBatch` calls. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * The queue's own name is not available on the binding object, so we use | ||||||||||||||||||||||||||||||||||
| * the env binding key (e.g. `MY_QUEUE`) as `messaging.destination.name`. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| export function instrumentQueueProducer<T extends Queue>(queue: T, bindingName: string): T { | ||||||||||||||||||||||||||||||||||
| export function instrumentQueueProducer<T extends Queue>( | ||||||||||||||||||||||||||||||||||
| queue: T, | ||||||||||||||||||||||||||||||||||
| bindingName: string, | ||||||||||||||||||||||||||||||||||
| enableTracePropagation = false, | ||||||||||||||||||||||||||||||||||
| ): T { | ||||||||||||||||||||||||||||||||||
| return new Proxy(queue, { | ||||||||||||||||||||||||||||||||||
| get(target, prop, receiver) { | ||||||||||||||||||||||||||||||||||
| if (prop === 'send') { | ||||||||||||||||||||||||||||||||||
| const original = Reflect.get(target, prop, receiver) as Queue['send']; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return function (this: unknown, message: unknown, options?: QueueSendOptions): ReturnType<Queue['send']> { | ||||||||||||||||||||||||||||||||||
| return startPublishSpan({ bindingName, bodySize: getBodySize(message) }, () => | ||||||||||||||||||||||||||||||||||
| Reflect.apply(original, target, [message, options]), | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| return startPublishSpan({ bindingName, bodySize: getBodySize(message) }, span => { | ||||||||||||||||||||||||||||||||||
| // Read trace data here so the carrier points to queue.publish, not its parent span. | ||||||||||||||||||||||||||||||||||
| const tracedMessage = enableTracePropagation | ||||||||||||||||||||||||||||||||||
| ? addQueueTraceContext(message, getTraceData()['sentry-trace']) | ||||||||||||||||||||||||||||||||||
| : message; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (enableTracePropagation) { | ||||||||||||||||||||||||||||||||||
| span.setAttribute('messaging.message.body.size', getBodySize(tracedMessage)); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+89
to
+97
Member
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. So, when trace propagation is enabled, we're calling Consider something like this:
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return Reflect.apply(original, target, [tracedMessage, options]); | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -84,17 +109,25 @@ export function instrumentQueueProducer<T extends Queue>(queue: T, bindingName: | |||||||||||||||||||||||||||||||||
| options?: QueueSendBatchOptions, | ||||||||||||||||||||||||||||||||||
| ): ReturnType<Queue['sendBatch']> { | ||||||||||||||||||||||||||||||||||
| const messageArray = Array.from(messages); | ||||||||||||||||||||||||||||||||||
| const totalBodySize = messageArray.reduce<number | undefined>((acc, m) => { | ||||||||||||||||||||||||||||||||||
| const size = getBodySize(m.body); | ||||||||||||||||||||||||||||||||||
| if (size === undefined) { | ||||||||||||||||||||||||||||||||||
| return acc; | ||||||||||||||||||||||||||||||||||
| const totalBodySize = getBatchBodySize(messageArray); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return startPublishSpan({ bindingName, bodySize: totalBodySize, messageCount: messageArray.length }, span => { | ||||||||||||||||||||||||||||||||||
| if (!enableTracePropagation) { | ||||||||||||||||||||||||||||||||||
| return Reflect.apply(original, target, [messageArray, options]); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return (acc ?? 0) + size; | ||||||||||||||||||||||||||||||||||
| }, undefined); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return startPublishSpan({ bindingName, bodySize: totalBodySize, messageCount: messageArray.length }, () => | ||||||||||||||||||||||||||||||||||
| Reflect.apply(original, target, [messageArray, options]), | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| // Every entry was published by this batch span, so they intentionally share one producer context. | ||||||||||||||||||||||||||||||||||
| const sentryTrace = getTraceData()['sentry-trace']; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const tracedMessages = messageArray.map(message => ({ | ||||||||||||||||||||||||||||||||||
| ...message, | ||||||||||||||||||||||||||||||||||
| body: addQueueTraceContext(message.body, sentryTrace), | ||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| span.setAttribute('messaging.message.body.size', getBatchBodySize(tracedMessages)); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return Reflect.apply(original, target, [tracedMessages, options]); | ||||||||||||||||||||||||||||||||||
|
cursor[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
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.
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.
This feels like it might be a weird fit. A
previous_tracevalue here indicates that the link is part of a sequential chain of traces, for example a series of browser interactions that a user might perform. In the UI, it's going to be presented with that kind of sequential semantics.However, this is more of a "fan out" relationship, not previous/next relationship, so I don't think
previous_tracemakes the most sense here. It might be better to just leave the type off, and let it be a plain old link.@andreiborza @mydea Do you think I'm misreading this? What is your opinion on the semantics here?