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

Commit 25fa96f

Browse files
committed
chord: merge commit
2 parents 90445f3 + fe5a6aa commit 25fa96f

4 files changed

Lines changed: 88 additions & 71 deletions

File tree

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

Lines changed: 17 additions & 12 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

@@ -46,7 +49,7 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
4649

4750
public start() {
4851
super.start()
49-
debug('starting %s %o', this._className, { traceId: this.traceId, id: this.id })
52+
debug('starting %s %o', this._className, { traceId: this.traceId, id: this.id, parentSpanId: this.getParentSpanId() })
5053
}
5154

5255
public end() {
@@ -60,21 +63,23 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
6063

6164
debug('ending %s %o',
6265
this._className,
63-
{ id: this.id,
66+
{
67+
id: this.id,
6468
traceId: this.traceId,
6569
name: this.name,
6670
startTime: this.startTime,
6771
endTime: this.endTime,
68-
duration: this.duration })
69-
72+
duration: this.duration
73+
})
74+
7075
this.tracer.onEndSpan(this)
7176
}
7277

7378
public onEndSpan(span: Span) {
74-
debug('ended span notified %o', {id: span.id, name: span.name})
79+
debug('%s notified ending by %o', { id: span.id, name: span.name })
7580
}
7681

77-
public startSpan(name: string, type: string) {
82+
public startSpan(name: string, type: string, parentSpanId?: string) {
7883
if (!this.started) {
7984
debug('calling %s.startSpan() on un-started %s %o',
8085
this._className, this._className,
@@ -88,12 +93,12 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
8893
return
8994
}
9095
let newSpan = new Span(this);
91-
newSpan.name = name
92-
newSpan.type = type
96+
if (name) { newSpan.name = name }
97+
if (type) { newSpan.type = type }
98+
if (type) { newSpan.setParentSpanId(parentSpanId || '') }
9399
newSpan.start();
94100
this._spans.push(newSpan);
95101
return newSpan;
96102
}
97-
98103
}
99104

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ 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';
2523
import { Sampler } from '../config/sampler'
24+
import { TraceContext, TraceOptions, OnEndSpanEventListener, SpanBaseModel } from '../types/tracetypes';
25+
import { TracerConfig, defaultConfig } from '../tracing';
2626

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

2929

3030
export class Tracer implements OnEndSpanEventListener {
3131

32-
readonly PLUGINS = ['http', 'https', 'mongodb-core', 'express'];
32+
readonly PLUGINS = ['http', 'https', 'mongodb-core'];
3333

3434
//public buffer: Buffer;
3535
private _active: boolean;
3636
private contextManager: cls.Namespace;
3737
private config: TracerConfig;
3838

3939
//TODO: simple solution - to be rewied in future
40-
private eventListeners: OnEndSpanEventListener[] = [];
40+
private eventListeners: OnEndSpanEventListener[] = [];
4141
//TODO: temp solution
4242
private endedTraces: RootSpan[] = [];
4343

@@ -76,12 +76,16 @@ export class Tracer implements OnEndSpanEventListener {
7676
return this.contextManager.runAndReturn((root) => {
7777
let newRoot = new RootSpan(this, options);
7878
this.setCurrentRootSpan(newRoot);
79-
if(options.sampler == null){
79+
if (!options) {
80+
options = <TraceOptions>{}
81+
}
82+
if (!options.sampler) {
8083
options.sampler = new Sampler(newRoot.traceId);
84+
//options.sampler.probability(0.5);
8185
options.sampler.always();
8286
}
8387
newRoot.sampler = options.sampler;
84-
if(newRoot.sampler.shouldSample(newRoot.traceId)){
88+
if (newRoot.sampler.shouldSample(newRoot.traceId)) {
8589
newRoot.start();
8690
return fn(newRoot);
8791
}
@@ -90,27 +94,25 @@ export class Tracer implements OnEndSpanEventListener {
9094
}
9195

9296

93-
public onEndSpan(root:RootSpan): void {
97+
public onEndSpan(root: RootSpan): void {
9498
if (!root) {
9599
return debug('cannot end trace - no active trace found')
96100
}
97-
if(this.currentRootSpan != root) {
98-
debug('currentRootSpan != root on notifyEnd. Need more investigation.')
101+
if (this.currentRootSpan != root) {
102+
debug('currentRootSpan != root on notifyEnd. Need more investigation.')
99103
}
100104
this.notifyEndSpan(root);
101105
//this.clearCurrentTrace();
102106
}
103107

104-
105-
public registerEndSpanListener(listener: OnEndSpanEventListener) {
106-
this.eventListeners.push(listener);
107-
//this.buffer.registerExporter(exporter)
108+
public registerEndSpanListener(listner: OnEndSpanEventListener) {
109+
this.eventListeners.push(listner);
108110
}
109111

110112
private notifyEndSpan(root: RootSpan) {
111113
if (this.active) {
112-
debug ('starting to notify listeners the end of rootspans')
113-
if(this.eventListeners&&this.eventListeners.length >0) {
114+
debug('starting to notify listeners the end of rootspans')
115+
if (this.eventListeners && this.eventListeners.length > 0) {
114116
this.eventListeners.forEach((listener) => listener.onEndSpan(root))
115117
}
116118
} else {
@@ -122,12 +124,12 @@ export class Tracer implements OnEndSpanEventListener {
122124
this.setCurrentRootSpan(null);
123125
}
124126

125-
public startSpan(name: string, type: string): Span {
127+
public startSpan(name?: string, type?: string, parentSpanId?: string): Span {
126128
let newSpan: Span = null;
127129
if (!this.currentRootSpan) {
128-
debug('no current trace found - cannot start a new span');
129-
} else{
130-
newSpan = this.currentRootSpan.startSpan(name, type);
130+
debug('no current trace found - must start a new root span first');
131+
} else {
132+
newSpan = this.currentRootSpan.startSpan(name, type, parentSpanId);
131133
}
132134
return newSpan;
133135
}

packages/opencensus-core/src/trace/sampler/sampler.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,38 @@ const maxNumber = 0xffffffffffffffff;
2323
/**
2424
* Class Sampler
2525
*/
26-
export class Sampler{
27-
traceId: string;
28-
private idUpperBound: number;
26+
export class Sampler {
27+
traceId: string;
28+
private idUpperBound: number;
2929

30-
/**
31-
* @param traceId Used for probability calculation
32-
* @param spanId todo: integration with propagation class
33-
* @param isRemote todo: integration with propagation class
34-
*/
35-
constructor(traceId?:string, spanId?:string, isRemote?:boolean){
36-
if(traceId){
37-
this.traceId = traceId;
38-
}
39-
}
30+
/**
31+
* @param traceId Used for probability calculation
32+
* @param spanId todo: integration with propagation class
33+
* @param isRemote todo: integration with propagation class
34+
*/
35+
constructor(traceId?: string, spanId?: string, isRemote?: boolean) {
36+
if (traceId) {
37+
this.traceId = traceId;
38+
}
39+
}
4040

4141
/**
4242
* @description Set idUpperBound with maxNumber
4343
* @returns a Sampler object
4444
*/
45-
public always(): Sampler{
46-
this.idUpperBound = maxNumber;
47-
return this;
48-
}
45+
public always(): Sampler {
46+
this.idUpperBound = maxNumber;
47+
return this;
48+
}
4949

5050
/**
5151
* @description Set idUpperBound with minNumber
5252
* @returns a Sampler object
5353
*/
54-
public never(): Sampler{
55-
this.idUpperBound = minNumber;
56-
return this;
57-
}
54+
public never(): Sampler {
55+
this.idUpperBound = minNumber;
56+
return this;
57+
}
5858

5959
/**
6060
* @description Set idUpperBound with the probability. If probability
@@ -63,8 +63,8 @@ export class Sampler{
6363
* @param probability probability between 0 and 1
6464
* @returns a Sampler object
6565
*/
66-
public probability(probability:number): Sampler{
67-
if(probability < minNumber){
66+
public probability(probability: number): Sampler {
67+
if (probability < minNumber) {
6868
return this.never();
6969

7070
} else if (probability > maxNumber) {
@@ -74,20 +74,20 @@ export class Sampler{
7474

7575
this.idUpperBound = probability * maxNumber;
7676
return this;
77-
}
78-
77+
}
78+
7979
/**
8080
* @description
8181
* @param traceId
8282
* @returns a boolean
8383
*/
84-
public shouldSample (traceId:string):boolean{
84+
public shouldSample(traceId: string): boolean {
8585
const lower_bytes = traceId.substring(16)
8686
const lower_long = parseInt(lower_bytes, 16);
8787

88-
if(lower_long <= this.idUpperBound){
88+
if (lower_long <= this.idUpperBound) {
8989
return true
90-
}else{
90+
} else {
9191
return false;
9292
}
9393
}

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ export interface MapLabels { [propName: string]: string; }
2323
export interface MapObjects { [propName: string]: any; }
2424

2525
export interface TraceContext {
26-
traceId: string,
27-
spanId: string,
28-
options?: number
26+
traceId: string,
27+
spanId: string,
28+
options?: number,
29+
sampleDecision?: boolean
2930
}
3031

3132
export interface TraceOptions {
32-
name: string;
33-
traceContext?: TraceContext;
34-
sampler?: Sampler;
33+
name:string;
34+
traceContext?:TraceContext;
35+
sampler?:Sampler;
36+
type?:string;
3537
}
3638

3739
export interface OnEndSpanEventListener {
@@ -53,6 +55,7 @@ export abstract class SpanBaseModel {
5355
private clock: Clock;
5456
//--Tra----
5557
private _remoteParent: string;
58+
private _parentSpanId: string;
5659
private _name: string;
5760
private _started: boolean;
5861
private _ended: boolean;
@@ -74,6 +77,7 @@ export abstract class SpanBaseModel {
7477
this.clock = null;
7578
this._truncated = false;
7679
this._ended = false;
80+
this._parentSpanId = ''
7781
this.setId(randomSpanId());
7882
}
7983

@@ -104,6 +108,14 @@ export abstract class SpanBaseModel {
104108
this._name = name;
105109
}
106110

111+
public setParentSpanId(parentSpanId: string) {
112+
this._parentSpanId = parentSpanId;
113+
}
114+
115+
public getParentSpanId() {
116+
return this._parentSpanId
117+
}
118+
107119
public get type(): string {
108120
return this._type;
109121
}
@@ -112,7 +124,7 @@ export abstract class SpanBaseModel {
112124
this._type = type;
113125
}
114126

115-
public set remoteParente(remoteParent: string) {
127+
public set remoteParent(remoteParent: string) {
116128
this._remoteParent = remoteParent;
117129
}
118130

@@ -148,11 +160,11 @@ export abstract class SpanBaseModel {
148160
}
149161

150162
public get traceContext(): TraceContext {
151-
return {
163+
return <TraceContext>{
152164
traceId: this.traceId.toString(),
153165
spanId: this.id.toString(),
154-
options: 1 // always traced
155-
};
166+
parentSpanId: this.getParentSpanId
167+
}
156168
}
157169

158170
//TODO: maybe key and values must be truncate
@@ -166,12 +178,10 @@ export abstract class SpanBaseModel {
166178
}
167179

168180
public get sampler() {
169-
debug('tracetypes get sampler()')
170181
return this._sampler;
171182
}
172183

173184
public set sampler(sampler: Sampler) {
174-
debug('tracetypes set sempler(sampler)')
175185
this._sampler = sampler;
176186
}
177187

0 commit comments

Comments
 (0)