@ogcio/o11y-sdk-node
Version:
Opentelemetry standard instrumentation SDK for NodeJS based project
53 lines (47 loc) • 1.38 kB
text/typescript
import { Attributes, Context, Link, SpanKind } from "@opentelemetry/api";
import {
Sampler,
SamplingDecision,
SamplingResult,
} from "@opentelemetry/sdk-trace-base";
import { SamplerCondition } from "./index.js";
export class UrlSampler implements Sampler {
private _samplerCondition: SamplerCondition[];
private _nextSampler: Sampler;
constructor(samplerCondition: SamplerCondition[] = [], nextSampler: Sampler) {
this._samplerCondition = samplerCondition;
this._nextSampler = nextSampler;
}
shouldSample(
_context: Context,
traceId: string,
_spanName: string,
_spanKind: SpanKind,
attributes: Attributes,
_links: Link[],
): SamplingResult {
const url: string | undefined = attributes["http.target"]?.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(): string {
return "UrlSampler";
}
}