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

Commit 12e0b50

Browse files
committed
Propagate trace context over B3 format
1 parent 8cd96cc commit 12e0b50

4 files changed

Lines changed: 50 additions & 88 deletions

File tree

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
4747
return this._traceId;
4848
}
4949

50-
public getOptions() {
51-
return {
52-
name: this.name,
53-
traceContext: {
54-
traceId: this.traceId,
55-
spanId: this.id,
56-
parentSpanId: this.getParentSpanId
57-
}
50+
public getContext(): TraceContext {
51+
return <TraceContext>{
52+
traceId: this.traceId,
53+
spanId: this.id,
54+
parentSpanId: this.getParentSpanId
5855
}
5956
}
6057

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

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ export class Tracer implements OnEndSpanEventListener {
8080
options = <TraceOptions>{}
8181
}
8282
if (!options.sampler) {
83-
// options.sampler = new Sampler(newRoot.traceId);
84-
// options.sampler.probability(0.6);
8583
options.sampler = new Sampler(newRoot.traceId);
84+
// options.sampler.probability(0.6);
8685
options.sampler.always();
8786
}
8887
newRoot.sampler = options.sampler;
@@ -135,40 +134,6 @@ export class Tracer implements OnEndSpanEventListener {
135134
return newSpan;
136135
}
137136

138-
/*public startSpan<T>(fn: (span: Span) => T): T {
139-
//debug('starting span: %o', options)
140-
if (!this.currentRootSpan) {
141-
debug('no current trace found - must start a new root span first');
142-
return null;
143-
} else {
144-
return this.contextManager.runAndReturn((span) => {
145-
let newSpan = this.currentRootSpan.startSpan();
146-
return fn(newSpan);
147-
});
148-
}
149-
}*/
150-
151-
/*public startSpan<T>(options: TraceOptions, fn: (span: SpanBaseModel) => T): T {
152-
//debug('starting span: %o', options)
153-
if (this.currentRootSpan) {
154-
// Has an active root span
155-
debug('starting span')
156-
return this.contextManager.runAndReturn((span) => {
157-
let newSpan = this.currentRootSpan.startSpan();
158-
return fn(newSpan);
159-
});
160-
} else {
161-
// Has no active root span
162-
debug('starting root span: %o', options)
163-
return this.contextManager.runAndReturn((root) => {
164-
let newRoot = new RootSpan(this, options);
165-
this.setCurrentRootSpan(newRoot);
166-
newRoot.start();
167-
return fn(newRoot);
168-
});
169-
}
170-
}*/
171-
172137
public wrap<T>(fn: Func<T>): Func<T> {
173138
if (!this.active) {
174139
return fn;

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

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,54 +20,54 @@ import { debug, randomSpanId } from '../../internal/util'
2020
const minNumber = 1e-4;
2121
const maxNumber = 0xffffffffffffffff;
2222

23-
export class Sampler{
24-
traceId: string;
25-
spanId: string;
26-
isRemote: boolean;
27-
idUpperBound: number;
28-
29-
/**
30-
*
31-
* @param traceId
32-
* @param spanId
33-
* @param isRemote
34-
*/
35-
constructor(traceId?:string, spanId?:string, isRemote?:boolean){
36-
debug('Samplre constructor')
37-
if(traceId){
38-
this.traceId = traceId;
39-
}
40-
if(spanId){
41-
this.spanId = spanId;
42-
}
43-
this.isRemote = isRemote || false;
44-
45-
}
46-
47-
public always(): Sampler{
48-
this.idUpperBound = maxNumber;
49-
return this;
50-
}
51-
52-
public never(): Sampler{
53-
this.idUpperBound = minNumber;
54-
return this;
55-
}
56-
57-
public probability(probability:number): Sampler{
58-
if(probability < minNumber){
23+
export class Sampler {
24+
traceId: string;
25+
spanId: string;
26+
isRemote: boolean;
27+
idUpperBound: number;
28+
29+
/**
30+
*
31+
* @param traceId
32+
* @param spanId
33+
* @param isRemote
34+
*/
35+
constructor(traceId?: string, spanId?: string, isRemote?: boolean) {
36+
debug('Samplre constructor')
37+
if (traceId) {
38+
this.traceId = traceId;
39+
}
40+
if (spanId) {
41+
this.spanId = spanId;
42+
}
43+
this.isRemote = isRemote || false;
44+
45+
}
46+
47+
public always(): Sampler {
48+
this.idUpperBound = maxNumber;
49+
return this;
50+
}
51+
52+
public never(): Sampler {
53+
this.idUpperBound = minNumber;
54+
return this;
55+
}
56+
57+
public probability(probability: number): Sampler {
58+
if (probability < minNumber) {
5959
return this.never();
6060

61-
} else if (probability > maxNumber){
61+
} else if (probability > maxNumber) {
6262
return this.always();
6363

6464
}
6565

6666
this.idUpperBound = probability * maxNumber;
6767
return this;
68-
}
68+
}
6969

70-
public continue (traceId:string):boolean{
70+
public continue(traceId: string): boolean {
7171
//debug('Samplre continue')
7272
let lower_bytes = traceId.substring(16)
7373
let lower_long: number
@@ -79,13 +79,12 @@ export class Sampler{
7979
//debug('SAMPLER CONTINUE this.idUpperBound :',this.idUpperBound)
8080
//debug('SAMPLER CONTINUE diff :',lower_long - this.idUpperBound)
8181

82-
if(lower_long <= this.idUpperBound){
82+
if (lower_long <= this.idUpperBound) {
8383
debug('trace sampler TRUE')
8484
return true
85-
}else{
85+
} else {
8686
debug('trace sampler FALSE')
8787
return false;
8888
}
89-
}
90-
91-
}
89+
}
90+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface TraceOptions {
3232
name:string;
3333
traceContext?:TraceContext;
3434
sampler?:Sampler;
35+
type?:string;
3536
}
3637

3738
export interface OnEndSpanEventListener {

0 commit comments

Comments
 (0)