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

Commit de75000

Browse files
committed
Refactoring: rename class and file Trace to RootSpan, add TraceConfig interface, add TraceContext interface
1 parent 172bf1b commit de75000

8 files changed

Lines changed: 122 additions & 106 deletions

File tree

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616

1717
import * as CLS from 'continuation-local-storage'
1818
import * as semver from 'semver'
19+
import { debug } from './util'
1920

2021
export type Namespace = CLS.Namespace;
2122
export type Func<T> = CLS.Func<T>;
2223

2324
const useAsyncHooks: boolean = semver.satisfies(process.version, '>=8') ;//&&
2425
// !!process.env.GCLOUD_TRACE_NEW_CONTEXT;
2526

27+
debug('useAsyncHooks = %s',useAsyncHooks);
28+
2629
const cls: typeof CLS =
2730
useAsyncHooks ? require('./cls-ah') : require('continuation-local-storage');
2831

32+
2933
const TRACE_NAMESPACE = 'opencensus.io';
3034

3135
/**
@@ -48,19 +52,4 @@ export function getNamespace(): CLS.Namespace {
4852
return cls.getNamespace(TRACE_NAMESPACE);
4953
}
5054

51-
/*
52-
export function set(name:string, trace:any) {
53-
getNamespace().set(name, trace);
54-
}
5555

56-
export function get(name:string): any {
57-
// First getNamespace check is necessary in case any
58-
// patched closures escaped before the agent was stopped and the
59-
// namespace was destroyed.
60-
var result: any = null;
61-
if (getNamespace() && getNamespace().get(name)) {
62-
result = getNamespace().get(name);
63-
}
64-
return result;
65-
}
66-
*/

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ import { Span } from './span'
1818
import { Clock } from '../../internal/clock'
1919
import * as uuid from 'uuid';
2020
import { debug } from '../../internal/util'
21-
import { TraceBaseModel } from '../types/tracetypes'
21+
import { SpanBaseModel, TraceContext } from '../types/tracetypes'
2222

