Skip to content

Commit

Permalink
fix: make logger property public instead of private in BasicTracer (#213
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mayurkale22 authored Aug 26, 2019
1 parent f4ed3f2 commit beda31e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
7 changes: 3 additions & 4 deletions packages/opentelemetry-basic-tracer/src/BasicTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class BasicTracer implements types.Tracer {
private readonly _httpTextFormat: types.HttpTextFormat;
private readonly _sampler: types.Sampler;
private readonly _scopeManager: ScopeManager;
private readonly _logger: Logger;
readonly logger: Logger;

/**
* Constructs a new Tracer instance.
Expand All @@ -55,7 +55,7 @@ export class BasicTracer implements types.Tracer {
this._httpTextFormat = config.httpTextFormat || new HttpTraceContext();
this._sampler = config.sampler || ALWAYS_SAMPLER;
this._scopeManager = config.scopeManager;
this._logger = config.logger || new ConsoleLogger(config.logLevel);
this.logger = config.logger || new ConsoleLogger(config.logLevel);
}

/**
Expand Down Expand Up @@ -83,13 +83,12 @@ export class BasicTracer implements types.Tracer {
const spanContext = { traceId, spanId, traceOptions, traceState };
const recordEvents = options.isRecordingEvents || false;
if (!recordEvents && !samplingDecision) {
this._logger.debug('Sampling is off, starting no recording span');
this.logger.debug('Sampling is off, starting no recording span');
return new NoRecordingSpan(spanContext);
}

const span = new Span(
this,
this._logger,
name,
spanContext,
options.kind || types.SpanKind.INTERNAL,
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-basic-tracer/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import * as types from '@opentelemetry/types';
import { performance } from 'perf_hooks';
import { ReadableSpan } from './export/ReadableSpan';
import { BasicTracer } from './BasicTracer';

/**
* This class represents a span.
Expand All @@ -42,8 +43,7 @@ export class Span implements types.Span, ReadableSpan {

/** Constructs a new Span instance. */
constructor(
parentTracer: types.Tracer,
logger: types.Logger,
parentTracer: BasicTracer,
spanName: string,
spanContext: types.SpanContext,
kind: types.SpanKind,
Expand All @@ -56,7 +56,7 @@ export class Span implements types.Span, ReadableSpan {
this.parentSpanId = parentSpanId;
this.kind = kind;
this.startTime = startTime || performance.now();
this._logger = logger;
this._logger = parentTracer.logger;
}

tracer(): types.Tracer {
Expand Down
53 changes: 18 additions & 35 deletions packages/opentelemetry-basic-tracer/test/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import {
TraceOptions,
SpanContext,
} from '@opentelemetry/types';
import { NoopTracer, NoopLogger } from '@opentelemetry/core';
import { BasicTracer } from '../src';
import { NoopScopeManager } from '@opentelemetry/scope-base';

describe('Span', () => {
const tracer = new NoopTracer();
const logger = new NoopLogger();
const tracer = new BasicTracer({
scopeManager: new NoopScopeManager(),
});
const name = 'span1';
const spanContext: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
Expand All @@ -35,14 +37,14 @@ describe('Span', () => {
};

it('should create a Span instance', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.SERVER);
const span = new Span(tracer, name, spanContext, SpanKind.SERVER);
assert.ok(span instanceof Span);
assert.strictEqual(span.tracer(), tracer);
span.end();
});

it('should get the span context of span', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
const context = span.context();
assert.strictEqual(context.traceId, spanContext.traceId);
assert.strictEqual(context.traceOptions, TraceOptions.SAMPLED);
Expand All @@ -53,13 +55,13 @@ describe('Span', () => {
});

it('should return true when isRecordingEvents:true', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
assert.ok(span.isRecordingEvents());
span.end();
});

it('should set an attribute', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);

['String', 'Number', 'Boolean'].map(attType => {
span.setAttribute('testKey' + attType, 'testValue' + attType);
Expand All @@ -69,7 +71,7 @@ describe('Span', () => {
});

it('should set an event', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
span.addEvent('sent');
span.addEvent('rev', { attr1: 'value', attr2: 123, attr3: true });
span.end();
Expand All @@ -81,14 +83,14 @@ describe('Span', () => {
spanId: '5e0c63257de34c92',
traceOptions: TraceOptions.SAMPLED,
};
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
span.addLink(spanContext);
span.addLink(spanContext, { attr1: 'value', attr2: 123, attr3: true });
span.end();
});

it('should set an error status', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
span.setStatus({
code: CanonicalCode.PERMISSION_DENIED,
message: 'This is an error',
Expand All @@ -100,7 +102,6 @@ describe('Span', () => {
const parentId = '5c1c63257de34c67';
const span = new Span(
tracer,
logger,
'my-span',
spanContext,
SpanKind.INTERNAL,
Expand All @@ -121,13 +122,7 @@ describe('Span', () => {
});

it('should return ReadableSpan with attributes', () => {
const span = new Span(
tracer,
logger,
'my-span',
spanContext,
SpanKind.CLIENT
);
const span = new Span(tracer, 'my-span', spanContext, SpanKind.CLIENT);
span.setAttribute('attr1', 'value1');
let readableSpan = span.toReadableSpan();
assert.deepStrictEqual(readableSpan.attributes, { attr1: 'value1' });
Expand All @@ -150,13 +145,7 @@ describe('Span', () => {
});

it('should return ReadableSpan with links', () => {
const span = new Span(
tracer,
logger,
'my-span',
spanContext,
SpanKind.CLIENT
);
const span = new Span(tracer, 'my-span', spanContext, SpanKind.CLIENT);
span.addLink(spanContext);
let readableSpan = span.toReadableSpan();
assert.strictEqual(readableSpan.links.length, 1);
Expand Down Expand Up @@ -193,13 +182,7 @@ describe('Span', () => {
});

it('should return ReadableSpan with events', () => {
const span = new Span(
tracer,
logger,
'my-span',
spanContext,
SpanKind.CLIENT
);
const span = new Span(tracer, 'my-span', spanContext, SpanKind.CLIENT);
span.addEvent('sent');
let readableSpan = span.toReadableSpan();
assert.strictEqual(readableSpan.events.length, 1);
Expand Down Expand Up @@ -232,7 +215,7 @@ describe('Span', () => {
});

it('should return ReadableSpan with new status', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
span.setStatus({
code: CanonicalCode.PERMISSION_DENIED,
message: 'This is an error',
Expand All @@ -254,14 +237,14 @@ describe('Span', () => {
});

it('should only end a span once', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.SERVER);
const span = new Span(tracer, name, spanContext, SpanKind.SERVER);
span.end(1234);
span.end(4567);
assert.strictEqual(span.endTime, 1234);
});

it('should update name', () => {
const span = new Span(tracer, logger, name, spanContext, SpanKind.SERVER);
const span = new Span(tracer, name, spanContext, SpanKind.SERVER);
span.updateName('foo-span');
span.end();

Expand Down

0 comments on commit beda31e

Please sign in to comment.