@ogcio/o11y-sdk-node
Version:
Opentelemetry standard instrumentation SDK for NodeJS based project
29 lines (28 loc) • 1.21 kB
JavaScript
import { SpanKind, } from "@opentelemetry/api";
import { SamplingDecision, } from "@opentelemetry/sdk-trace-base";
export class UrlSampler {
_samplerCondition;
_nextSampler;
constructor(samplerCondition, nextSampler) {
this._samplerCondition = samplerCondition ?? [];
this._nextSampler = nextSampler;
}
shouldSample(_context, traceId, _spanName, _spanKind, attributes, _links) {
// Check both http.target and url.path (@fastify/otel) attributes
const url = attributes["http.target"]?.toString() ||
attributes["url.path"]?.toString();
if (url) {
for (const condition of this._samplerCondition) {
if ((condition.type === "equals" && url === condition.url) ||
(condition.type === "endsWith" && url.endsWith(condition.url)) ||
(condition.type === "includes" && url.includes(condition.url))) {
return { decision: SamplingDecision.NOT_RECORD };
}
}
}
return this._nextSampler.shouldSample(_context, traceId, _spanName, _spanKind, attributes, _links);
}
toString() {
return "UrlSampler";
}
}