UNPKG

@gati-framework/observability

Version:

Observability stack for Gati framework - Prometheus, Grafana, Loki, and Tracing

55 lines (54 loc) 1.48 kB
import { DistributedTracing } from '../tracing/distributed-tracing.js'; import * as api from '@opentelemetry/api'; class SpanAdapter { span; constructor(span) { this.span = span; } get spanId() { return this.span.spanContext().spanId; } get traceId() { return this.span.spanContext().traceId; } setAttribute(key, value) { this.span.setAttribute(key, value); } addEvent(name, attributes) { this.span.addEvent(name, attributes); } recordException(error) { this.span.recordException(error); } setStatus(status) { this.span.setStatus({ code: status.code === 'OK' ? api.SpanStatusCode.OK : api.SpanStatusCode.ERROR, message: status.message, }); } end() { this.span.end(); } } export class OpenTelemetryAdapter { tracing; constructor(config) { this.tracing = new DistributedTracing(config); } createSpan(name, attributes) { const span = this.tracing.createSpan(name, attributes); return new SpanAdapter(span); } async withSpan(name, fn, attributes) { return this.tracing.withSpan(name, async (otelSpan) => { const span = new SpanAdapter(otelSpan); return fn(span); }, attributes); } getTraceContext() { return this.tracing.getTraceContext(); } getDistributedTracing() { return this.tracing; } }