Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 36f63e6

Browse files
authored
Code Enhancement/Refactor (#403)
* Code Enhancement/Refactor * fix review comments Change console instance to logger instance (ConsoleLogger). Remove return from the logger statement.
1 parent 73f83a1 commit 36f63e6

11 files changed

Lines changed: 136 additions & 151 deletions

File tree

packages/opencensus-core/src/trace/model/no-record/no-record-root-span.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,27 @@ export class NoRecordRootSpan extends NoRecordSpanBase implements
8787
}
8888

8989
/**
90-
* Starts a new child span in the noop root span.
91-
* @param name Span name.
92-
* @param kind Span kind.
93-
* @param parentSpanId Span parent ID.
90+
* Starts a new no record child span in the no record root span.
91+
* @param nameOrOptions Span name string or SpanOptions object.
92+
* @param kind Span kind if not using SpanOptions object.
9493
*/
9594
startChildSpan(
96-
nameOrOptions?: string|types.SpanOptions, kind?: types.SpanKind,
97-
parentSpanId?: string): types.Span {
98-
const newSpan = new NoRecordSpan(this);
99-
let spanName;
100-
let spanKind;
101-
if (typeof nameOrOptions === 'object') {
102-
spanName = nameOrOptions.name;
103-
spanKind = nameOrOptions.kind;
104-
} else {
105-
spanName = nameOrOptions;
106-
spanKind = kind;
107-
}
95+
nameOrOptions?: string|types.SpanOptions,
96+
kind?: types.SpanKind): types.Span {
97+
const noRecordChild = new NoRecordSpan(this);
10898

99+
const spanName =
100+
typeof nameOrOptions === 'object' ? nameOrOptions.name : nameOrOptions;
101+
const spanKind =
102+
typeof nameOrOptions === 'object' ? nameOrOptions.kind : kind;
109103
if (spanName) {
110-
newSpan.name = spanName;
104+
noRecordChild.name = spanName;
111105
}
112106
if (spanKind) {
113-
newSpan.kind = spanKind;
107+
noRecordChild.kind = spanKind;
114108
}
115-
newSpan.start();
116-
return newSpan;
109+
110+
noRecordChild.start();
111+
return noRecordChild;
117112
}
118113
}

