Skip to content

Commit 8129f1e

Browse files
committed
chore: object constructor for sampler
1 parent afca80b commit 8129f1e

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

examples/simple_chat/instrumentation.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import { OpenAIInstrumentation } from "@opentelemetry/instrumentation-openai";
22
import { SamplingDecision } from "@opentelemetry/sdk-trace-base";
3-
import { Judgeval, ExperimentalSpanFilterSampler, type NodeTracer } from "judgeval";
3+
import {
4+
ExperimentalSpanFilterSampler,
5+
Judgeval,
6+
type NodeTracer,
7+
} from "judgeval";
48

59
export const client = Judgeval.create();
610

711
const initPromise = client.nodeTracer.create({
812
projectName: "auto_instrumentation_example",
913
instrumentations: [new OpenAIInstrumentation()],
10-
sampler: new ExperimentalSpanFilterSampler((span) => {
11-
console.log(span);
12-
return span.resource.attributes["service.name"] === "judgeval"
13-
? SamplingDecision.RECORD_AND_SAMPLED
14-
: SamplingDecision.NOT_RECORD;
14+
sampler: new ExperimentalSpanFilterSampler({
15+
filter: (span) => {
16+
console.log(span);
17+
return span.resource.attributes["service.name"] === "judgeval"
18+
? SamplingDecision.RECORD_AND_SAMPLED
19+
: SamplingDecision.NOT_RECORD;
20+
},
1521
}),
1622
});
1723

src/tracer/samplers/ExperimentalSpanFilterSampler.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import { Logger } from "../../utils";
1515
// @ref https://github.com/open-telemetry/opentelemetry-js/blob/main/api/src/trace/context-utils.ts
1616
const SPAN_KEY_NAME = "OpenTelemetry Context Key SPAN" as const;
1717

18+
export interface ExperimentalSpanFilterSamplerConfig {
19+
filter: (span: ReadableSpan) => SamplingDecision;
20+
}
1821
export class ExperimentalSpanFilterSampler implements Sampler {
19-
constructor(
20-
private readonly filterFn: (span: ReadableSpan) => SamplingDecision,
21-
) {}
22+
constructor(private readonly config: ExperimentalSpanFilterSamplerConfig) {}
2223

2324
shouldSample(
2425
context: Context,
2526
_traceId: string,
2627
_spanName: string,
2728
_spanKind: SpanKind,
2829
_attributes: Attributes,
29-
_links: Link[],
30+
_links: Link[]
3031
): SamplingResult {
3132
// @ts-expect-error - not intended to be public API but exists
3233
const currentContext = context._currentContext as Map<symbol, unknown>;
@@ -37,22 +38,22 @@ export class ExperimentalSpanFilterSampler implements Sampler {
3738
}
3839

3940
const spanKey = Array.from(currentContext.keys()).find(
40-
(key) => key.description === SPAN_KEY_NAME,
41+
(key) => key.description === SPAN_KEY_NAME
4142
);
4243

4344
// // @ref https://github.com/open-telemetry/opentelemetry-js/blob/main/api/src/context/context.ts
4445
const span = spanKey ? currentContext.get(spanKey) : undefined;
4546
try {
46-
const decision = this.filterFn(span as ReadableSpan);
47+
const decision = this.config.filter(span as ReadableSpan);
4748
if (decision === SamplingDecision.NOT_RECORD) {
4849
Logger.info(
49-
`[ExperimentalSpanFilterSampler] Dropping span because it does not match the filter function.`,
50+
`[ExperimentalSpanFilterSampler] Dropping span because it does not match the filter function.`
5051
);
5152
}
5253
return { decision };
5354
} catch (error) {
5455
Logger.error(
55-
`[ExperimentalSpanFilterSampler] Error filtering span: ${error}`,
56+
`[ExperimentalSpanFilterSampler] Error filtering span: ${error}`
5657
);
5758
return { decision: SamplingDecision.RECORD_AND_SAMPLED };
5859
}

0 commit comments

Comments
 (0)