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

Commit 1e2f049

Browse files
committed
Creates Traces with sent context
1 parent 6cc8995 commit 1e2f049

3 files changed

Lines changed: 75 additions & 21 deletions

File tree

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
2828
private _traceId: string;
2929

3030
//TODO - improve root name setup
31-
constructor(tracer: Tracer, context?: TraceOptions ) {
31+
constructor(tracer: Tracer, context?: TraceOptions) {
3232
super()
3333
this.tracer = tracer;
34-
this._traceId = context&&context.traceContext&&context.traceContext.traceId?context.traceContext.traceId:(uuid.v4().split('-').join(''));
35-
this.name = context&&context.name?context.name:'undefined';
34+
this._traceId = context && context.traceContext && context.traceContext.traceId ? context.traceContext.traceId : (uuid.v4().split('-').join(''));
35+
this.name = context && context.name ? context.name : 'undefined';
36+
if (context && context.traceContext) {
37+
this.setParentSpanId(context.traceContext.spanId || '')
38+
}
3639
this._spans = [];
3740
}
3841

@@ -44,9 +47,20 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
4447
return this._traceId;
4548
}
4649

50+
public getOptions() {
51+
return {
52+
name: this.name,
53+
traceContext: {
54+
traceId: this.traceId,
55+
spanId: this.id,
56+
parentSpanId: this.getParentSpanId
57+
}
58+
}
59+
}
60+
4761
public start() {
4862
super.start()
49-
debug('starting %s %o', this._className, { traceId: this.traceId, id: this.id })
63+
debug('starting %s %o', this._className, { traceId: this.traceId, id: this.id, parentSpanId: this.getParentSpanId() })
5064
}
5165

5266
public end() {
@@ -76,14 +90,14 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
7690
debug('%s notified ending by %o', { id: span.id, name: span.name })
7791
}
7892

79-
public startSpan(name?: string, type?: string) {
93+
public startSpan(name?: string, type?: string, parentSpanId?: string) {
8094
let newSpan = new Span(this);
8195
if (name) { newSpan.name = name }
8296
if (type) { newSpan.type = type }
97+
if (type) { newSpan.setParentSpanId(parentSpanId || '') }
8398
newSpan.start();
8499
this._spans.push(newSpan);
85100
return newSpan;
86101
}
87-
88102
}
89103

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

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ 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 { TraceContext, TraceOptions, OnEndSpanEventListener } from '../types/tracetypes';
24-
import { TracerConfig, defaultConfig } from '../tracing';
23+
import { TraceContext, TraceOptions, OnEndSpanEventListener, SpanBaseModel } from '../types/tracetypes';
24+
import { TracerConfig, defaultConfig } from '../tracing';
2525

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

@@ -81,28 +81,24 @@ export class Tracer implements OnEndSpanEventListener {
8181
});
8282
}
8383

84-
8584
public onEndSpan(root: RootSpan): void {
8685
if (!this.currentRootSpan) {
87-
return debug('cannot end trace - no active trace found')
86+
return debug('cannot end trace - no active trace found', this.currentRootSpan)
8887
}
8988
if (this.currentRootSpan != root) {
90-
return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
89+
debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
90+
debug('ROOT SPAN ', root)
91+
return debug('CURRENT ROOT SPAN ', this.currentRootSpan)
92+
//return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
9193
}
9294
this.notifyEndSpan(this.currentRootSpan);
9395
//this.clearCurrentTrace();
9496
}
9597

96-
9798
public registerEndSpanListener(listner: OnEndSpanEventListener) {
98-
this.eventListeners.push(listner);
99+
this.eventListeners.push(listner);
99100
}
100101

101-
/*public registerExporter(exporter: Exporter) {
102-
//this.eventListeners.push(listner);
103-
this.buffer.registerExporter(exporter)
104-
}*/
105-
106102
private notifyEndSpan(root: RootSpan) {
107103
if (this.active) {
108104
//this.buffer.onEndSpan(root);
@@ -118,16 +114,50 @@ export class Tracer implements OnEndSpanEventListener {
118114
this.setCurrentRootSpan(null);
119115
}
120116

121-
public startSpan(name?: string, type?: string): Span {
117+
public startSpan(name?: string, type?: string, parentSpanId?: string): Span {
122118
let newSpan: Span = null;
123119
if (!this.currentRootSpan) {
124120
debug('no current trace found - must start a new root span first');
125121
} else {
126-
newSpan = this.currentRootSpan.startSpan(name, type);
122+
newSpan = this.currentRootSpan.startSpan(name, type, parentSpanId);
127123
}
128124
return newSpan;
129125
}
130126

127+
/*public startSpan<T>(fn: (span: Span) => T): T {
128+
//debug('starting span: %o', options)
129+
if (!this.currentRootSpan) {
130+
debug('no current trace found - must start a new root span first');
131+
return null;
132+
} else {
133+
return this.contextManager.runAndReturn((span) => {
134+
let newSpan = this.currentRootSpan.startSpan();
135+
return fn(newSpan);
136+
});
137+
}
138+
}*/
139+
140+
/*public startSpan<T>(options: TraceOptions, fn: (span: SpanBaseModel) => T): T {
141+
//debug('starting span: %o', options)
142+
if (this.currentRootSpan) {
143+
// Has an active root span
144+
debug('starting span')
145+
return this.contextManager.runAndReturn((span) => {
146+
let newSpan = this.currentRootSpan.startSpan();
147+
return fn(newSpan);
148+
});
149+
} else {
150+
// Has no active root span
151+
debug('starting root span: %o', options)
152+
return this.contextManager.runAndReturn((root) => {
153+
let newRoot = new RootSpan(this, options);
154+
this.setCurrentRootSpan(newRoot);
155+
newRoot.start();
156+
return fn(newRoot);
157+
});
158+
}
159+
}*/
160+
131161
public wrap<T>(fn: Func<T>): Func<T> {
132162
if (!this.active) {
133163
return fn;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface MapObjects { [propName: string]: any; }
2424
export interface TraceContext {
2525
traceId: string,
2626
spanId: string,
27-
options?: number
27+
options?: number
2828
}
2929

3030
export interface TraceOptions {
@@ -43,6 +43,7 @@ export abstract class SpanBaseModel {
4343
private clock: Clock;
4444
//--Tra----
4545
private _remoteParent: string;
46+
private _parentSpanId: string;
4647
private _name: string;
4748
private _started: boolean;
4849
private _ended: boolean;
@@ -63,6 +64,7 @@ export abstract class SpanBaseModel {
6364
this.clock = null;
6465
this._truncated = false;
6566
this._ended = false;
67+
this._parentSpanId = ''
6668
this.setId(randomSpanId());
6769
}
6870

@@ -92,6 +94,14 @@ export abstract class SpanBaseModel {
9294
this._name = name;
9395
}
9496

97+
public setParentSpanId(parentSpanId: string) {
98+
this._parentSpanId = parentSpanId;
99+
}
100+
101+
public getParentSpanId() {
102+
return this._parentSpanId
103+
}
104+
95105
public get type(): string {
96106
return this._type;
97107
}

0 commit comments

Comments
 (0)