packages/opencensus-core/src/trace/model/root-span.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,12 @@ export class RootSpan extends SpanBase implements types.RootSpan {
112112

113113
/**
114114
* Starts a new child span in the root span.
115-
* @param name Span name.
116-
* @param kind Span kind.
117-
* @param parentSpanId Span parent ID.
115+
* @param nameOrOptions Span name string or SpanOptions object.
116+
* @param kind Span kind if not using SpanOptions object.
118117
*/
119118
startChildSpan(
120-
nameOrOptions?: string|types.SpanOptions, kind?: types.SpanKind,
121-
parentSpanId?: string): types.Span {
119+
nameOrOptions?: string|types.SpanOptions,
120+
kind?: types.SpanKind): types.Span {
122121
if (this.ended) {
123122
this.logger.debug(
124123
'calling %s.startSpan() on ended %s %o', this.className,
@@ -132,25 +131,22 @@ export class RootSpan extends SpanBase implements types.RootSpan {
132131
return null;
133132
}
134133
this.numberOfChildrenLocal++;
135-
const newSpan = new Span(this);
136-
let spanName;
137-
let spanKind;
138-
if (typeof nameOrOptions === 'object') {
139-
spanName = nameOrOptions.name;
140-
spanKind = nameOrOptions.kind;
141-
} else {
142-
spanName = nameOrOptions;
143-
spanKind = kind;
144-
}
145134

135+
const child = new Span(this);
136+
137+
const spanName =
138+
typeof nameOrOptions === 'object' ? nameOrOptions.name : nameOrOptions;
139+
const spanKind =
140+
typeof nameOrOptions === 'object' ? nameOrOptions.kind : kind;
146141
if (spanName) {
147-
newSpan.name = spanName;
142+
child.name = spanName;
148143
}
149144
if (spanKind) {
150-
newSpan.kind = spanKind;
145+
child.kind = spanKind;
151146
}
152-
newSpan.start();
153-
this.spansLocal.push(newSpan);
154-
return newSpan;
147+
148+
child.start();
149+
this.spansLocal.push(child);
150+
return child;
155151
}
156152
}

packages/opencensus-core/src/trace/model/tracer.ts

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ export class CoreTracer implements types.Tracer {
3939
private config: configTypes.TracerConfig;
4040
/** A list of end span event listeners */
4141
private eventListenersLocal: types.SpanEventListener[] = [];
42-
/** A list of ended root spans */
43-
// @ts-ignore
44-
private endedTraces: types.RootSpan[] = [];
4542
/** Bit to represent whether trace is sampled or not. */
4643
private readonly IS_SAMPLED = 0x1;
4744
/** A sampler used to make sample decisions */
@@ -162,34 +159,31 @@ export class CoreTracer implements types.Tracer {
162159
});
163160
}
164161

162+
/** Notifies listeners of the span start. */
165163
onStartSpan(root: types.RootSpan): void {
166-
if (this.active) {
167-
if (!root) {
168-
return this.logger.debug('cannot start trace - no active trace found');
169-
}
170-
if (this.currentRootSpan !== root) {
171-
this.logger.debug(
172-
'currentRootSpan != root on notifyStart. Need more investigation.');
173-
}
174-
this.notifyStartSpan(root);
164+
if (!this.active) return;
165+
if (!root) {
166+
return this.logger.debug('cannot start trace - no active trace found');
167+
}
168+
if (this.currentRootSpan !== root) {
169+
this.logger.debug(
170+
'currentRootSpan != root on notifyStart. Need more investigation.');
175171
}
172+
this.notifyStartSpan(root);
176173
}
177174

178-
/**
179-
* Is called when a span is ended.
180-
* @param root The ended span.
181-
*/
175+
/** Notifies listeners of the span end. */
182176
onEndSpan(root: types.RootSpan): void {
183-
if (this.active) {
184-
if (!root) {
185-
return this.logger.debug('cannot end trace - no active trace found');
186-
}
187-
if (this.currentRootSpan !== root) {
188-
this.logger.debug(
189-
'currentRootSpan != root on notifyEnd. Need more investigation.');
190-
}
191-
this.notifyEndSpan(root);
177+
if (!this.active) return;
178+
if (!root) {
179+
this.logger.debug('cannot end trace - no active trace found');
180+
return;
181+
}
182+
if (this.currentRootSpan !== root) {
183+
this.logger.debug(
184+
'currentRootSpan != root on notifyEnd. Need more investigation.');
192185
}
186+
this.notifyEndSpan(root);
193187
}
194188

195189
/**
@@ -221,15 +215,11 @@ export class CoreTracer implements types.Tracer {
221215
}
222216

223217
private notifyEndSpan(root: types.RootSpan) {
224-
if (this.active) {
225-
this.logger.debug('starting to notify listeners the end of rootspans');
226-
if (this.eventListenersLocal && this.eventListenersLocal.length > 0) {
227-
for (const listener of this.eventListenersLocal) {
228-
listener.onEndSpan(root);
229-
}
218+
this.logger.debug('starting to notify listeners the end of rootspans');
219+
if (this.eventListenersLocal && this.eventListenersLocal.length > 0) {
220+
for (const listener of this.eventListenersLocal) {
221+
listener.onEndSpan(root);
230222
}
231-
} else {
232-
this.logger.debug('this tracer is inactivate cant notify endspan');
233223
}
234224
}
235225

@@ -240,9 +230,8 @@ export class CoreTracer implements types.Tracer {
240230

241231
/**
242232
* Starts a span.
243-
* @param name The span name.
244-
* @param kind optional The span kind.
245-
* @param parentSpanId The parent span ID.
233+
* @param nameOrOptions Span name string or SpanOptions object.
234+
* @param kind Span kind if not using SpanOptions object.
246235
*/
247236
startChildSpan(
248237
nameOrOptions?: string|types.SpanOptions,
@@ -277,7 +266,7 @@ export class CoreTracer implements types.Tracer {
277266
* This is necessary in order to create child spans correctly in event
278267
* handlers.
279268
* @param emitter An event emitter whose handlers should have
280-
* the trace context binded to them.
269+
* the trace context binded to them.
281270
*/
282271
wrapEmitter(emitter: NodeJS.EventEmitter): void {
283272
if (!this.active) {

packages/opencensus-core/src/trace/model/types.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ export interface RootSpan extends Span {
464464
readonly numberOfChildren: number;
465465

466466
/** Starts a new Span instance in the RootSpan instance */
467-
startChildSpan(name?: string, kind?: SpanKind, parentSpanId?: string): Span;
467+
startChildSpan(name?: string, kind?: SpanKind): Span;
468468
startChildSpan(options?: SpanOptions): Span;
469469
startChildSpan(nameOrOptions?: string|SpanOptions, kind?: SpanKind): Span;
470470
}
@@ -529,12 +529,11 @@ export interface Tracer extends SpanEventListener {
529529
/**
530530
* Start a new Span instance to the currentRootSpan
531531
* @param name Span name
532-
* @param type Span type
533-
* @param parentSpanId Parent SpanId
532+
* @param kind Span kind
534533
* @param options Span Options
535534
* @returns The new Span instance started
536535
*/
537-
startChildSpan(name?: string, type?: SpanKind, parentSpanId?: string): Span;
536+
startChildSpan(name?: string, kind?: SpanKind): Span;
538537
startChildSpan(options?: SpanOptions): Span;
539538

540539
/**

packages/opencensus-core/test/test-console-exporter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('ConsoleLogExporter', () => {
9191
const exporter = new ConsoleExporter(defaultBufferConfig);
9292
const rootSpan = new RootSpan(tracer, name, kind, traceId, parentSpanId);
9393
rootSpan.start();
94-
rootSpan.startChildSpan('name', SpanKind.UNSPECIFIED, rootSpan.traceId);
94+
rootSpan.startChildSpan('name', SpanKind.UNSPECIFIED);
9595
const queue: RootSpan[] = [rootSpan];
9696

9797
return exporter.publish(queue).then(() => {

packages/opencensus-core/test/test-tracer.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -343,34 +343,31 @@ describe('Tracer', () => {
343343
});
344344

345345
it('should start a span with SpanObject', () => {
346-
let spanWithObject: types.Span;
347346
tracer.startRootSpan(options, (rootSpan) => {
348-
spanWithObject = tracer.startChildSpan(
347+
const spanWithObject = tracer.startChildSpan(
349348
{name: 'my-span', kind: types.SpanKind.SERVER});
349+
assert.ok(spanWithObject.started);
350+
assert.strictEqual(spanWithObject.name, 'my-span');
351+
assert.strictEqual(spanWithObject.kind, types.SpanKind.SERVER);
350352
});
351-
assert.ok(spanWithObject.started);
352-
assert.strictEqual(spanWithObject.name, 'my-span');
353-
assert.strictEqual(spanWithObject.kind, types.SpanKind.SERVER);
354353
});
355354

356355
it('should start a span with SpanObject-name', () => {
357-
let spanWithObject: types.Span;
358356
tracer.startRootSpan(options, (rootSpan) => {
359-
spanWithObject = tracer.startChildSpan({name: 'my-span1'});
357+
const spanWithObject = tracer.startChildSpan({name: 'my-span1'});
358+
assert.ok(spanWithObject.started);
359+
assert.strictEqual(spanWithObject.name, 'my-span1');
360+
assert.strictEqual(spanWithObject.kind, types.SpanKind.UNSPECIFIED);
360361
});
361-
assert.ok(spanWithObject.started);
362-
assert.strictEqual(spanWithObject.name, 'my-span1');
363-
assert.strictEqual(spanWithObject.kind, types.SpanKind.UNSPECIFIED);
364362
});
365363

366364
it('should start a span without params', () => {
367-
let spanWithObject: types.Span;
368365
tracer.startRootSpan(options, (rootSpan) => {
369-
spanWithObject = tracer.startChildSpan();
366+
const spanWithObject = tracer.startChildSpan();
367+
assert.ok(spanWithObject.started);
368+
assert.strictEqual(spanWithObject.name, null);
369+
assert.strictEqual(spanWithObject.kind, types.SpanKind.UNSPECIFIED);
370370
});
371-
assert.ok(spanWithObject.started);
372-
assert.strictEqual(spanWithObject.name, null);
373-
assert.strictEqual(spanWithObject.kind, types.SpanKind.UNSPECIFIED);
374371
});
375372
});
376373

packages/opencensus-nodejs/src/trace/instrumentation/plugin-loader.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Logger, NamedPluginConfig, Plugin, PluginConfig, PluginNames, Tracer} from '@opencensus/core';
17+
import {Logger, Plugin, PluginConfig, PluginNames, Tracer} from '@opencensus/core';
1818
import * as fs from 'fs';
1919
import * as path from 'path';
2020
import * as hook from 'require-in-the-middle';
21-
2221
import {Constants} from '../constants';
2322

2423
enum HookState {

packages/opencensus-nodejs/src/trace/tracing.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
* limitations under the License.
1515
*/
1616
import * as core from '@opencensus/core';
17-
import {Logger, logger} from '@opencensus/core';
18-
17+
import {logger} from '@opencensus/core';
1918
import * as extend from 'extend';
20-
2119
import {defaultConfig} from './config/default-config';
2220
import {Constants} from './constants';
2321
import {PluginLoader} from './instrumentation/plugin-loader';
2422

23+
const NOOP_EXPORTER = new core.NoopExporter();
2524

2625
/** Implements a Tracing. */
2726
export class Tracing implements core.Tracing {
@@ -33,12 +32,12 @@ export class Tracing implements core.Tracing {
3332
private defaultPlugins: core.PluginNames;
3433
/** A configuration object to start the tracing */
3534
private configLocal: core.Config = {};
36-
/** An object to log information to */
37-
private logger: core.Logger = null;
35+
/** An object to log information to. Logs to the JS console by default. */
36+
private logger: core.Logger = logger.logger();
3837
/** Singleton instance */
3938
private static singletonInstance: core.Tracing;
4039
/** Indicates if the tracing is active */
41-
private activeLocal: boolean;
40+
private activeLocal = false;
4241

4342
/** Constructs a new TracingImpl instance. */
4443
constructor() {
@@ -95,15 +94,15 @@ export class Tracing implements core.Tracing {
9594
this.tracer.stop();
9695
this.pluginLoader.unloadPlugins();
9796
this.configLocal = {};
98-
this.logger = null;
97+
this.logger = logger.logger();
9998
}
10099

101100

102101
/** Gets the exporter. */
103102
get exporter(): core.Exporter {
104103
return this.configLocal.exporter ?
105104
this.configLocal.exporter as core.Exporter :
106-
null;
105+
NOOP_EXPORTER;
107106
}
108107

109108
/**
@@ -128,7 +127,7 @@ export class Tracing implements core.Tracing {
128127
*/
129128
unregisterExporter(exporter: core.Exporter): core.Tracing {
130129
this.tracer.unregisterSpanEventListener(exporter);
131-
this.configLocal.exporter = null;
130+
this.configLocal.exporter = NOOP_EXPORTER;
132131
return this;
133132
}
134133
}

packages/opencensus-nodejs/test/test-plugin-loader.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616

1717
import {CoreTracer, logger} from '@opencensus/core';
1818
import * as assert from 'assert';
19-
import * as path from 'path';
20-
2119
import {Constants} from '../src/trace/constants';
2220
import {PluginLoader} from '../src/trace/instrumentation/plugin-loader';
2321

24-
2522
const INSTALLED_PLUGINS_PATH = `${__dirname}/instrumentation/node_modules`;
2623
const TEST_MODULES = [
2724
'simple-module' // this module exist and has a plugin
@@ -130,7 +127,6 @@ describe('Plugin Loader', () => {
130127
const pluginLoader = new PluginLoader(log, tracer);
131128
assert.strictEqual(pluginLoader.plugins.length, 0);
132129
pluginLoader.loadPlugins(plugins);
133-
const http = require(TEST_MODULES[2]);
134130
intercept((txt: string) => {
135131
assert.ok(txt.indexOf('error') >= 0);
136132
})();

0 commit comments

Comments
 (0)