-
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 10 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. | ||
|
|
@@ -58,6 +60,9 @@ export class Tracer implements api.Tracer { | |
| this._idGenerator = config.idGenerator || new RandomIdGenerator(); | ||
| this._resource = resource; | ||
| this._spanProcessor = spanProcessor; | ||
| this._tracerMetrics = new TracerMetrics( | ||
| localConfig.meterProvider || api.metrics.getMeterProvider() | ||
| ); | ||
|
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. @anuraaga Am I correct in understanding that this default to the global MeterProvider differs from the Java impl? https://github.com/open-telemetry/opentelemetry-java/pull/7895/files#diff-918636d01c04fc2f4763f5564a621b35a919e1d80b5b9be3b4d9daa2649a96c3R42 looks like Java defaults to a Noop MeterProvider. I was going to start discussing whether these "development"-status metrics should be on by default in this ("stable") package. My understanding is that OTel is still trying to figure things out around what things should be on by default and whether and how to opt-in to things. @open-telemetry/javascript-maintainers I'd appreciate other opinions here. Once we sort out "on/off by default", presumably sdk-node/src/sdk.ts should be updated to pass in the just-created meterProvider.
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. Thanks, as the spec for SDK metrics doesn't mention opt-in at all, I've been going under the assumption that they are emitted by default, and views can be used to delete them. For better or worse, the "legacy" metrics in the Java SDK have behaved that way as far as precedent goes. But either way, in terms of initializing a tracer, I agree that noop looks better here, and matches what we do in Java, where it's autoconfigure that wires things up. So |
||
| this.instrumentationScope = instrumentationScope; | ||
| } | ||
|
|
||
|
|
@@ -120,6 +125,11 @@ export class Tracer implements api.Tracer { | |
| links | ||
| ); | ||
|
|
||
| const recordEndMetrics = this._tracerMetrics.startSpan( | ||
| parentSpanContext, | ||
| samplingResult.decision | ||
| ); | ||
|
|
||
| traceState = samplingResult.traceState ?? traceState; | ||
|
|
||
| const traceFlags = | ||
|
|
@@ -154,6 +164,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,87 @@ | ||
| /* | ||
| * 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, | ||
| MeterProvider, | ||
| SpanContext, | ||
| UpDownCounter, | ||
| } from '@opentelemetry/api'; | ||
| import { SamplingDecision } from './Sampler'; | ||
|
|
||
| export class TracerMetrics { | ||
|
anuraaga marked this conversation as resolved.
|
||
| private readonly startedSpans: Counter; | ||
| private readonly liveSpans: UpDownCounter; | ||
|
|
||
| constructor(meterProvider: MeterProvider) { | ||
| const meter = 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. This string becomes the InstrumentationScope.name. Typically our instrumentation uses the JS package name for this, which in this case has the "-base" suffix. I can understand wanting to use "sdk-trace" without the "-base":
I'm fine with using this name. |
||
|
|
||
| this.startedSpans = meter.createCounter('otel.sdk.span.started', { | ||
| unit: '{span}', | ||
| description: 'The number of created spans.', | ||
| }); | ||
|
|
||
| this.liveSpans = meter.createUpDownCounter('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, { | ||
| 'otel.span.parent.origin': parentOrigin(parentSpanCtx), | ||
| 'otel.span.sampling_result': samplingDecisionStr, | ||
| }); | ||
|
|
||
| if (samplingDecision === SamplingDecision.NOT_RECORD) { | ||
| return () => {}; | ||
| } | ||
|
|
||
| const liveSpanAttributes = { | ||
| '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'; | ||
| default: | ||
| return 'unknown'; | ||
|
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. Can a SamplingDecision be any other value? I.e. is the "unknown" fallback necessary? /shrug
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. You're right, somehow I thought SamplingDecision was in a different package. Removed it |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,7 +14,11 @@ | |||||
| * limitations under the License. | ||||||
| */ | ||||||
|
|
||||||
| import { ContextManager, TextMapPropagator } from '@opentelemetry/api'; | ||||||
| import { | ||||||
|
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. nit: I think this could be
Suggested change
|
||||||
| ContextManager, | ||||||
| MeterProvider, | ||||||
| TextMapPropagator, | ||||||
| } from '@opentelemetry/api'; | ||||||
| import { Resource } from '@opentelemetry/resources'; | ||||||
| import { IdGenerator } from './IdGenerator'; | ||||||
| import { Sampler } from './Sampler'; | ||||||
|
|
@@ -54,6 +58,9 @@ export interface TracerConfig { | |||||
| * List of SpanProcessor for the tracer | ||||||
| */ | ||||||
| spanProcessors?: SpanProcessor[]; | ||||||
|
|
||||||
| /** A meter provider to record trace SDK metrics to. */ | ||||||
| meterProvider?: MeterProvider; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
||||||
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.
This should be added to the top-dir CHANGELOG.md. This file is for changes to the api/... dir.