Created
January 30, 2025 15:18
-
-
Save gunzip/0ff6907ad8718bf25e095464829052c7 to your computer and use it in GitHub Desktop.
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
import { SpanProcessor, Span, ReadableSpan } from '@opentelemetry/sdk-trace-base'; | |
class RemoveQueryParamsProcessor implements SpanProcessor { | |
onStart(span: Span): void { | |
// No action on span start for this processor | |
} | |
onEnd(span: ReadableSpan): void { | |
// Check if the span has an attribute that represents a URL | |
const url = span.attributes['http.url'] as string | undefined; | |
if (url) { | |
// Remove query parameters from the URL | |
const urlWithoutQuery = url.split('?')[0]; | |
// Update the span's attributes with the new URL | |
span.setAttribute('http.url', urlWithoutQuery); | |
} | |
} | |
shutdown(): Promise<void> { | |
return Promise.resolve(); | |
} | |
forceFlush(): Promise<void> { | |
return Promise.resolve(); | |
} | |
} | |
class DisableSamplingByTagProcessor implements SpanProcessor { | |
onStart(span: Span): void { | |
// We check for sampling here because we want to modify the sampling behavior before span processing continues | |
const samplingEnabled = span.attributes['sampling.enabled'] as string | undefined; | |
if (samplingEnabled === 'false') { | |
// In OpenTelemetry, setting sampling decision to always sample means setting the sampling priority to 1 | |
span.setAttribute('sampling.priority', 1); | |
} | |
} | |
onEnd(span: ReadableSpan): void { | |
// No action needed on end for this specific use case since we've already set the sampling in onStart | |
} | |
shutdown(): Promise<void> { | |
return Promise.resolve(); | |
} | |
forceFlush(): Promise<void> { | |
return Promise.resolve(); | |
} | |
} | |
// You would typically register this processor with a TracerProvider or a SpanExporter like so: | |
// tracerProvider.addSpanProcessor(new DisableSamplingByTagProcessor()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment