UNPKG

@ogcio/o11y-sdk-node

Version:

Opentelemetry standard instrumentation SDK for NodeJS based project

40 lines (36 loc) 994 B
import { Context } from "@opentelemetry/api"; import { ReadableSpan, Span, SpanProcessor, } from "@opentelemetry/sdk-trace-base"; import { SignalAttributeValue } from "../index.js"; export class EnrichSpanProcessor implements SpanProcessor { private _spanAttributes: | Record<string, SignalAttributeValue | (() => SignalAttributeValue)> | undefined; constructor( spanAttributes?: Record< string, SignalAttributeValue | (() => SignalAttributeValue) >, ) { this._spanAttributes = spanAttributes; } forceFlush(): Promise<void> { return Promise.resolve(); } onStart(span: Span, _context: Context): void { if (this._spanAttributes != undefined) { for (const [key, value] of Object.entries(this._spanAttributes)) { span.setAttribute(key, typeof value === "function" ? value() : value); } } } onEnd(_span: ReadableSpan): void { return; } shutdown(): Promise<void> { return Promise.resolve(); } }