23-
export class Trace extends TraceBaseModel {
23+
export class RootSpan extends SpanBaseModel {
2424

2525
private _spans: Span[];
2626
private _traceId: string;
2727

28-
constructor() {
28+
constructor(context?: TraceContext ) {
2929
super()
30-
this.setId((uuid.v4().split('-').join('')));
30+
this._traceId = context&&context.traceId?context.traceId:(uuid.v4().split('-').join(''));
3131
this._spans = [];
3232
}
3333

@@ -41,7 +41,7 @@ export class Trace extends TraceBaseModel {
4141

4242
public start() {
4343
super.start()
44-
debug('starting trace %o', { traceId: this.traceId })
44+
debug('starting %s %o', this._className, { traceId: this.traceId, id: this.id })
4545
}
4646

4747
public end() {
@@ -53,14 +53,14 @@ export class Trace extends TraceBaseModel {
5353
span.truncate()
5454
})
5555

56-
debug('ending trace %o',
57-
{
58-
id: this.id,
56+
debug('ending %s %o',
57+
this._className,
58+
{ id: this.id,
59+
traceId: this.traceId,
5960
name: this.name,
6061
startTime: this.startTime,
6162
endTime: this.endTime,
62-
duration: this.duration
63-
})
63+
duration: this.duration })
6464
}
6565

6666
public startSpan(name: string, type: string) {

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

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

1717
import { Clock } from '../../internal/clock'
18-
import { Trace } from './trace'
18+
import { RootSpan } from './rootspan'
1919
import { debug, randomSpanId } from '../../internal/util'
20-
import { TraceBaseModel } from '../types/tracetypes'
20+
import { SpanBaseModel, TraceContext } from '../types/tracetypes'
2121

2222

23-
export class Span extends TraceBaseModel {
23+
export class Span extends SpanBaseModel {
2424

25-
private trace: TraceBaseModel;
26-
// private _parentSpanId: string;
25+
private root: SpanBaseModel;
26+
// private _parentSpanId: string;
2727

28-
constructor(trace: TraceBaseModel) {
28+
constructor(root: SpanBaseModel) {
2929
super()
30-
this.trace = trace;
30+
this.root = root;
3131
}
3232

3333
public get traceId(): string {
34-
return this.trace.traceId;
34+
return this.root.traceId;
3535
}
3636

3737
public get parentSpanId(): string {
38-
return this.trace.id;
38+
return this.root.id;
3939
}
4040

41-
public get traceContext() {
41+
public get traceContext(): TraceContext {
4242
return {
4343
traceId: this.traceId.toString(),
4444
spanId: this.id.toString(),
@@ -50,8 +50,8 @@ export class Span extends TraceBaseModel {
5050
super.start();
5151
debug('starting span %o',
5252
{
53-
id: this.id,
5453
traceId: this.traceId,
54+
spanId: this.id,
5555
name: this.name
5656
})
5757
}
@@ -61,7 +61,7 @@ export class Span extends TraceBaseModel {
6161
debug('ending span %o',
6262
{
6363
spanId: this.id,
64-
traceId: this.trace.id,
64+
traceId: this.traceId,
6565
name: this.name,
6666
startTime: this.startTime,
6767
endTime: this.endTime,

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,56 +15,80 @@
1515
*/
1616

1717
import * as cls from '../../internal/cls'
18-
import { Trace } from './trace'
18+
import { RootSpan } from './rootspan'
1919
import { Span } from './span'
2020
import { debug } from '../../internal/util'
2121
import { Stackdriver } from '../../exporters/stackdriver/stackdriver'
2222
import { StackdriverOptions } from '../../exporters/stackdriver/options'
23-
import { Exporter } from '../../exporters/exporter'
23+
import { Exporter, NoopExporter } from '../../exporters/exporter'
24+
import { TraceContext } from '../types/tracetypes';
2425

2526
export type Func<T> = (...args: any[]) => T;
2627

28+
export interface TracerConfig {
29+
exporter?: Exporter,
30+
sampleRate?: number;
31+
ignoreUrls?: Array<string|RegExp>;
32+
}
33+
34+
export const defaultConfig: TracerConfig = {
35+
exporter: new NoopExporter(),
36+
sampleRate: 1.0
37+
}
38+
2739
export class Tracer {
2840

2941
readonly PLUGINS = ['http', 'https', 'mongodb-core', 'express'];
3042

3143
private _active: boolean;
3244
private contextManager: cls.Namespace;
3345
private exporter: Exporter;
46+
private config: TracerConfig;
3447

3548
//TODO: temp solution
36-
private endedTraces: Trace[] = [];
49+
private endedTraces: RootSpan[] = [];
3750

3851
constructor() {
3952
this._active = false;
4053
this.contextManager = cls.createNamespace();
4154
}
4255

43-
public get currentTrace(): Trace {
56+
public get currentTrace(): RootSpan {
4457
return this.contextManager.get('trace');
4558
}
4659

47-
private setCurrentTrace(trace: Trace) {
60+
private setCurrentTrace(trace: RootSpan) {
4861
this.contextManager.set('trace', trace);
4962
}
5063

51-
public start(config?: Object): Tracer {
64+
public start(config?: TracerConfig): Tracer {
5265
this._active = true;
66+
this.config = config || defaultConfig;
67+
this.exporter = this.config.exporter;
5368
return this;
5469
}
5570

71+
public stop() {
72+
this._active = false;
73+
}
74+
5675
public get active(): boolean {
5776
return this._active;
5877
}
5978

60-
public startTrace(): Trace {
61-
let newTrace = new Trace();
79+
public startRootSpan(context?: TraceContext): RootSpan {
80+
let newTrace = new RootSpan(context);
6281
this.setCurrentTrace(newTrace);
6382
newTrace.start();
6483
return newTrace;
6584
}
6685

67-
public endTrace(): void {
86+
//TODO: review
87+
public runInContex<T>(fn: Func<T>): T {
88+
return this.contextManager.runAndReturn (fn)
89+
}
90+
91+
public endRootSpan(): void {
6892
if (!this.currentTrace) {
6993
return debug('cannot end trace - no active trace found')
7094
}
@@ -93,7 +117,7 @@ export class Tracer {
93117
this.exporter.emit(this.currentTrace);
94118
}*/
95119

96-
private addEndedTrace(trace: Trace) {
120+
private addEndedTrace(trace: RootSpan) {
97121
if (this.active) {
98122
//TODO: temp solution
99123
//this.endedTraces.push(trace);

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,28 @@ import { debug, randomSpanId } from '../../internal/util'
2121
export interface MapLabels { [propName: string]: string; }
2222
export interface MapObjects { [propName: string]: any; }
2323

24-
export abstract class TraceBaseModel {
24+
export interface TraceContext {
25+
traceId: string,
26+
spanId: string,
27+
options?: number
28+
}
29+
30+
export abstract class SpanBaseModel {
2531

26-
private _className: string;
32+
protected _className: string;
2733
private _id: string;
2834
private clock: Clock;
2935
//------
3036
private _remoteParent: string;
31-
private attributes: MapLabels = {};
32-
private annotations: MapObjects = {};
33-
//messageEvents
34-
//links
3537
private _name: string;
3638
private _started: boolean;
3739
private _ended: boolean;
3840
private _type: string;
3941
private _status: number;
42+
private attributes: MapLabels = {};
43+
private annotations: MapObjects = {};
44+
//messageEvents
45+
//links
4046
//TODO truncated
4147
private _truncated: boolean;
4248

@@ -113,7 +119,7 @@ export abstract class TraceBaseModel {
113119
return this.clock.duration;
114120
}
115121

116-
public get traceContext() {
122+
public get traceContext(): TraceContext {
117123
return {
118124
traceId: this.traceId.toString(),
119125
spanId: this.id.toString(),

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

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

1717
import { Span } from '../src/trace/model/span';
18-
import { Trace } from '../src/trace/model/trace';
18+
import { RootSpan } from '../src/trace/model/rootspan';
1919

2020
var assert = require('assert');
2121

2222
describe('Span creation', function () {
2323

24-
let trace;
24+
let root;
2525
let span;
2626

2727
before(function () {
28-
trace = new Trace();
29-
span = trace.startSpan('spanName', 'typeSpan');
28+
root = new RootSpan();
29+
span = root.startSpan('spanName', 'typeSpan');
3030
});
3131

3232
it('should create an span on the trace', function () {
33-
assert.ok(trace instanceof Trace);
34-
span = trace.startSpan('spanName', 'typeSpan');
33+
assert.ok(root instanceof RootSpan);
34+
span = root.startSpan('spanName', 'typeSpan');
3535
assert.ok(span instanceof Span);
3636
assert.ok(span.id);
3737
});
@@ -49,12 +49,12 @@ describe('Span creation', function () {
4949

5050
describe('Span checking creation', function () {
5151

52-
let trace;
52+
let root;
5353
let span;
5454

5555
before(function () {
56-
trace = new Trace();
57-
span = trace.startSpan('spanName', 'typeSpan');
56+
root = new RootSpan();
57+
span = root.startSpan('spanName', 'typeSpan');
5858
});
5959

6060
it('should not start span after it ended', function () {
@@ -65,16 +65,16 @@ describe('Span checking creation', function () {
6565

6666
describe('Span data', function () {
6767

68-
let trace;
68+
let root;
6969

7070
before(function () {
71-
trace = new Trace();
71+
root = new RootSpan();
7272
});
7373

7474
it('generates unique numeric span ID strings', function () {
7575
var numberOfSpansToCheck = 5;
7676
for (var i = 0; i < numberOfSpansToCheck; i++) {
77-
var span = trace.startSpan('spanName' + i, 'typeSpan' + i);
77+
var span = root.startSpan('spanName' + i, 'typeSpan' + i);
7878
var spanId = span.id;
7979
assert.ok(typeof spanId === 'string');
8080
assert.ok(spanId.match(/\d+/));

0 commit comments

Comments
 (0)