Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Add ignoreSpans option#17078
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -42,6 +42,7 @@ import { merge } from './utils/merge'; | ||
| import { checkOrSetAlreadyCaught, uuid4 } from './utils/misc'; | ||
| import { parseSampleRate } from './utils/parseSampleRate'; | ||
| import { prepareEvent } from './utils/prepareEvent'; | ||
| import { reparentChildSpans, shouldIgnoreSpan } from './utils/should-ignore-span'; | ||
| import { getActiveSpan, showSpanDropWarning, spanToTraceContext } from './utils/spanUtils'; | ||
| import { rejectedSyncPromise, resolvedSyncPromise, SyncPromise } from './utils/syncpromise'; | ||
| import { convertSpanJsonToTransactionEvent, convertTransactionEventToSpanJson } from './utils/transactionEvent'; | ||
| @@ -1281,36 +1282,67 @@ function processBeforeSend( | ||
| event: Event, | ||
| hint: EventHint, | ||
| ): PromiseLike<Event | null> | Event | null { | ||
| const { beforeSend, beforeSendTransaction, beforeSendSpan } = options; | ||
| const { beforeSend, beforeSendTransaction, beforeSendSpan, ignoreSpans } = options; | ||
| let processedEvent = event; | ||
| if (isErrorEvent(processedEvent) && beforeSend) { | ||
| return beforeSend(processedEvent, hint); | ||
| } | ||
| if (isTransactionEvent(processedEvent)) { | ||
| if (beforeSendSpan) { | ||
| // process root span | ||
| const processedRootSpanJson = beforeSendSpan(convertTransactionEventToSpanJson(processedEvent)); | ||
| if (!processedRootSpanJson) { | ||
| showSpanDropWarning(); | ||
| } else { | ||
| // update event with processed root span values | ||
| processedEvent = merge(event, convertSpanJsonToTransactionEvent(processedRootSpanJson)); | ||
| // Avoid processing if we don't have to | ||
| if (beforeSendSpan || ignoreSpans) { | ||
| // 1. Process root span | ||
| const rootSpanJson = convertTransactionEventToSpanJson(processedEvent); | ||
| // 1.1 If the root span should be ignored, drop the whole transaction | ||
| if (ignoreSpans?.length && shouldIgnoreSpan(rootSpanJson, ignoreSpans)) { | ||
| // dropping the whole transaction! | ||
| return null; | ||
Lms24 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| // process child spans | ||
| // 1.2 If a `beforeSendSpan` callback is defined, process the root span | ||
| if (beforeSendSpan) { | ||
| const processedRootSpanJson = beforeSendSpan(rootSpanJson); | ||
| if (!processedRootSpanJson) { | ||
| showSpanDropWarning(); | ||
| } else { | ||
| // update event with processed root span values | ||
| processedEvent = merge(event, convertSpanJsonToTransactionEvent(processedRootSpanJson)); | ||
| } | ||
| } | ||
| // 2. Process child spans | ||
| if (processedEvent.spans) { | ||
| const processedSpans: SpanJSON[] = []; | ||
| for (const span of processedEvent.spans) { | ||
| const processedSpan = beforeSendSpan(span); | ||
| if (!processedSpan) { | ||
| showSpanDropWarning(); | ||
| processedSpans.push(span); | ||
| const initialSpans = processedEvent.spans; | ||
| for (const span of initialSpans) { | ||
| // 2.a If the child span should be ignored, reparent it to the root span | ||
| if (ignoreSpans?.length && shouldIgnoreSpan(span, ignoreSpans)) { | ||
| reparentChildSpans(initialSpans, span); | ||
| continue; | ||
| } | ||
| // 2.b If a `beforeSendSpan` callback is defined, process the child span | ||
| if (beforeSendSpan) { | ||
| const processedSpan = beforeSendSpan(span); | ||
| if (!processedSpan) { | ||
| showSpanDropWarning(); | ||
| processedSpans.push(span); | ||
| } else { | ||
| processedSpans.push(processedSpan); | ||
| } | ||
| } else { | ||
| processedSpans.push(processedSpan); | ||
| processedSpans.push(span); | ||
| } | ||
| } | ||
| const droppedSpans = processedEvent.spans.length - processedSpans.length; | ||
| if (droppedSpans) { | ||
| client.recordDroppedEvent('before_send', 'span', droppedSpans); | ||
| } | ||
| processedEvent.spans = processedSpans; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import type { ClientOptions } from '../types-hoist/options'; | ||
| import type { SpanJSON } from '../types-hoist/span'; | ||
| import { isMatchingPattern } from './string'; | ||
| /** | ||
| * Check if a span should be ignored based on the ignoreSpans configuration. | ||
| */ | ||
| export function shouldIgnoreSpan( | ||
| span: Pick<SpanJSON, 'description' | 'op'>, | ||
| ignoreSpans: Required<ClientOptions>['ignoreSpans'], | ||
| ): boolean { | ||
| if (!ignoreSpans?.length || !span.description) { | ||
| return false; | ||
| } | ||
Lms24 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| for (const pattern of ignoreSpans) { | ||
| if (isStringOrRegExp(pattern)) { | ||
| if (isMatchingPattern(span.description, pattern)) { | ||
| return true; | ||
| } | ||
| continue; | ||
| } | ||
| if (!pattern.name && !pattern.op) { | ||
| continue; | ||
| } | ||
| const nameMatches = pattern.name ? isMatchingPattern(span.description, pattern.name) : true; | ||
| const opMatches = pattern.op ? span.op && isMatchingPattern(span.op, pattern.op) : true; | ||
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. Bug: Span Ignoring Fails for Empty OperationsThe | ||
| // This check here is only correct because we can guarantee that we ran `isMatchingPattern` | ||
| // for at least one of `nameMatches` and `opMatches`. So in contrary to how this looks, | ||
| // not both op and name actually have to match. This is the most efficient way to check | ||
| // for all combinations of name and op patterns. | ||
| if (nameMatches && opMatches) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| /** | ||
| * Takes a list of spans, and a span that was dropped, and re-parents the child spans of the dropped span to the parent of the dropped span, if possible. | ||
| * This mutates the spans array in place! | ||
| */ | ||
| export function reparentChildSpans(spans: SpanJSON[], dropSpan: SpanJSON): void { | ||
| const droppedSpanParentId = dropSpan.parent_span_id; | ||
| const droppedSpanId = dropSpan.span_id; | ||
| // This should generally not happen, as we do not apply this on root spans | ||
| // but to be safe, we just bail in this case | ||
| if (!droppedSpanParentId) { | ||
| return; | ||
| } | ||
| for (const span of spans) { | ||
| if (span.parent_span_id === droppedSpanId) { | ||
| span.parent_span_id = droppedSpanParentId; | ||
| } | ||
| } | ||
| } | ||
| function isStringOrRegExp(value: unknown): value is string | RegExp { | ||
| return typeof value === 'string' || value instanceof RegExp; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.