Skip to content

Commit

Permalink
feat: pipe InstrumentationLibrary from Tracer to ReadableSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Jun 9, 2020
1 parent 3e41a04 commit 38059cb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/opentelemetry-tracing/src/BasicTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export class BasicTracerProvider implements api.TracerProvider {
getTracer(name: string, version = '*', config?: TracerConfig): Tracer {
const key = `${name}@${version}`;
if (!this._tracers.has(key)) {
this._tracers.set(key, new Tracer(config || this._config, this));
this._tracers.set(
key,
new Tracer(name, version, config || this._config, this)
);
}

return this._tracers.get(key)!;
Expand Down
3 changes: 3 additions & 0 deletions packages/opentelemetry-tracing/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as api from '@opentelemetry/api';
import {
hrTime,
hrTimeDuration,
InstrumentationLibrary,
isTimeInput,
timeInputToHrTime,
} from '@opentelemetry/core';
Expand All @@ -41,6 +42,7 @@ export class Span implements api.Span, ReadableSpan {
readonly events: api.TimedEvent[] = [];
readonly startTime: api.HrTime;
readonly resource: Resource;
readonly instrumentationLibrary: InstrumentationLibrary;
name: string;
status: api.Status = {
code: api.CanonicalCode.OK,
Expand Down Expand Up @@ -69,6 +71,7 @@ export class Span implements api.Span, ReadableSpan {
this.links = links;
this.startTime = timeInputToHrTime(startTime);
this.resource = parentTracer.resource;
this.instrumentationLibrary = parentTracer.instrumentationLibrary;
this._logger = parentTracer.logger;
this._traceParams = parentTracer.getActiveTraceParams();
this._spanProcessor = parentTracer.getActiveSpanProcessor();
Expand Down
5 changes: 5 additions & 0 deletions packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ConsoleLogger,
getActiveSpan,
getParentSpanContext,
InstrumentationLibrary,
isValid,
NoRecordingSpan,
randomSpanId,
Expand All @@ -39,12 +40,15 @@ export class Tracer implements api.Tracer {
private readonly _sampler: api.Sampler;
private readonly _traceParams: TraceParams;
readonly resource: Resource;
readonly instrumentationLibrary: InstrumentationLibrary;
readonly logger: api.Logger;

/**
* Constructs a new Tracer instance.
*/
constructor(
name: string,
version: string,
config: TracerConfig,
private _tracerProvider: BasicTracerProvider
) {
Expand All @@ -53,6 +57,7 @@ export class Tracer implements api.Tracer {
this._sampler = localConfig.sampler;
this._traceParams = localConfig.traceParams;
this.resource = _tracerProvider.resource;
this.instrumentationLibrary = new InstrumentationLibrary(name, version);
this.logger = config.logger || new ConsoleLogger(config.logLevel);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-tracing/src/export/ReadableSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
TimedEvent,
} from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { InstrumentationLibrary } from '@opentelemetry/core';

export interface ReadableSpan {
readonly name: string;
Expand All @@ -39,4 +40,5 @@ export interface ReadableSpan {
readonly duration: HrTime;
readonly ended: boolean;
readonly resource: Resource;
readonly instrumentationLibrary: InstrumentationLibrary;
}
2 changes: 2 additions & 0 deletions packages/opentelemetry-tracing/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
hrTimeToMilliseconds,
NoopLogger,
hrTimeDuration,
InstrumentationLibrary,
} from '@opentelemetry/core';

const performanceTimeOrigin = hrTime();
Expand Down Expand Up @@ -230,6 +231,7 @@ describe('Span', () => {
assert.deepStrictEqual(span.attributes, {});
assert.deepStrictEqual(span.links, []);
assert.deepStrictEqual(span.events, []);
assert.ok(span.instrumentationLibrary instanceof InstrumentationLibrary);
});

it('should return ReadableSpan with attributes', () => {
Expand Down
39 changes: 34 additions & 5 deletions packages/opentelemetry-tracing/test/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import * as assert from 'assert';
import { NoopSpan, Sampler, SamplingDecision } from '@opentelemetry/api';
import { BasicTracerProvider, Tracer, Span } from '../src';
import { NoopLogger, ALWAYS_SAMPLER, NEVER_SAMPLER } from '@opentelemetry/core';
import {
InstrumentationLibrary,
NoopLogger,
ALWAYS_SAMPLER,
NEVER_SAMPLER,
} from '@opentelemetry/core';

describe('Tracer', () => {
const tracerProvider = new BasicTracerProvider({
Expand All @@ -36,28 +41,52 @@ describe('Tracer', () => {
}

it('should create a Tracer instance', () => {
const tracer = new Tracer({}, tracerProvider);
const tracer = new Tracer('default', '0.0.1', {}, tracerProvider);
assert.ok(tracer instanceof Tracer);
});

it('should respect NO_RECORD sampling result', () => {
const tracer = new Tracer({ sampler: NEVER_SAMPLER }, tracerProvider);
const tracer = new Tracer(
'default',
'0.0.1',
{ sampler: NEVER_SAMPLER },
tracerProvider
);
const span = tracer.startSpan('span1');
assert.ok(span instanceof NoopSpan);
span.end();
});

it('should respect RECORD_AND_SAMPLE sampling result', () => {
const tracer = new Tracer({ sampler: ALWAYS_SAMPLER }, tracerProvider);
const tracer = new Tracer(
'default',
'0.0.1',
{ sampler: ALWAYS_SAMPLER },
tracerProvider
);
const span = tracer.startSpan('span2');
assert.ok(!(span instanceof NoopSpan));
span.end();
});

it('should start a span with attributes in sampling result', () => {
const tracer = new Tracer({ sampler: new TestSampler() }, tracerProvider);
const tracer = new Tracer(
'default',
'0.0.1',
{ sampler: new TestSampler() },
tracerProvider
);
const span = tracer.startSpan('span3');
assert.strictEqual((span as Span).attributes.testAttribute, 'foobar');
span.end();
});

it('should have an instrumentationLibrary', () => {
const tracer = new Tracer('default', '0.0.1', {}, tracerProvider);

const lib: InstrumentationLibrary = tracer.instrumentationLibrary;

assert.strictEqual(lib.name, 'default');
assert.strictEqual(lib.version, '0.0.1');
});
});

0 comments on commit 38059cb

Please sign in to comment.