-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(sdk-trace): implement span processor metrics #6504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
874d2ff
503f8cd
a05cf07
7e488af
5bda843
3263924
39678b9
150b366
19f0a8a
ce888e6
9d2882a
1c6f8e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| */ | ||
|
|
||
| import type { Context } from '@opentelemetry/api'; | ||
| import { context, diag, TraceFlags } from '@opentelemetry/api'; | ||
| import { context, createNoopMeter, diag, TraceFlags } from '@opentelemetry/api'; | ||
| import { | ||
| BindOnceFuture, | ||
| ExportResultCode, | ||
|
|
@@ -14,9 +14,12 @@ import { | |
| } from '@opentelemetry/core'; | ||
| import type { Span } from '../Span'; | ||
| import type { SpanProcessor } from '../SpanProcessor'; | ||
| import type { SpanProcessorConfig } from './SpanProcessorConfig'; | ||
| import type { BufferConfig } from '../types'; | ||
| import type { ReadableSpan } from './ReadableSpan'; | ||
| import type { SpanExporter } from './SpanExporter'; | ||
| import { SpanProcessorMetrics } from './SpanProcessorMetrics'; | ||
| import { OTEL_COMPONENT_TYPE_VALUE_BATCHING_SPAN_PROCESSOR } from '../semconv'; | ||
|
|
||
| /** | ||
| * Implementation of the {@link SpanProcessor} that batches spans exported by | ||
|
|
@@ -30,14 +33,15 @@ export abstract class BatchSpanProcessorBase<T extends BufferConfig> | |
| private readonly _scheduledDelayMillis: number; | ||
| private readonly _exportTimeoutMillis: number; | ||
| private readonly _exporter: SpanExporter; | ||
| private readonly _metrics: SpanProcessorMetrics; | ||
|
|
||
| private _isExporting = false; | ||
| private _finishedSpans: ReadableSpan[] = []; | ||
| private _timer: NodeJS.Timeout | number | undefined; | ||
| private _shutdownOnce: BindOnceFuture<void>; | ||
| private _droppedSpansCount: number = 0; | ||
|
|
||
| constructor(exporter: SpanExporter, config?: T) { | ||
| constructor(exporter: SpanExporter, config?: T & SpanProcessorConfig) { | ||
| this._exporter = exporter; | ||
| this._maxExportBatchSize = | ||
| typeof config?.maxExportBatchSize === 'number' | ||
|
|
@@ -64,6 +68,18 @@ export abstract class BatchSpanProcessorBase<T extends BufferConfig> | |
| ); | ||
| this._maxExportBatchSize = this._maxQueueSize; | ||
| } | ||
|
|
||
| const meter = config?.meterProvider | ||
| ? config.meterProvider.getMeter('@opentelemetry/sdk-trace') | ||
| : createNoopMeter(); | ||
| this._metrics = new SpanProcessorMetrics( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh thanks - I can add explicit cleanup. But just curious, shouldn't the GC still be handle cycles like that or is there something else at play?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a cycle issue. The problem is that the Meter lives longer than the span processor and holds onto the callback. Since the callback references the processor, the processor can never be garbage collected, even after shutdown(). Removing the callback in shutdown() should fix it. |
||
| OTEL_COMPONENT_TYPE_VALUE_BATCHING_SPAN_PROCESSOR, | ||
| meter, | ||
| { | ||
| capacity: this._maxQueueSize, | ||
| getQueueSize: () => this._finishedSpans.length, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| forceFlush(): Promise<void> { | ||
|
|
@@ -101,6 +117,7 @@ export abstract class BatchSpanProcessorBase<T extends BufferConfig> | |
| return this._flushAll(); | ||
| }) | ||
| .then(() => { | ||
| this._metrics.shutdown(); | ||
| return this._exporter.shutdown(); | ||
| }); | ||
| } | ||
|
|
@@ -114,6 +131,7 @@ export abstract class BatchSpanProcessorBase<T extends BufferConfig> | |
| diag.debug('maxQueueSize reached, dropping spans'); | ||
| } | ||
| this._droppedSpansCount++; | ||
| this._metrics.dropSpans(1); | ||
|
|
||
| return; | ||
| } | ||
|
|
@@ -179,6 +197,7 @@ export abstract class BatchSpanProcessorBase<T extends BufferConfig> | |
| const doExport = () => | ||
| this._exporter.export(spans, result => { | ||
| clearTimeout(timer); | ||
| this._metrics.finishSpans(spans.length, result.error); | ||
| if (result.code === ExportResultCode.SUCCESS) { | ||
| resolve(); | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { MeterProvider } from '@opentelemetry/api'; | ||
|
|
||
| /** | ||
| * Common options for SDK span processors. | ||
| */ | ||
| export interface SpanProcessorConfig { | ||
| meterProvider?: MeterProvider; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.