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

Commit db46f4f

Browse files
eduardoemerysilva-fabio
authored andcommitted
refactor: address opencensus-core TODOs (#47)
* refactor: changes to address opencensus-core TODOs * refactor: reveiw TODO for clearCurrentTrace when using continuation-local-storage * refactor(fix): changes to address review comments Co-authored-by: Fabio Silva <fgs@cesar.org.br>
1 parent 17dca7d commit db46f4f

15 files changed

Lines changed: 33 additions & 43 deletions

File tree

packages/opencensus-core/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
},
4040
"devDependencies": {
4141
"@types/continuation-local-storage": "^3.2.1",
42-
"@types/debug": "0.0.30",
4342
"@types/mocha": "^2.2.48",
4443
"@types/node": "^9.4.7",
4544
"@types/semver": "^5.5.0",
4645
"@types/shimmer": "^1.0.1",
4746
"@types/uuid": "^3.4.3",
4847
"gts": "^0.5.4",
48+
"intercept-stdout": "^0.1.2",
4949
"mocha": "^5.0.4",
5050
"ncp": "^2.0.0",
5151
"nyc": "11.6.0",
@@ -54,8 +54,6 @@
5454
},
5555
"dependencies": {
5656
"continuation-local-storage": "^3.2.1",
57-
"debug": "^3.1.0",
58-
"intercept-stdout": "^0.1.2",
5957
"log-driver": "^1.2.7",
6058
"semver": "^5.5.0",
6159
"shimmer": "^1.2.0",

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ export class ConsoleLogger implements types.Logger {
2727
static LEVELS = ['silent', 'error', 'warn', 'info', 'debug', 'silly'];
2828
level: string;
2929

30-
31-
// TODO: reevaluate options to accept numbers as a parameter
32-
3330
/**
3431
* Constructs a new ConsoleLogger instance
3532
* @param options A logger configuration object.
@@ -104,19 +101,15 @@ export class ConsoleLogger implements types.Logger {
104101
}
105102
}
106103

107-
108-
// TODO: reevaluate the need to create a new logger instance on every call to
109-
// logger();
110-
111104
/**
112-
* Function logger exported to others classes.
105+
* Function logger exported to others classes. Inspired by:
106+
* https://github.com/cainus/logdriver/blob/bba1761737ca72f04d6b445629848538d038484a/index.js#L50
113107
* @param options A logger options or strig to logger in console
114108
*/
115109
// tslint:disable-next-line:no-any
116-
const logger: any = (options?: types.LoggerOptions|string|number) => {
117-
const aLogger = new ConsoleLogger(options);
118-
logger['logger'] = aLogger;
119-
return aLogger;
120-
};
110+
const logger: any =
111+
(options?: types.LoggerOptions|string|number): types.Logger => {
112+
return new ConsoleLogger(options);
113+
};
121114

122115
export {logger};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type LogFunction = (message: any, ...args: any[]) => void;
1919

2020
/** Defines an logger interface. */
2121
export interface Logger {
22+
level: string;
2223
error: LogFunction;
2324
warn: LogFunction;
2425
info: LogFunction;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export class NoopExporter implements types.Exporter {
2525
logger: loggerTypes.Logger;
2626
onStartSpan(root: modelTypes.RootSpan) {}
2727
onEndSpan(root: modelTypes.RootSpan) {}
28-
publish(rootSpans: modelTypes.RootSpan[]) {}
28+
publish(rootSpans: modelTypes.RootSpan[]) {
29+
return Promise.resolve();
30+
}
2931
}
3032

3133
/** Format and sends span data to the console. */
@@ -72,5 +74,6 @@ export class ConsoleExporter implements types.Exporter {
7274
`${SPANS_STR.join('\n')}`);
7375
console.log(`${result}`);
7476
});
77+
return Promise.resolve();
7578
}
7679
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export interface Exporter extends modelTypes.SpanEventListener {
2525
* Sends a list of root spans to the service.
2626
* @param rootSpans A list of root spans to publish.
2727
*/
28-
publish(rootSpans: modelTypes.RootSpan[]): void;
28+
29+
publish(rootSpans: modelTypes.RootSpan[]): Promise<number|string|void>;
2930
}
3031

