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

Commit eba9b69

Browse files
eduardoemerysilva-fabio
authored andcommitted
refactor: add logger to ather classes
1 parent 5095782 commit eba9b69

14 files changed

Lines changed: 374 additions & 845 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"repository": "census-instrumentation/opencensus-node",
88
"scripts": {
99
"postinstall": "npm run bootstrap;npm run build;",
10-
"build" : "node_modules/.bin/lerna run build",
10+
"build": "node_modules/.bin/lerna run build",
1111
"bootstrap": "node_modules/.bin/lerna bootstrap",
1212
"bump": "node_modules/.bin/lerna publish"
1313
},

packages/opencensus-core/package-lock.json

Lines changed: 254 additions & 816 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-core/src/common/console-logger.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const logDriver = require('log-driver');
2424
*/
2525
class ConsoleLogger implements Logger {
2626
// tslint:disable:no-any
27-
private logger: any;
27+
private logger: any;
2828
static LEVELS = ['error', 'warn', 'info', 'debug', 'silly'];
2929

3030
/**
@@ -39,8 +39,10 @@ class ConsoleLogger implements Logger {
3939
opt = options || {};
4040
}
4141

42-
this.logger =
43-
logDriver({levels: ConsoleLogger.LEVELS, level: opt.level || 'error'});
42+
this.logger = logDriver({
43+
levels: ConsoleLogger.LEVELS,
44+
level: opt.level || 'silly'
45+
});
4446
}
4547

4648
/**

packages/opencensus-core/src/exporters/buffer.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import * as uuidv4 from 'uuid/v4';
1919
import {debug} from '../internal/util';
2020
import {RootSpan} from '../trace/model/types';
2121
import {OnEndSpanEventListener} from '../trace/model/types';
22-
22+
import {Logger} from '../common/types';
2323
import {Exporter} from './types';
2424
import {Config, BufferConfig} from '../trace/config/types';
2525

26+
import * as logger from '../common/consolelogger';
27+
2628

2729
/** Controls the sending of traces to exporters. */
2830
export class Buffer {
@@ -38,6 +40,8 @@ export class Buffer {
3840
private resetTimeout = false;
3941
/** Indicates when the buffer timeout is running */
4042
private bufferTimeoutInProgress = false;
43+
/** An object to log information to */
44+
logger: Logger;
4145

4246
/**
4347
* Constructs a new Buffer instance.
@@ -46,6 +50,7 @@ export class Buffer {
4650
*/
4751
constructor(exporter: Exporter, config: BufferConfig) {
4852
this.exporter = exporter;
53+
this.logger = config.logger || logger();
4954
this.bufferSize = config.bufferSize;
5055
this.bufferTimeout = config.bufferTimeout;
5156
return this;
@@ -66,7 +71,7 @@ export class Buffer {
6671
*/
6772
addToBuffer(trace: RootSpan) {
6873
this.queue.push(trace);
69-
debug('BUFFER: added new trace');
74+
this.logger.debug('BUFFER: added new trace');
7075

7176
if (this.queue.length > this.bufferSize) {
7277
this.flush();
@@ -83,13 +88,13 @@ export class Buffer {
8388

8489
/** Reset the buffer timeout */
8590
private resetBufferTimeout() {
86-
debug('BUFFER: reset timeout');
91+
this.logger.debug('BUFFER: reset timeout');
8792
this.resetTimeout = true;
8893
}
8994

9095
/** Start the buffer timeout, when finished calls flush method */
9196
private setBufferTimeout() {
92-
debug('BUFFER: set timerout');
97+
this.logger.debug('BUFFER: set timerout');
9398
this.bufferTimeoutInProgress = true;
9499

95100
setTimeout(() => {

packages/opencensus-core/src/exporters/console-exporter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
import {Exporter, ExporterConfig} from '../exporters/types';
1818
import {RootSpan} from '../trace/model/types';
1919
import {Buffer} from './buffer';
20+
import {Logger} from '../common/types';
2021

2122

2223
/** Do not send span data */
2324
export class NoopExporter implements Exporter {
25+
logger: Logger
2426
onEndSpan(root: RootSpan) {}
2527
publish(rootSpans: RootSpan[]) {}
2628
}
@@ -29,6 +31,7 @@ export class NoopExporter implements Exporter {
2931
export class ConsoleLogExporter implements Exporter {
3032
/** Buffer object to store the spans. */
3133
private buffer: Buffer;
34+
logger: Logger;
3235

3336
/**
3437
* Constructs a new ConsoleLogExporter instance.
@@ -37,6 +40,7 @@ export class ConsoleLogExporter implements Exporter {
3740
*/
3841
constructor(config: ExporterConfig) {
3942
this.buffer = new Buffer(this, config);
43+
this.logger = config.logger;
4044
}
4145

4246
/**

packages/opencensus-core/src/exporters/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
import {OnEndSpanEventListener, RootSpan} from '../trace/model/types';
1919
import {BufferConfig} from '../trace/config/types';
20+
import {Logger} from '../common/types'
2021

2122
/** Defines an exporter interface. */
2223
export interface Exporter extends OnEndSpanEventListener {
24+
/** An object to log information to */
25+
logger: Logger
2326

2427
/**
2528
* Sends a list of root spans to the service.

packages/opencensus-core/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from './trace/model/types';
2121
export * from './trace/config/types';
2222
export * from './trace/instrumentation/types';
2323
export * from './exporters/types';
24+
export * from './common/types';
2425

2526
// domain models impls
2627
export * from './trace/model/rootspan';
@@ -38,4 +39,4 @@ export * from './exporters/buffer';
3839
export * from './exporters/consolelog-exporter';
3940

4041
// util
41-
export * from './internal/util';
42+
export * from './internal/util';

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Exporter} from '../../exporters/types';
22
import {PluginNames} from '../instrumentation/types';
3+
import {Logger} from '../../common/types';
34

45
/** This interface represent the probability of a tracer. */
56
export interface Sampler {
@@ -37,6 +38,7 @@ export interface Sampler {
3738
export interface BufferConfig {
3839
bufferSize?: number;
3940
bufferTimeout?: number;
41+
logger?: Logger;
4042
}
4143

4244
/** Defines tracer configuration parameters */
@@ -45,6 +47,8 @@ export interface TracerConfig {
4547
samplingRate?: number;
4648
/** Determines the ignored (or blacklisted) URLs */
4749
ignoreUrls?: Array<string|RegExp>;
50+
/** A logger object to show infos */
51+
logger?: Logger;
4852
}
4953

5054
/** Available configuration options. */
@@ -53,6 +57,7 @@ export interface TracingConfig {
5357
maximumLabelValueSize?: number;
5458
plugins?: PluginNames;
5559
exporter?: Exporter;
60+
logger?: Logger;
5661
}
5762

5863
export type Config = TracingConfig&TracerConfig&BufferConfig;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {SpanBaseModel} from './spanbasemodel';
2424
import {TracerImpl} from './tracer';
2525
import {OnEndSpanEventListener, RootSpan, Span, TraceContext, TraceOptions, Tracer} from './types';
2626

27+
import * as logger from '../../common/consolelogger';
28+
2729
/** Defines a root span */
2830
export class RootSpanImpl extends SpanBaseModel implements RootSpan {
2931
/** A tracer object */
@@ -50,6 +52,7 @@ export class RootSpanImpl extends SpanBaseModel implements RootSpan {
5052
this.parentSpanId = context.traceContext.spanId || '';
5153
}
5254
this.spansLocal = [];
55+
this.logger = tracer.logger || logger();
5356
}
5457

5558
/** Gets span list from rootspan instance. */
@@ -65,7 +68,7 @@ export class RootSpanImpl extends SpanBaseModel implements RootSpan {
6568
/** Starts a rootspan instance. */
6669
start() {
6770
super.start();
68-
debug(
71+
this.logger.debug(
6972
'starting %s %o', this.className,
7073
{traceId: this.traceId, id: this.id, parentSpanId: this.parentSpanId});
7174
}
@@ -89,7 +92,7 @@ export class RootSpanImpl extends SpanBaseModel implements RootSpan {
8992
* @param span Span ended.
9093
*/
9194
onEndSpan(span: Span) {
92-
debug('ending span %o', {
95+
this.logger.debug('ending span %o', {
9396
id: span.id,
9497
traceId: span.traceId,
9598
name: span.name,
@@ -107,13 +110,13 @@ export class RootSpanImpl extends SpanBaseModel implements RootSpan {
107110
*/
108111
startSpan(name: string, type: string, parentSpanId?: string) {
109112
if (this.ended) {
110-
debug(
113+
this.logger.debug(
111114
'calling %s.startSpan() on ended %s %o', this.className,
112115
this.className, {id: this.id, name: this.name, type: this.type});
113116
return;
114117
}
115118
if (!this.started) {
116-
debug(
119+
this.logger.debug(
117120
'calling %s.startSpan() on un-started %s %o', this.className,
118121
this.className, {id: this.id, name: this.name, type: this.type});
119122
return;

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {debug, randomSpanId} from '../../internal/util';
1919
import {Sampler} from '../config/types';
2020

2121
import {Annotation, Attributes, Link, MessageEvent, Span, TraceContext} from './types';
22+
import {Logger} from '../../common/types';
2223

2324
/** Defines a base model for spans. */
2425
export abstract class SpanBaseModel implements Span {
@@ -33,6 +34,8 @@ export abstract class SpanBaseModel implements Span {
3334
private truncated = false;
3435
/** The Span ID of this span */
3536
readonly id: string;
37+
/** An object to log information to */
38+
logger: Logger;
3639
/** A set of attributes, each in the format [KEY]:[VALUE] */
3740
attributes: Attributes = {};
3841
/** A text annotation with a set of attributes. */
@@ -78,7 +81,7 @@ export abstract class SpanBaseModel implements Span {
7881
*/
7982
get startTime(): Date {
8083
if (!this.clock) {
81-
debug('calling startTime() on null clock');
84+
this.logger.debug('calling startTime() on null clock');
8285
return null;
8386
}
8487

@@ -91,7 +94,7 @@ export abstract class SpanBaseModel implements Span {
9194
*/
9295
get endTime(): Date {
9396
if (!this.clock) {
94-
debug('calling endTime() on null clock');
97+
this.logger.debug('calling endTime() on null clock');
9598
return null;
9699
}
97100

@@ -104,7 +107,7 @@ export abstract class SpanBaseModel implements Span {
104107
*/
105108
get duration(): number {
106109
if (!this.clock) {
107-
debug('calling duration() on null clock');
110+
this.logger.debug('calling duration() on null clock');
108111
return null;
109112
}
110113

@@ -176,7 +179,7 @@ export abstract class SpanBaseModel implements Span {
176179
/** Starts the span. */
177180
start() {
178181
if (this.started) {
179-
debug(
182+
this.logger.debug(
180183
'calling %s.start() on already started %s %o', this.className,
181184
this.className, {id: this.id, name: this.name, type: this.type});
182185
return;
@@ -188,13 +191,13 @@ export abstract class SpanBaseModel implements Span {
188191
/** Ends the span. */
189192
end(): void {
190193
if (this.ended) {
191-
debug(
194+
this.logger.debug(
192195
'calling %s.end() on already ended %s %o', this.className,
193196
this.className, {id: this.id, name: this.name, type: this.type});
194197
return;
195198
}
196199
if (!this.started) {
197-
debug(
200+
this.logger.debug(
198201
'calling %s.end() on un-started %s %o', this.className,
199202
this.className, {id: this.id, name: this.name, type: this.type});
200203
return;
@@ -210,6 +213,7 @@ export abstract class SpanBaseModel implements Span {
210213
// TODO: review
211214
this.truncated = true;
212215
this.end();
213-
debug('truncating %s %o', this.className, {id: this.id, name: this.name});
216+
this.logger.debug(
217+
'truncating %s %o', this.className, {id: this.id, name: this.name});
214218
}
215219
}

0 commit comments

Comments
 (0)