opentelemetry-js
opentelemetry-js copied to clipboard
Expose internal opentelemetry metrics
When setting up and operating a system that uses opentelemetry components, it is useful to have some insight into the metrics collection system itself, to see how many spans are being collected, and whether those spans are successfully delivered to collectors.
For example, I hand-created a little class that counts the number of spans & span batches exported by a SpanExporter and records those into metrics:
class MonitoredSpanExporter implements SpanExporter {
_delegate: SpanExporter;
constructor(delegate: SpanExporter) {
this._delegate = delegate;
}
export(
spans: ReadableSpan[],
resultCallback: (result: ExportResult) => void,
): void {
metrics.span_batches_exported_total.inc();
metrics.spans_exported_total.inc(spans.length);
this._delegate.export(spans, (result: ExportResult) => {
resultCallback(result);
if (result === ExportResult.FAILED_NOT_RETRYABLE) {
metrics.span_batches_exported_error_total.inc();
metrics.spans_exported_error_total.inc(spans.length);
}
});
}
shutdown(): Promise<void> {
return collectorTraceExporter.shutdown();
}
}
I wrap this around my span exporter implementation to count the number of spans and span batches, and the number of errors. I can then use these metrics to alert if spans are not being successfully sent.
I think a version of this could be provided as part of the library which works for both span and metrics collection and export. Likely it would export to opentelemetry's own metrics subsystem (I'm still using prom-client).