3132
export type ExporterConfig = configTypes.BufferConfig;

packages/opencensus-core/src/internal/cls-ah.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AsyncHooksNamespace implements CLSNamespace {
3737
}
3838

3939
get active(): Context {
40-
throw new Error('Not implemented');
40+
return current;
4141
}
4242

4343
createContext(): Context {

packages/opencensus-core/src/internal/cls.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ const useAsyncHooks: boolean = semver.satisfies(
2727
process.version, '>=8'); //&&
2828
// !!process.env.GCLOUD_TRACE_NEW_CONTEXT;
2929

30-
import * as Debug from 'debug';
31-
const debug = Debug('opencensus');
32-
debug('useAsyncHooks = %s', useAsyncHooks);
33-
3430
const cls: typeof CLS = useAsyncHooks ? require('./cls-ah') : CLS;
3531

3632

packages/opencensus-core/src/internal/util.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
import * as crypto from 'crypto';
2121

22-
23-
// TODO: rethink this snippet aproach
24-
2522
// Use 6 bytes of randomness only as JS numbers are doubles not 64-bit ints.
2623
const SPAN_ID_RANDOM_BYTES = 6;
2724

packages/opencensus-core/src/trace/instrumentation/base-plugin.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ import * as types from './types';
2626
* Maps a name (key) representing a internal file module and its exports
2727
*/
2828
export type ModuleExportsMapping = {
29-
// tslint:disable:no-any
29+
// tslint:disable-next-line:no-any
3030
[key: string]: any;
3131
};
3232

3333

3434
/** This class represent the base to patch plugin. */
3535
export abstract class BasePlugin implements types.Plugin {
3636
/** Exports from the nodejs module to be instrumented */
37-
// tslint:disable:no-any
37+
// tslint:disable-next-line:no-any
3838
protected moduleExports: any;
3939
/** The module name */
4040
protected moduleName: string;
@@ -66,8 +66,8 @@ export abstract class BasePlugin implements types.Plugin {
6666
* @param version module version description
6767
* @param basedir module absolute path
6868
*/
69-
// tslint:disable:no-any
7069
private setPluginContext(
70+
// tslint:disable-next-line:no-any
7171
moduleExports: any, tracer: modelTypes.Tracer, version: string,
7272
basedir?: string) {
7373
this.moduleExports = moduleExports;
@@ -78,7 +78,6 @@ export abstract class BasePlugin implements types.Plugin {
7878
this.internalFilesExports = this.loadInternalFiles();
7979
}
8080

81-
8281
/**
8382
* Method that enables the instrumentation patch.
8483
*
@@ -91,9 +90,9 @@ export abstract class BasePlugin implements types.Plugin {
9190
* @param version version of the current instaled module to patch
9291
* @param basedir module absolute path
9392
*/
94-
enable(
95-
// tslint:disable:no-any
96-
moduleExports: any, tracer: modelTypes.Tracer, version: string,
93+
enable<T>(
94+
// tslint:disable-next-line:no-any
95+
moduleExports: T, tracer: modelTypes.Tracer, version: string,
9796
basedir: string) {
9897
this.setPluginContext(moduleExports, tracer, version, basedir);
9998
return this.applyPatch();
@@ -108,10 +107,10 @@ export abstract class BasePlugin implements types.Plugin {
108107
* This method implements the GoF Template Method Pattern,
109108
* 'applyPatch' is the variant part, each instrumentation should
110109
* implement its own version, 'enable' method is the invariant.
111-
* Wil be called when enable is called.
110+
* It will be called when enable is called.
112111
*
113112
*/
114-
// tslint:disable:no-any
113+
// tslint:disable-next-line:no-any
115114
protected abstract applyPatch(): any;
116115
protected abstract applyUnpatch(): void;
117116

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ export class RootSpan extends SpanBase implements types.RootSpan {
7474
this.tracer.onStartSpan(this);
7575
}
7676

77-
// TODO: review end() behavior if it should throw an error when it is called
78-
// before start()
7977
/** Ends a rootspan instance. */
8078
end() {
8179
super.end();

0 commit comments

Comments
 (0)