-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(sdk-trace): implement span start/end metrics #6213
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
Changes from 14 commits
53d90a7
8f7d498
3ef44da
d573088
2af11e6
0fcaed8
d212424
f336a00
12dc161
28328d4
91ba67f
568aef4
6379c36
8ba3cf4
ddf2814
26a9d38
49c10d5
feb1621
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ import { Sampler } from './Sampler'; | |
| import { IdGenerator } from './IdGenerator'; | ||
| import { RandomIdGenerator } from './platform'; | ||
| import { Resource } from '@opentelemetry/resources'; | ||
| import { TracerMetrics } from './TracerMetrics'; | ||
|
|
||
| /** | ||
| * This class represents a basic tracer. | ||
|
|
@@ -41,6 +42,7 @@ export class Tracer implements api.Tracer { | |
|
|
||
| private readonly _resource: Resource; | ||
| private readonly _spanProcessor: SpanProcessor; | ||
| private readonly _tracerMetrics: TracerMetrics; | ||
|
|
||
| /** | ||
| * Constructs a new Tracer instance. | ||
|
|
@@ -59,6 +61,11 @@ export class Tracer implements api.Tracer { | |
| this._resource = resource; | ||
| this._spanProcessor = spanProcessor; | ||
| this.instrumentationScope = instrumentationScope; | ||
|
|
||
| const meter = localConfig.meterProvider | ||
| ? localConfig.meterProvider.getMeter('@opentelemetry/sdk-trace') | ||
|
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. Perhaps this change to add a version to that Meter's instrumentationScope: Instrumentations typically pass in their package version to be used for instrumentationScope.version for their tracer/meter/logger. |
||
| : api.createNoopMeter(); | ||
| this._tracerMetrics = new TracerMetrics(meter); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -120,6 +127,11 @@ export class Tracer implements api.Tracer { | |
| links | ||
| ); | ||
|
|
||
| const recordEndMetrics = this._tracerMetrics.startSpan( | ||
| parentSpanContext, | ||
| samplingResult.decision | ||
| ); | ||
|
|
||
| traceState = samplingResult.traceState ?? traceState; | ||
|
|
||
| const traceFlags = | ||
|
|
@@ -154,6 +166,7 @@ export class Tracer implements api.Tracer { | |
| startTime: options.startTime, | ||
| spanProcessor: this._spanProcessor, | ||
| spanLimits: this._spanLimits, | ||
| recordEndMetrics, | ||
| }); | ||
| return span; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { Counter, Meter, SpanContext, UpDownCounter } from '@opentelemetry/api'; | ||
| import { SamplingDecision } from './Sampler'; | ||
| import { | ||
| ATTR_OTEL_SPAN_PARENT_ORIGIN, | ||
| ATTR_OTEL_SPAN_SAMPLING_RESULT, | ||
| METRIC_OTEL_SDK_SPAN_LIVE, | ||
| METRIC_OTEL_SDK_SPAN_STARTED, | ||
| } from './semconv'; | ||
|
|
||
| /** | ||
| * Generates `otel.sdk.span.*` metrics. | ||
| * https://opentelemetry.io/docs/specs/semconv/otel/sdk-metrics/#span-metrics | ||
| */ | ||
| export class TracerMetrics { | ||
|
anuraaga marked this conversation as resolved.
|
||
| private readonly startedSpans: Counter; | ||
| private readonly liveSpans: UpDownCounter; | ||
|
|
||
| constructor(meter: Meter) { | ||
| this.startedSpans = meter.createCounter(METRIC_OTEL_SDK_SPAN_STARTED, { | ||
| unit: '{span}', | ||
| description: 'The number of created spans.', | ||
| }); | ||
|
|
||
| this.liveSpans = meter.createUpDownCounter(METRIC_OTEL_SDK_SPAN_LIVE, { | ||
| unit: '{span}', | ||
| description: 'The number of currently live spans.', | ||
| }); | ||
| } | ||
|
|
||
| startSpan( | ||
| parentSpanCtx: SpanContext | undefined, | ||
| samplingDecision: SamplingDecision | ||
| ): () => void { | ||
| const samplingDecisionStr = samplingDecisionToString(samplingDecision); | ||
| this.startedSpans.add(1, { | ||
| [ATTR_OTEL_SPAN_PARENT_ORIGIN]: parentOrigin(parentSpanCtx), | ||
| [ATTR_OTEL_SPAN_SAMPLING_RESULT]: samplingDecisionStr, | ||
| }); | ||
|
|
||
| if (samplingDecision === SamplingDecision.NOT_RECORD) { | ||
| return () => {}; | ||
| } | ||
|
|
||
| const liveSpanAttributes = { | ||
| [ATTR_OTEL_SPAN_SAMPLING_RESULT]: samplingDecisionStr, | ||
| }; | ||
| this.liveSpans.add(1, liveSpanAttributes); | ||
| return () => { | ||
| this.liveSpans.add(-1, liveSpanAttributes); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function parentOrigin(parentSpanContext: SpanContext | undefined): string { | ||
| if (!parentSpanContext) { | ||
| return 'none'; | ||
| } | ||
| if (parentSpanContext.isRemote) { | ||
| return 'remote'; | ||
| } | ||
| return 'local'; | ||
| } | ||
|
|
||
| function samplingDecisionToString(decision: SamplingDecision): string { | ||
| switch (decision) { | ||
| case SamplingDecision.RECORD_AND_SAMPLED: | ||
| return 'RECORD_AND_SAMPLE'; | ||
|
trentm marked this conversation as resolved.
|
||
| case SamplingDecision.RECORD: | ||
| return 'RECORD_ONLY'; | ||
| case SamplingDecision.NOT_RECORD: | ||
| return 'DROP'; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this isn't great but couldn't think of anything better. Let me know if you have any ideas