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

Commit 7dca176

Browse files
committed
refactor: update and standardize use of types module namespace
1 parent d01e0f4 commit 7dca176

19 files changed

Lines changed: 135 additions & 156 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Logger, LoggerOptions} from './types';
17+
import * as types from './types';
1818

1919
const logDriver = require('log-driver');
2020

2121

2222
/**
2323
* This class represente a console-logger
2424
*/
25-
class ConsoleLogger implements Logger {
25+
class ConsoleLogger implements types.Logger {
2626
// tslint:disable:no-any
2727
private logger: any;
2828
static LEVELS = ['error', 'warn', 'info', 'debug', 'silly'];
@@ -31,8 +31,8 @@ class ConsoleLogger implements Logger {
3131
* Constructs a new ConsoleLogger instance
3232
* @param options A logger configuration object.
3333
*/
34-
constructor(options?: LoggerOptions|string) {
35-
let opt: LoggerOptions = {};
34+
constructor(options?: types.LoggerOptions|string) {
35+
let opt: types.LoggerOptions = {};
3636
if (typeof options === 'string') {
3737
opt = {level: options};
3838
} else {
@@ -102,7 +102,7 @@ let defaultLogger = null;
102102
* Function logger exported to others classes.
103103
* @param options A logger options or strig to logger in console
104104
*/
105-
const logger = (options?: LoggerOptions|string) => {
105+
const logger = (options?: types.LoggerOptions|string) => {
106106
defaultLogger = new ConsoleLogger(options);
107107
logger['logger'] = defaultLogger;
108108
return defaultLogger;

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,17 @@
1515
*/
1616

1717
import * as uuidv4 from 'uuid/v4';
18-
19-
import {debug} from '../internal/util';
20-
import {RootSpan} from '../trace/model/types';
21-
import {OnEndSpanEventListener} from '../trace/model/types';
22-
import {Logger} from '../common/types';
23-
import {Exporter} from './types';
24-
import {Config, BufferConfig} from '../trace/config/types';
25-
18+
import * as types from './types';
19+
import * as modelTypes from '../trace/model/types';
20+
import * as configTypes from '../trace/config/types';
21+
import * as loggerTypes from '../common/types';
2622
import * as logger from '../common/console-logger';
2723

2824

2925
/** Controls the sending of traces to exporters. */
3026
export class Buffer {
3127
/** The service to send the collected spans. */
32-
private exporter: Exporter;
28+
private exporter: types.Exporter;
3329
/** Maximum size of a buffer. */
3430
private bufferSize: number;
3531
/** Max time for a buffer can wait before being sent */
@@ -39,16 +35,16 @@ export class Buffer {
3935
/** Indicates when the buffer timeout is running */
4036
private bufferTimeoutInProgress = false;
4137
/** An object to log information to */
42-
logger: Logger;
38+
logger: loggerTypes.Logger;
4339
/** Trace queue of a buffer */
44-
queue: RootSpan[] = [];
40+
queue: modelTypes.RootSpan[] = [];
4541

4642
/**
4743
* Constructs a new Buffer instance.
4844
* @param exporter The service to send the collected spans.
4945
* @param config A buffer configuration object to create a buffer.
5046
*/
51-
constructor(exporter: Exporter, config: BufferConfig) {
47+
constructor(exporter: types.Exporter, config: configTypes.BufferConfig) {
5248
this.exporter = exporter;
5349
this.logger = config.logger || logger.logger();
5450
this.bufferSize = config.bufferSize;
@@ -69,7 +65,7 @@ export class Buffer {
6965
* Add a trace (rootSpan) in the buffer.
7066
* @param trace RootSpan to be added in the buffer.
7167
*/
72-
addToBuffer(trace: RootSpan) {
68+
addToBuffer(trace: modelTypes.RootSpan) {
7369
this.queue.push(trace);
7470
this.logger.debug('BUFFER: added new trace');
7571

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Exporter, ExporterConfig} from '../exporters/types';
18-
import {RootSpan} from '../trace/model/types';
19-
import {Buffer} from './buffer';
20-
import {Logger} from '../common/types';
17+
import * as types from './types';
18+
import * as modelTypes from '../trace/model/types';
19+
import * as loggerTypes from '../common/types';
2120

21+
import {Buffer} from './buffer';
2222

2323
/** Do not send span data */
24-
export class NoopExporter implements Exporter {
25-
logger: Logger;
26-
onEndSpan(root: RootSpan) {}
27-
publish(rootSpans: RootSpan[]) {}
24+
export class NoopExporter implements types.Exporter {
25+
logger: loggerTypes.Logger;
26+
onEndSpan(root: modelTypes.RootSpan) {}
27+
publish(rootSpans: modelTypes.RootSpan[]) {}
2828
}
2929

3030
/** Format and sends span data to the console. */
31-
export class ConsoleExporter implements Exporter {
31+
export class ConsoleExporter implements types.Exporter {
3232
/** Buffer object to store the spans. */
3333
private buffer: Buffer;
34-
logger: Logger;
34+
logger: loggerTypes.Logger;
3535

3636
/**
3737
* Constructs a new ConsoleLogExporter instance.
3838
* @param config Exporter configuration object to create a console log
3939
* exporter.
4040
*/
41-
constructor(config: ExporterConfig) {
41+
constructor(config: types.ExporterConfig) {
4242
this.buffer = new Buffer(this, config);
4343
this.logger = config.logger;
4444
}
@@ -47,15 +47,15 @@ export class ConsoleExporter implements Exporter {
4747
* Event called when a span is ended.
4848
* @param root Ended span.
4949
*/
50-
onEndSpan(root: RootSpan) {
50+
onEndSpan(root: modelTypes.RootSpan) {
5151
this.buffer.addToBuffer(root);
5252
}
5353

5454
/**
5555
* Sends the spans information to the console.
5656
* @param rootSpans
5757
*/
58-
publish(rootSpans: RootSpan[]) {
58+
publish(rootSpans: modelTypes.RootSpan[]) {
5959
rootSpans.map((root) => {
6060
const ROOT_STR: string = (`
6161
RootSpan: {traceId: ${root.traceId},

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
*/
1616

1717

18-
import {OnEndSpanEventListener, RootSpan} from '../trace/model/types';
19-
import {BufferConfig} from '../trace/config/types';
20-
import {Logger} from '../common/types';
18+
import * as modelTypes from '../trace/model/types';
19+
import * as configTypes from '../trace/config/types';
20+
import * as loggerTypes from '../common/types';
2121

2222
/** Defines an exporter interface. */
23-
export interface Exporter extends OnEndSpanEventListener {
23+
export interface Exporter extends modelTypes.OnEndSpanEventListener {
2424
/** An object to log information to */
25-
logger: Logger;
25+
logger: loggerTypes.Logger;
2626

2727
/**
2828
* Sends a list of root spans to the service.
2929
* @param rootSpans A list of root spans to publish.
3030
*/
31-
publish(rootSpans: RootSpan[]);
31+
publish(rootSpans: modelTypes.RootSpan[]);
3232
}
3333

34-
export type ExporterConfig = BufferConfig;
34+
export type ExporterConfig = configTypes.BufferConfig;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2018 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// all classes
18+
19+
// domain models impls
20+
export * from './trace/model/root-span';
21+
export * from './trace/model/span';
22+
export * from './trace/model/tracer';
23+
24+
// sampler impl
25+
export * from './trace/sampler/sampler';
26+
27+
// base instrumetation class
28+
export * from './trace/instrumentation/base-plugin';
29+
30+
// console exporter and buffer impls
31+
export * from './exporters/buffer';
32+
export * from './exporters/console-exporter';

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

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

17+
// all types
1718
export * from './trace/types';
1819
export * from './trace/model/types';
1920
export * from './trace/config/types';

packages/opencensus-core/src/index.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,8 @@
1515
*/
1616

1717

18-
// opencensus core api interfaces
19-
export * from './trace/types';
20-
export * from './trace/model/types';
21-
export * from './trace/config/types';
22-
export * from './trace/sampler/types';
23-
export * from './trace/instrumentation/types';
24-
export * from './exporters/types';
25-
export * from './common/types';
26-
27-
// base instrumetation class
28-
export * from './trace/instrumentation/base-plugin';
29-
30-
// console exporter and buffer impls
31-
export * from './exporters/buffer';
32-
export * from './exporters/console-exporter';
33-
34-
// util
35-
export * from './internal/util';
36-
18+
import * as logger from './common/console-logger';
19+
import * as classes from './index-classes';
3720
import * as types from './index-types';
38-
export {types};
21+
22+
export {classes, logger, types};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import * as CLS from 'continuation-local-storage';
1818
import * as semver from 'semver';
1919

2020
import * as cls_ah from './cls-ah';
21-
import {debug} from './util';
2221

2322
export type Namespace = CLS.Namespace;
2423
export type Func<T> = CLS.Func<T>;
@@ -27,6 +26,8 @@ const useAsyncHooks: boolean = semver.satisfies(
2726
process.version, '>=8'); //&&
2827
// !!process.env.GCLOUD_TRACE_NEW_CONTEXT;
2928

29+
import * as Debug from 'debug';
30+
const debug = Debug('opencensus');
3031
debug('useAsyncHooks = %s', useAsyncHooks);
3132

3233
const cls: typeof CLS =

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616

1717
import * as crypto from 'crypto';
18-
import * as Debug from 'debug';
19-
20-
export const debug = Debug('opencensus');
2118

2219

2320
// TODO: rethink this snippet aproach

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
import * as shimmer from 'shimmer';
1717
import * as types from './types';
18-
import {Tracer} from '../model/types';
18+
import * as modelTypes from '../model/types';
1919

2020
/** This class represent the base to patch plugin. */
2121
export abstract class BasePlugin implements types.Plugin {
@@ -26,7 +26,7 @@ export abstract class BasePlugin implements types.Plugin {
2626
/** The module name */
2727
protected moduleName: string;
2828
/** A tracer object. */
29-
protected tracer: Tracer;
29+
protected tracer: modelTypes.Tracer;
3030
/** The module version. */
3131
protected version: string;
3232

@@ -45,7 +45,7 @@ export abstract class BasePlugin implements types.Plugin {
4545
* @param version module version description
4646
*/
4747
// tslint:disable:no-any
48-
protected setPluginContext(exporter: any, tracer: Tracer, version: string) {
48+
protected setPluginContext(exporter: any, tracer: modelTypes.Tracer, version: string) {
4949
this.exporter = exporter;
5050
this.tracer = tracer;
5151
this.version = version;
@@ -90,7 +90,7 @@ export abstract class BasePlugin implements types.Plugin {
9090
}
9191

9292
// tslint:disable:no-any
93-
applyPatch(exporter: any, tracer: Tracer, version: string): any{
93+
applyPatch(exporter: any, tracer: modelTypes.Tracer, version: string): any{
9494
this.setPluginContext(exporter, tracer, version);
9595
}
9696

0 commit comments

Comments
 (0)