Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.

* feat(api): improve isValidSpanId, isValidTraceId performance [#5714](https://github.com/open-telemetry/opentelemetry-js/pull/5714) @seemk
* feat(diag): change types in `DiagComponentLogger` from `any` to `unknown`[#5478](https://github.com/open-telemetry/opentelemetry-js/pull/5478) @loganrosen
* feat(sdk-trace): implement span start/end metrics [#1851](https://github.com/open-telemetry/opentelemetry-js/pull/6213) @anuraaga

Copy link
Copy Markdown
Contributor

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.


### :bug: (Bug Fix)

Expand Down
11 changes: 8 additions & 3 deletions e2e-tests/verify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ for (const line of lines) {
verifiedSpan = true;
}
if (parsed.resourceMetrics) {
console.log('found metric');
verifyMetric(parsed.resourceMetrics[0].scopeMetrics[0].metrics[0]);
verifiedMetric = true;
const scopeMetrics = parsed.resourceMetrics[0].scopeMetrics.find(
sm => sm.scope.name === 'example-meter'
);
if (scopeMetrics) {
console.log('found metric');
verifyMetric(scopeMetrics.metrics[0]);
verifiedMetric = true;
}
}
if (parsed.resourceLogs) {
console.log('found log');
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/opentelemetry-sdk-trace-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
},
"devDependencies": {
"@opentelemetry/api": ">=1.3.0 <1.10.0",
"@opentelemetry/sdk-metrics": "2.2.0",
"@types/mocha": "10.0.10",
"@types/node": "18.6.5",
"@types/sinon": "17.0.4",
Expand Down
4 changes: 4 additions & 0 deletions packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ interface SpanOptions {
attributes?: Attributes;
spanLimits: SpanLimits;
spanProcessor: SpanProcessor;
recordEndMetrics?: () => void;
}

/**
Expand Down Expand Up @@ -105,6 +106,7 @@ export class SpanImpl implements Span {
private readonly _spanProcessor: SpanProcessor;
private readonly _spanLimits: SpanLimits;
private readonly _attributeValueLengthLimit: number;
private readonly _recordEndMetrics?: () => void;

private readonly _performanceStartTime: number;
private readonly _performanceOffset: number;
Expand Down Expand Up @@ -133,6 +135,7 @@ export class SpanImpl implements Span {
this.startTime = this._getTime(opts.startTime ?? now);
this.resource = opts.resource;
this.instrumentationScope = opts.scope;
this._recordEndMetrics = opts.recordEndMetrics;

if (opts.attributes != null) {
this.setAttributes(opts.attributes);
Expand Down Expand Up @@ -292,6 +295,7 @@ export class SpanImpl implements Span {
this._spanProcessor.onEnding(this);
}

this._recordEndMetrics?.();
this._ended = true;
this._spanProcessor.onEnd(this);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/opentelemetry-sdk-trace-base/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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()
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
FWIW, OTel Java has a otel.instrumentation.http.client.emit-experimental-telemetry boolean config item. I don't see many similar config items, so I gather things are still being felt out.

Once we sort out "on/off by default", presumably sdk-node/src/sdk.ts should be updated to pass in the just-created meterProvider.
This might mean re-ordering creation of providers in NodeSDK.start(), not sure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 NodeSDK in opentelemetry-js terms, tweaked to that

this.instrumentationScope = instrumentationScope;
}

Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -154,6 +164,7 @@ export class Tracer implements api.Tracer {
startTime: options.startTime,
spanProcessor: this._spanProcessor,
spanLimits: this._spanLimits,
recordEndMetrics,
});
return span;
}
Expand Down
87 changes: 87 additions & 0 deletions packages/opentelemetry-sdk-trace-base/src/TracerMetrics.ts
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 {
Comment thread
anuraaga marked this conversation as resolved.
private readonly startedSpans: Counter;
private readonly liveSpans: UpDownCounter;

constructor(meterProvider: MeterProvider) {
const meter = meterProvider.getMeter('@opentelemetry/sdk-trace');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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":

  • the equivalents for metrics and logs are in the .../sdk-metrics and .../sdk-logs packages (no "-base" on them)

I'm fine with using this name.
@pichlermarc Do you have a particular opinion on this?


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';
Comment thread
trentm marked this conversation as resolved.
case SamplingDecision.RECORD:
return 'RECORD_ONLY';
case SamplingDecision.NOT_RECORD:
return 'DROP';
default:
return 'unknown';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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

}
}
9 changes: 8 additions & 1 deletion packages/opentelemetry-sdk-trace-base/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
* limitations under the License.
*/

import { ContextManager, TextMapPropagator } from '@opentelemetry/api';
import {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this could be import type, though this isn't something you changed.

Suggested change
import {
import type {

ContextManager,
MeterProvider,
TextMapPropagator,
} from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { IdGenerator } from './IdGenerator';
import { Sampler } from './Sampler';
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Loading