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

Commit f3246b3

Browse files
committed
Sends Trace Context over http headers
2 parents 3b00c70 + 9bdb21f commit f3246b3

3 files changed

Lines changed: 40 additions & 23 deletions

File tree

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
2727
private _spans: Span[];
2828
private _traceId: string;
2929

30-
constructor(tracer: Tracer, context?: TraceContext ) {
30+
constructor(tracer: Tracer, context?: TraceContext) {
3131
super()
3232
this.tracer = tracer;
33-
this._traceId = context&&context.traceId?context.traceId:(uuid.v4().split('-').join(''));
33+
this._traceId = context && context.traceId ? context.traceId : (uuid.v4().split('-').join(''));
3434
this._spans = [];
3535
}
3636

@@ -58,24 +58,26 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
5858

5959
debug('ending %s %o',
6060
this._className,
61-
{ id: this.id,
61+
{
62+
id: this.id,
6263
traceId: this.traceId,
6364
name: this.name,
6465
startTime: this.startTime,
6566
endTime: this.endTime,
66-
duration: this.duration })
67-
67+
duration: this.duration
68+
})
69+
6870
this.tracer.onEndSpan(this)
6971
}
7072

7173
public onEndSpan(span: Span) {
72-
debug('%s notified ending by %o',{id: span.id, name: span.name})
74+
debug('%s notified ending by %o', { id: span.id, name: span.name })
7375
}
7476

75-
public startSpan(name: string, type: string) {
77+
public startSpan(name?: string, type?: string) {
7678
let newSpan = new Span(this);
77-
newSpan.name = name
78-
newSpan.type = type
79+
if (name) { newSpan.name = name }
80+
if (type) { newSpan.type = type }
7981
newSpan.start();
8082
this._spans.push(newSpan);
8183
return newSpan;

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ 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'
2324
import { TraceContext, OnEndSpanEventListener } from '../types/tracetypes';
2425
import { TracerConfig, defaultConfig } from '../tracing';
26+
import { Buffer } from '../../exporters/buffer'
2527

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

@@ -30,12 +32,13 @@ export class Tracer implements OnEndSpanEventListener {
3032

3133
readonly PLUGINS = ['http', 'https', 'mongodb-core', 'express'];
3234

35+
//public buffer: Buffer;
3336
private _active: boolean;
3437
private contextManager: cls.Namespace;
3538
private config: TracerConfig;
3639

3740
//TODO: simple solution - to be rewied in future
38-
private eventListeners: OnEndSpanEventListener[] = [];
41+
private eventListeners: OnEndSpanEventListener[] = [];
3942
//TODO: temp solution
4043
private endedTraces: RootSpan[] = [];
4144

@@ -58,6 +61,10 @@ export class Tracer implements OnEndSpanEventListener {
5861
return this;
5962
}
6063

64+
public getEventListeners(): OnEndSpanEventListener[] {
65+
return this.eventListeners;
66+
}
67+
6168
public stop() {
6269
this._active = false;
6370
}
@@ -66,53 +73,61 @@ export class Tracer implements OnEndSpanEventListener {
6673
return this._active;
6774
}
6875

69-
public startRootSpan(context?: TraceContext): RootSpan {
76+
public startRootSpan(name?: string, type?: string, context?: TraceContext): RootSpan {
7077
let newRootSpan = new RootSpan(this, context);
78+
if (name) { newRootSpan.name = name }
79+
if (type) { newRootSpan.type = type }
7180
this.setCurrentRootSpan(newRootSpan);
7281
newRootSpan.start();
7382
return newRootSpan;
7483
}
7584

7685

77-
public onEndSpan(root:RootSpan): void {
86+
public onEndSpan(root: RootSpan): void {
7887
if (!this.currentRootSpan) {
7988
return debug('cannot end trace - no active trace found')
8089
}
81-
if(this.currentRootSpan != root) {
82-
return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
90+
if (this.currentRootSpan != root) {
91+
return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
8392
}
8493
this.notifyEndSpan(this.currentRootSpan);
8594
//this.clearCurrentTrace();
8695
}
8796

8897
//TODO: review
8998
public runInContex<T>(fn: Func<T>): T {
90-
return this.contextManager.runAndReturn (fn)
99+
return this.contextManager.runAndReturn(fn)
91100
}
92-
93-
public registerEndSpanListener(listner: OnEndSpanEventListener) {
94101

95-
this.eventListeners.push(listner);
102+
public registerEndSpanListener(listener: OnEndSpanEventListener) {
103+
this.eventListeners.push(listener);
104+
//this.buffer.registerExporter(exporter)
96105
}
97106

107+
/*public registerExporter(exporter: Exporter) {
108+
//this.eventListeners.push(listner);
109+
this.buffer.registerExporter(exporter)
110+
}*/
111+
98112
private notifyEndSpan(root: RootSpan) {
99113
if (this.active) {
100-
if(this.eventListeners&&this.eventListeners.length >0) {
114+
//this.buffer.onEndSpan(root);
115+
if (this.eventListeners && this.eventListeners.length > 0) {
101116
this.eventListeners.forEach((listener) => listener.onEndSpan(root))
102117
}
103118
} else {
104-
debug ('this tracer is inactivate cant notify endspan')
119+
debug('this tracer is inactivate cant notify endspan')
105120
}
106121
}
107122

108123
public clearCurrentTrace() {
109124
this.setCurrentRootSpan(null);
110125
}
111126

112-
public startSpan(name: string, type: string): Span {
127+
public startSpan(name?: string, type?: string): Span {
113128
let newSpan: Span = null;
114129
if (!this.currentRootSpan) {
115-
debug('no current trace found - cannot start a new span');
130+
debug('no current trace found - must start a new root span first');
116131
} else {
117132
newSpan = this.currentRootSpan.startSpan(name, type);
118133
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export abstract class SpanBaseModel {
9595
this._type = type;
9696
}
9797

98-
public set remoteParente(remoteParent: string) {
98+
public set remoteParent(remoteParent: string) {
9999
this._remoteParent = remoteParent;
100100
}
101101

0 commit comments

Comments
 (0)