forked from evanderkoogh/otel-cf-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampling.ts
26 lines (21 loc) · 911 Bytes
/
sampling.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { TraceFlags, SpanStatusCode } from '@opentelemetry/api'
import { ReadableSpan } from '@opentelemetry/sdk-trace-base'
export interface LocalTrace {
readonly traceId: string
readonly localRootSpan: ReadableSpan
readonly spans: ReadableSpan[]
}
export type TailSampleFn = (traceInfo: LocalTrace) => boolean
export function multiTailSampler(samplers: TailSampleFn[]): TailSampleFn {
return (traceInfo) => {
return samplers.reduce((result, sampler) => result || sampler(traceInfo), false)
}
}
export const isHeadSampled: TailSampleFn = (traceInfo) => {
const localRootSpan = traceInfo.localRootSpan as unknown as ReadableSpan
return localRootSpan.spanContext().traceFlags === TraceFlags.SAMPLED
}
export const isRootErrorSpan: TailSampleFn = (traceInfo) => {
const localRootSpan = traceInfo.localRootSpan as unknown as ReadableSpan
return localRootSpan.status.code === SpanStatusCode.ERROR
}