Skip to content

Commit beda31e

Browse files
authored
fix: make logger property public instead of private in BasicTracer (open-telemetry#213)
1 parent f4ed3f2 commit beda31e

File tree

3 files changed

+24
-42
lines changed

3 files changed

+24
-42
lines changed

packages/opentelemetry-basic-tracer/src/BasicTracer.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class BasicTracer implements types.Tracer {
4444
private readonly _httpTextFormat: types.HttpTextFormat;
4545
private readonly _sampler: types.Sampler;
4646
private readonly _scopeManager: ScopeManager;
47-
private readonly _logger: Logger;
47+
readonly logger: Logger;
4848

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

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

9090
const span = new Span(
9191
this,
92-
this._logger,
9392
name,
9493
spanContext,
9594
options.kind || types.SpanKind.INTERNAL,

packages/opentelemetry-basic-tracer/src/Span.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import * as types from '@opentelemetry/types';
1818
import { performance } from 'perf_hooks';
1919
import { ReadableSpan } from './export/ReadableSpan';
20+
import { BasicTracer } from './BasicTracer';
2021

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

4344
/** Constructs a new Span instance. */
4445
constructor(
45-
parentTracer: types.Tracer,
46-
logger: types.Logger,
46+
parentTracer: BasicTracer,
4747
spanName: string,
4848
spanContext: types.SpanContext,
4949
kind: types.SpanKind,
@@ -56,7 +56,7 @@ export class Span implements types.Span, ReadableSpan {
5656
this.parentSpanId = parentSpanId;
5757
this.kind = kind;
5858
this.startTime = startTime || performance.now();
59-
this._logger = logger;
59+
this._logger = parentTracer.logger;
6060
}
6161

6262
tracer(): types.Tracer {

packages/opentelemetry-basic-tracer/test/Span.test.ts

+18-35
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import {
2222
TraceOptions,
2323
SpanContext,
2424
} from '@opentelemetry/types';
25-
import { NoopTracer, NoopLogger } from '@opentelemetry/core';
25+
import { BasicTracer } from '../src';
26+
import { NoopScopeManager } from '@opentelemetry/scope-base';
2627

2728
describe('Span', () => {
28-
const tracer = new NoopTracer();
29-
const logger = new NoopLogger();
29+
const tracer = new BasicTracer({
30+
scopeManager: new NoopScopeManager(),
31+
});
3032
const name = 'span1';
3133
const spanContext: SpanContext = {
3234
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
@@ -35,14 +37,14 @@ describe('Span', () => {
3537
};
3638

3739
it('should create a Span instance', () => {
38-
const span = new Span(tracer, logger, name, spanContext, SpanKind.SERVER);
40+
const span = new Span(tracer, name, spanContext, SpanKind.SERVER);
3941
assert.ok(span instanceof Span);
4042
assert.strictEqual(span.tracer(), tracer);
4143
span.end();
4244
});
4345

4446
it('should get the span context of span', () => {
45-
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
47+
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
4648
const context = span.context();
4749
assert.strictEqual(context.traceId, spanContext.traceId);
4850
assert.strictEqual(context.traceOptions, TraceOptions.SAMPLED);
@@ -53,13 +55,13 @@ describe('Span', () => {
5355
});
5456

5557
it('should return true when isRecordingEvents:true', () => {
56-
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
58+
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
5759
assert.ok(span.isRecordingEvents());
5860
span.end();
5961
});
6062

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

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

7173
it('should set an event', () => {
72-
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
74+
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
7375
span.addEvent('sent');
7476
span.addEvent('rev', { attr1: 'value', attr2: 123, attr3: true });
7577
span.end();
@@ -81,14 +83,14 @@ describe('Span', () => {
8183
spanId: '5e0c63257de34c92',
8284
traceOptions: TraceOptions.SAMPLED,
8385
};
84-
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
86+
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
8587
span.addLink(spanContext);
8688
span.addLink(spanContext, { attr1: 'value', attr2: 123, attr3: true });
8789
span.end();
8890
});
8991

9092
it('should set an error status', () => {
91-
const span = new Span(tracer, logger, name, spanContext, SpanKind.CLIENT);
93+
const span = new Span(tracer, name, spanContext, SpanKind.CLIENT);
9294
span.setStatus({
9395
code: CanonicalCode.PERMISSION_DENIED,
9496
message: 'This is an error',
@@ -100,7 +102,6 @@ describe('Span', () => {
100102
const parentId = '5c1c63257de34c67';
101103
const span = new Span(
102104
tracer,
103-
logger,
104105
'my-span',
105106
spanContext,
106107
SpanKind.INTERNAL,
@@ -121,13 +122,7 @@ describe('Span', () => {
121122
});
122123

123124
it('should return ReadableSpan with attributes', () => {
124-
const span = new Span(
125-
tracer,
126-
logger,
127-
'my-span',
128-
spanContext,
129-
SpanKind.CLIENT
130-
);
125+
const span = new Span(tracer, 'my-span', spanContext, SpanKind.CLIENT);
131126
span.setAttribute('attr1', 'value1');
132127
let readableSpan = span.toReadableSpan();
133128
assert.deepStrictEqual(readableSpan.attributes, { attr1: 'value1' });
@@ -150,13 +145,7 @@ describe('Span', () => {
150145
});
151146

152147
it('should return ReadableSpan with links', () => {
153-
const span = new Span(
154-
tracer,
155-
logger,
156-
'my-span',
157-
spanContext,
158-
SpanKind.CLIENT
159-
);
148+
const span = new Span(tracer, 'my-span', spanContext, SpanKind.CLIENT);
160149
span.addLink(spanContext);
161150
let readableSpan = span.toReadableSpan();
162151
assert.strictEqual(readableSpan.links.length, 1);
@@ -193,13 +182,7 @@ describe('Span', () => {
193182
});
194183

195184
it('should return ReadableSpan with events', () => {
196-
const span = new Span(
197-
tracer,
198-
logger,
199-
'my-span',
200-
spanContext,
201-
SpanKind.CLIENT
202-
);
185+
const span = new Span(tracer, 'my-span', spanContext, SpanKind.CLIENT);
203186
span.addEvent('sent');
204187
let readableSpan = span.toReadableSpan();
205188
assert.strictEqual(readableSpan.events.length, 1);
@@ -232,7 +215,7 @@ describe('Span', () => {
232215
});
233216

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

256239
it('should only end a span once', () => {
257-
const span = new Span(tracer, logger, name, spanContext, SpanKind.SERVER);
240+
const span = new Span(tracer, name, spanContext, SpanKind.SERVER);
258241
span.end(1234);
259242
span.end(4567);
260243
assert.strictEqual(span.endTime, 1234);
261244
});
262245

263246
it('should update name', () => {
264-
const span = new Span(tracer, logger, name, spanContext, SpanKind.SERVER);
247+
const span = new Span(tracer, name, spanContext, SpanKind.SERVER);
265248
span.updateName('foo-span');
266249
span.end();
267250

0 commit comments

Comments
 (0)