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

Commit c224918

Browse files
committed
Update code after refactoring
2 parents 1cff931 + fe5ece9 commit c224918

8 files changed

Lines changed: 171 additions & 141 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: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,37 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as uuid from 'uuid'
18-
1917
import { Span } from './span'
2018
import { Clock } from '../../internal/clock'
19+
import * as uuid from 'uuid';
2120
import { debug } from '../../internal/util'
22-
import { TraceBaseModel } from '../types/tracetypes'
21+
import { SpanBaseModel, TraceContext, OnEndSpanEventListener } from '../types/tracetypes'
22+
import { Tracer } from './tracer';
2323

24-
export class Trace extends TraceBaseModel {
24+
export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
2525

26-
private _spans: Span[] = [];
26+
private tracer: Tracer;
27+
private _spans: Span[];
2728
private _traceId: string;
2829

29-
constructor() {
30+
constructor(tracer: Tracer, context?: TraceContext ) {
3031
super()
31-
this._traceId = (uuid.v4().split('-').join(''));
32+
this.tracer = tracer;
33+
this._traceId = context&&context.traceId?context.traceId:(uuid.v4().split('-').join(''));
34+
this._spans = [];
3235
}
3336

34-
public get spans(): Span[] {
37+
public get spans() {
3538
return this._spans;
3639
}
3740

38-
public get traceId(): string {
41+
public get traceId() {
3942
return this._traceId;
4043
}
4144

4245
public start() {
4346
super.start()
44-
debug('starting trace %o', { traceId: this.traceId })
47+
debug('starting %s %o', this._className, { traceId: this.traceId, id: this.id })
4548
}
4649

4750
public end() {
@@ -53,14 +56,20 @@ export class Trace extends TraceBaseModel {
5356
span.truncate()
5457
})
5558

56-
debug('ending trace %o',
57-
{
58-
id: this.id,
59+
debug('ending %s %o',
60+
this._className,
61+
{ id: this.id,
62+
traceId: this.traceId,
5963
name: this.name,
6064
startTime: this.startTime,
6165
endTime: this.endTime,
62-
duration: this.duration
63-
})
66+
duration: this.duration })
67+
68+
this.tracer.onEndSpan(this)
69+
}
70+
71+
public onEndSpan(span: Span) {
72+
debug('%s notified ending by %o',{id: span.id, name: span.name})
6473
}
6574

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

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

Lines changed: 17 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: RootSpan;
26+
// private _parentSpanId: string;
2727

28-
constructor(trace: TraceBaseModel) {
28+
constructor(root: RootSpan) {
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,18 +50,23 @@ 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
}
5858

59+
private notifyEnd () {
60+
this.root.onEndSpan(this);
61+
}
62+
5963
public end(): void {
6064
super.end();
65+
this.notifyEnd();
6166
debug('ending span %o',
6267
{
6368
spanId: this.id,
64-
traceId: this.trace.id,
69+
traceId: this.traceId,
6570
name: this.name,
6671
startTime: this.startTime,
6772
endTime: this.endTime,

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

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,92 +15,109 @@
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 { TraceContext, OnEndSpanEventListener } from '../types/tracetypes';
24+
import { TracerConfig, defaultConfig } from '../tracing';
2425

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

27-
export class Tracer {
28+
29+
export class Tracer implements OnEndSpanEventListener {
2830

2931
readonly PLUGINS = ['http', 'https', 'mongodb-core', 'express'];
3032

3133
private _active: boolean;
3234
private contextManager: cls.Namespace;
33-
private exporter: Exporter;
35+
private config: TracerConfig;
3436

37+
//TODO: simple solution - to be rewied in future
38+
private eventListeners: OnEndSpanEventListener[] = [];
3539
//TODO: temp solution
36-
private endedTraces: Trace[] = [];
40+
private endedTraces: RootSpan[] = [];
3741

3842
constructor() {
3943
this._active = false;
4044
this.contextManager = cls.createNamespace();
4145
}
4246

43-
public get currentTrace(): Trace {
44-
return this.contextManager.get('trace');
47+
public get currentRootSpan(): RootSpan {
48+
return this.contextManager.get('rootspan');
4549
}
4650

47-
private setCurrentTrace(trace: Trace) {
48-
this.contextManager.set('trace', trace);
51+
private setCurrentRootSpan(root: RootSpan) {
52+
this.contextManager.set('rootspan', root);
4953
}
5054

51-
public start(config?: Object): Tracer {
55+
public start(config?: TracerConfig): Tracer {
5256
this._active = true;
57+
this.config = config || defaultConfig;
5358
return this;
5459
}
5560

61+
public stop() {
62+
this._active = false;
63+
}
64+
5665
public get active(): boolean {
5766
return this._active;
5867
}
5968

60-
public startTrace(): Trace {
61-
let newTrace = new Trace();
62-
this.setCurrentTrace(newTrace);
69+
public startRootSpan(context?: TraceContext): RootSpan {
70+
let newTrace = new RootSpan(this, context);
71+
this.setCurrentRootSpan(newTrace);
6372
newTrace.start();
6473
return newTrace;
6574
}
6675

67-
public endTrace(): void {
68-
if (!this.currentTrace) {
76+
77+
public onEndSpan(root:RootSpan): void {
78+
if (!this.currentRootSpan) {
6979
return debug('cannot end trace - no active trace found')
7080
}
71-
this.currentTrace.end();
72-
this.addEndedTrace(this.currentTrace);
81+
if(this.currentRootSpan != root) {
82+
return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
83+
}
84+
this.notifyEndSpan(this.currentRootSpan);
7385
//this.clearCurrentTrace();
7486
}
7587

88+
//TODO: review
89+
public runInContex<T>(fn: Func<T>): T {
90+
return this.contextManager.runAndReturn (fn)
91+
}
92+
93+
public registerEndSpanListener(listner: OnEndSpanEventListener) {
94+
this.eventListeners.push(listner);
95+
}
96+
97+
private notifyEndSpan(root: RootSpan) {
98+
if (this.active) {
99+
if(this.eventListeners&&this.eventListeners.length >0) {
100+
this.eventListeners.forEach((listener) => listener.onEndSpan(root))
101+
}
102+
} else {
103+
debug ('this tracer is inactivate cant notify endspan')
104+
}
105+
}
106+
76107
public clearCurrentTrace() {
77-
this.setCurrentTrace(null);
108+
this.setCurrentRootSpan(null);
78109
}
79110

80111
public startSpan(name: string, type: string): Span {
81112
let newSpan: Span = null;
82-
if (!this.currentTrace) {
113+
if (!this.currentRootSpan) {
83114
debug('no current trace found - cannot start a new span');
84115
} else {
85-
newSpan = this.currentTrace.startSpan(name, type);
116+
newSpan = this.currentRootSpan.startSpan(name, type);
86117
}
87118
return newSpan;
88119
}
89120

90-
/*public endSpan(span: Span) {
91-
debug('END SPAN', span);
92-
span.end();
93-
this.exporter.emit(this.currentTrace);
94-
}*/
95-
96-
private addEndedTrace(trace: Trace) {
97-
if (this.active) {
98-
//TODO: temp solution
99-
//this.endedTraces.push(trace);
100-
this.exporter.writeTrace(this.currentTrace);
101-
}
102-
}
103-
104121
public wrap<T>(fn: Func<T>): Func<T> {
105122
if (!this.active) {
106123
return fn;
@@ -121,10 +138,6 @@ export class Tracer {
121138
namespace.bindEmitter(emitter);
122139
}
123140

124-
public registerExporter(exporter: Exporter) {
125-
this.exporter = exporter;
126-
}
127-
128141
}
129142

130143

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,32 @@ 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 interface OnEndSpanEventListener {
31+
onEndSpan(span: SpanBaseModel): void;
32+
}
33+
34+
export abstract class SpanBaseModel {
2535

26-
private _className: string;
36+
protected _className: string;
2737
private _id: string;
2838
private clock: Clock;
29-
//------
39+
//--Tra----
3040
private _remoteParent: string;
31-
private attributes: MapLabels = {};
32-
private annotations: MapObjects = {};
33-
//messageEvents
34-
//links
3541
private _name: string;
3642
private _started: boolean;
3743
private _ended: boolean;
3844
private _type: string;
3945
private _status: number;
46+
private attributes: MapLabels = {};
47+
private annotations: MapObjects = {};
48+
//messageEvents
49+
//links
4050
//TODO truncated
4151
private _truncated: boolean;
4252

@@ -113,7 +123,7 @@ export abstract class TraceBaseModel {
113123
return this.clock.duration;
114124
}
115125

116-
public get traceContext() {
126+
public get traceContext(): TraceContext {
117127
return {
118128
traceId: this.traceId.toString(),
119129
spanId: this.id.toString(),
@@ -157,7 +167,6 @@ export abstract class TraceBaseModel {
157167
this._started = false;
158168
this._ended = true;
159169
this.clock.end();
160-
161170
}
162171

163172

0 commit comments

Comments
 (0)