@@ -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
1616const SPAN_KEY_NAME = "OpenTelemetry Context Key SPAN" as const ;
1717
18+ export interface ExperimentalSpanFilterSamplerConfig {
19+ filter : ( span : ReadableSpan ) => SamplingDecision ;
20+ }
1821export 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