Skip to content

Commit 38059cb

Browse files
committed
feat: pipe InstrumentationLibrary from Tracer to ReadableSpan
1 parent 3e41a04 commit 38059cb

6 files changed

Lines changed: 50 additions & 6 deletions

File tree

packages/opentelemetry-tracing/src/BasicTracerProvider.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export class BasicTracerProvider implements api.TracerProvider {
4747
getTracer(name: string, version = '*', config?: TracerConfig): Tracer {
4848
const key = `${name}@${version}`;
4949
if (!this._tracers.has(key)) {
50-
this._tracers.set(key, new Tracer(config || this._config, this));
50+
this._tracers.set(
51+
key,
52+
new Tracer(name, version, config || this._config, this)
53+
);
5154
}
5255

5356
return this._tracers.get(key)!;

packages/opentelemetry-tracing/src/Span.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as api from '@opentelemetry/api';
1818
import {
1919
hrTime,
2020
hrTimeDuration,
21+
InstrumentationLibrary,
2122
isTimeInput,
2223
timeInputToHrTime,
2324
} from '@opentelemetry/core';
@@ -41,6 +42,7 @@ export class Span implements api.Span, ReadableSpan {
4142
readonly events: api.TimedEvent[] = [];
4243
readonly startTime: api.HrTime;
4344
readonly resource: Resource;
45+
readonly instrumentationLibrary: InstrumentationLibrary;
4446
name: string;
4547
status: api.Status = {
4648
code: api.CanonicalCode.OK,
@@ -69,6 +71,7 @@ export class Span implements api.Span, ReadableSpan {
6971
this.links = links;
7072
this.startTime = timeInputToHrTime(startTime);
7173
this.resource = parentTracer.resource;
74+
this.instrumentationLibrary = parentTracer.instrumentationLibrary;
7275
this._logger = parentTracer.logger;
7376
this._traceParams = parentTracer.getActiveTraceParams();
7477
this._spanProcessor = parentTracer.getActiveSpanProcessor();

packages/opentelemetry-tracing/src/Tracer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
ConsoleLogger,
2020
getActiveSpan,
2121
getParentSpanContext,
22+
InstrumentationLibrary,
2223
isValid,
2324
NoRecordingSpan,
2425
randomSpanId,
@@ -39,12 +40,15 @@ export class Tracer implements api.Tracer {
3940
private readonly _sampler: api.Sampler;
4041
private readonly _traceParams: TraceParams;
4142
readonly resource: Resource;
43+
readonly instrumentationLibrary: InstrumentationLibrary;
4244
readonly logger: api.Logger;
4345

4446
/**
4547
* Constructs a new Tracer instance.
4648
*/
4749
constructor(
50+
name: string,
51+
version: string,
4852
config: TracerConfig,
4953
private _tracerProvider: BasicTracerProvider
5054
) {
@@ -53,6 +57,7 @@ export class Tracer implements api.Tracer {
5357
this._sampler = localConfig.sampler;
5458
this._traceParams = localConfig.traceParams;
5559
this.resource = _tracerProvider.resource;
60+
this.instrumentationLibrary = new InstrumentationLibrary(name, version);
5661
this.logger = config.logger || new ConsoleLogger(config.logLevel);
5762
}
5863

packages/opentelemetry-tracing/src/export/ReadableSpan.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
TimedEvent,
2525
} from '@opentelemetry/api';
2626
import { Resource } from '@opentelemetry/resources';
27+
import { InstrumentationLibrary } from '@opentelemetry/core';
2728

2829
export interface ReadableSpan {
2930
readonly name: string;
@@ -39,4 +40,5 @@ export interface ReadableSpan {
3940
readonly duration: HrTime;
4041
readonly ended: boolean;
4142
readonly resource: Resource;
43+
readonly instrumentationLibrary: InstrumentationLibrary;
4244
}

packages/opentelemetry-tracing/test/Span.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
hrTimeToMilliseconds,
3030
NoopLogger,
3131
hrTimeDuration,
32+
InstrumentationLibrary,
3233
} from '@opentelemetry/core';
3334

3435
const performanceTimeOrigin = hrTime();
@@ -230,6 +231,7 @@ describe('Span', () => {
230231
assert.deepStrictEqual(span.attributes, {});
231232
assert.deepStrictEqual(span.links, []);
232233
assert.deepStrictEqual(span.events, []);
234+
assert.ok(span.instrumentationLibrary instanceof InstrumentationLibrary);
233235
});
234236

235237
it('should return ReadableSpan with attributes', () => {

packages/opentelemetry-tracing/test/Tracer.test.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import * as assert from 'assert';
1818
import { NoopSpan, Sampler, SamplingDecision } from '@opentelemetry/api';
1919
import { BasicTracerProvider, Tracer, Span } from '../src';
20-
import { NoopLogger, ALWAYS_SAMPLER, NEVER_SAMPLER } from '@opentelemetry/core';
20+
import {
21+
InstrumentationLibrary,
22+
NoopLogger,
23+
ALWAYS_SAMPLER,
24+
NEVER_SAMPLER,
25+
} from '@opentelemetry/core';
2126

2227
describe('Tracer', () => {
2328
const tracerProvider = new BasicTracerProvider({
@@ -36,28 +41,52 @@ describe('Tracer', () => {
3641
}
3742

3843
it('should create a Tracer instance', () => {
39-
const tracer = new Tracer({}, tracerProvider);
44+
const tracer = new Tracer('default', '0.0.1', {}, tracerProvider);
4045
assert.ok(tracer instanceof Tracer);
4146
});
4247

4348
it('should respect NO_RECORD sampling result', () => {
44-
const tracer = new Tracer({ sampler: NEVER_SAMPLER }, tracerProvider);
49+
const tracer = new Tracer(
50+
'default',
51+
'0.0.1',
52+
{ sampler: NEVER_SAMPLER },
53+
tracerProvider
54+
);
4555
const span = tracer.startSpan('span1');
4656
assert.ok(span instanceof NoopSpan);
4757
span.end();
4858
});
4959

5060
it('should respect RECORD_AND_SAMPLE sampling result', () => {
51-
const tracer = new Tracer({ sampler: ALWAYS_SAMPLER }, tracerProvider);
61+
const tracer = new Tracer(
62+
'default',
63+
'0.0.1',
64+
{ sampler: ALWAYS_SAMPLER },
65+
tracerProvider
66+
);
5267
const span = tracer.startSpan('span2');
5368
assert.ok(!(span instanceof NoopSpan));
5469
span.end();
5570
});
5671

5772
it('should start a span with attributes in sampling result', () => {
58-
const tracer = new Tracer({ sampler: new TestSampler() }, tracerProvider);
73+
const tracer = new Tracer(
74+
'default',
75+
'0.0.1',
76+
{ sampler: new TestSampler() },
77+
tracerProvider
78+
);
5979
const span = tracer.startSpan('span3');
6080
assert.strictEqual((span as Span).attributes.testAttribute, 'foobar');
6181
span.end();
6282
});
83+
84+
it('should have an instrumentationLibrary', () => {
85+
const tracer = new Tracer('default', '0.0.1', {}, tracerProvider);
86+
87+
const lib: InstrumentationLibrary = tracer.instrumentationLibrary;
88+
89+
assert.strictEqual(lib.name, 'default');
90+
assert.strictEqual(lib.version, '0.0.1');
91+
});
6392
});

0 commit comments

Comments
 (0)