@ogcio/o11y-sdk-node
Version:
Opentelemetry standard instrumentation SDK for NodeJS based project
42 lines (41 loc) • 1.26 kB
JavaScript
/**
* A SpanProcessor that provides a hook to execute custom logic
* just before a span is ended.
*
* This allows for capturing or modifying span data at the precise moment
* the span is about to be finished, while it is still writable.
*
* Usage:
* Extend this class and implement the onEnding method with custom logic.
* Override the onStart method if needed to set up any required state.
* The onEnding method will be called just before the span's end() method
* is invoked.
*
* Example:
* ```typescript
* class CustomSpanProcessor extends OnEndingHookSpanProcessor {
* onEnding(span: Span, context: Context): void {
* // Custom logic to execute before the span ends
* span.setAttribute("custom.attribute", "value");
* }
* }
* ```
*/
export class OnEndingHookSpanProcessor {
onStart(span, _context) {
// Store the original end method
const originalEnd = span.end.bind(span);
// Wrap the end method to inject custom logic before ending the span
span.end = (endTime) => {
this.onEnding(span);
originalEnd(endTime);
};
}
onEnd(_span) { }
forceFlush() {
return Promise.resolve();
}
shutdown() {
return Promise.resolve();
}
}