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

Commit c1482fe

Browse files
committed
chord: merge commit
2 parents a740a0e + 088abb0 commit c1482fe

5 files changed

Lines changed: 160 additions & 29 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import { Span } from './span'
1818
import { Clock } from '../../internal/clock'
1919
import * as uuid from 'uuid';
2020
import { debug } from '../../internal/util'
21-
import { SpanBaseModel, TraceContext, OnEndSpanEventListener, TraceOptions } from '../types/tracetypes'
22-
import { Tracer } from './tracer';
21+
import { SpanBaseModel, TraceOptions, TraceContext, OnEndSpanEventListener } from '../types/tracetypes'
22+
import { Tracer } from './tracer'
23+
import { Sampler } from './sampler'
2324

2425
export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
2526

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,20 @@ export class Span extends SpanBaseModel {
6161
}
6262

6363
public end(): void {
64-
super.end();
65-
this.notifyEnd();
66-
debug('ending span %o',
67-
{
68-
spanId: this.id,
69-
traceId: this.traceId,
70-
name: this.name,
71-
startTime: this.startTime,
72-
endTime: this.endTime,
73-
duration: this.duration
74-
}
75-
)
76-
64+
// if(this.sampler.continue(this.traceId)) {
65+
66+
super.end();
67+
this.notifyEnd();
68+
debug('ending span %o',
69+
{
70+
spanId: this.id,
71+
traceId: this.traceId,
72+
name: this.name,
73+
startTime: this.startTime,
74+
endTime: this.endTime,
75+
duration: this.duration
76+
});
77+
// }
7778
}
7879

7980

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Stackdriver } from '../../exporters/stackdriver/stackdriver'
2222
import { StackdriverOptions } from '../../exporters/stackdriver/options'
2323
import { TraceContext, TraceOptions, OnEndSpanEventListener } from '../types/tracetypes';
2424
import { TracerConfig, defaultConfig } from '../tracing';
25+
import { Sampler } from './sampler'
2526

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

@@ -30,6 +31,7 @@ export class Tracer implements OnEndSpanEventListener {
3031

3132
readonly PLUGINS = ['http', 'https', 'mongodb-core', 'express'];
3233

34+
//public buffer: Buffer;
3335
private _active: boolean;
3436
private contextManager: cls.Namespace;
3537
private config: TracerConfig;
@@ -58,6 +60,10 @@ export class Tracer implements OnEndSpanEventListener {
5860
return this;
5961
}
6062

63+
public getEventListeners(): OnEndSpanEventListener[] {
64+
return this.eventListeners;
65+
}
66+
6167
public stop() {
6268
this._active = false;
6369
}
@@ -67,12 +73,19 @@ export class Tracer implements OnEndSpanEventListener {
6773
}
6874

6975
public startRootSpan<T>(options: TraceOptions, fn: (root: RootSpan) => T): T {
70-
debug('starting root span: %o', options)
7176
return this.contextManager.runAndReturn((root) => {
7277
let newRoot = new RootSpan(this, options);
7378
this.setCurrentRootSpan(newRoot);
74-
newRoot.start();
75-
return fn(newRoot);
79+
if(options.sampler == null){
80+
options.sampler = new Sampler(newRoot.traceId);
81+
options.sampler.probability(0.6);
82+
}
83+
newRoot.sampler = options.sampler;
84+
if(newRoot.sampler.continue(newRoot.traceId)){
85+
newRoot.start();
86+
return fn(newRoot);
87+
}
88+
return fn(null);
7689
});
7790
}
7891

@@ -89,8 +102,9 @@ export class Tracer implements OnEndSpanEventListener {
89102
}
90103

91104

92-
public registerEndSpanListener(listner: OnEndSpanEventListener) {
93-
this.eventListeners.push(listner);
105+
public registerEndSpanListener(listener: OnEndSpanEventListener) {
106+
this.eventListeners.push(listener);
107+
//this.buffer.registerExporter(exporter)
94108
}
95109

96110
private notifyEndSpan(root: RootSpan) {
@@ -100,7 +114,7 @@ export class Tracer implements OnEndSpanEventListener {
100114
this.eventListeners.forEach((listener) => listener.onEndSpan(root))
101115
}
102116
} else {
103-
debug ('this tracer is inactivate cant notify endspan')
117+
debug('this tracer is inactivate cant notify endspan')
104118
}
105119
}
106120

@@ -112,7 +126,7 @@ export class Tracer implements OnEndSpanEventListener {
112126
let newSpan: Span = null;
113127
if (!this.currentRootSpan) {
114128
debug('no current trace found - cannot start a new span');
115-
} else {
129+
} else{
116130
newSpan = this.currentRootSpan.startSpan(name, type);
117131
}
118132
return newSpan;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright 2018 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { debug, randomSpanId } from '../../internal/util'
18+
19+
20+
const minNumber = 1e-4;
21+
const maxNumber = 0xffffffffffffffff;
22+
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){
59+
return this.never();
60+
61+
} else if (probability > maxNumber){
62+
return this.always();
63+
64+
}
65+
66+
this.idUpperBound = probability * maxNumber;
67+
return this;
68+
}
69+
70+
public continue (traceId:string):boolean{
71+
debug('Samplre continue')
72+
let lower_bytes = traceId.substring(16)
73+
let lower_long: number
74+
debug('SAMPLER CONTINUE lower_bytes :',lower_bytes)
75+
76+
lower_long = parseInt(lower_bytes, 16);
77+
78+
debug('SAMPLER CONTINUE lower_long :',lower_long)
79+
debug('SAMPLER CONTINUE this.idUpperBound :',this.idUpperBound)
80+
debug('SAMPLER CONTINUE diff :',lower_long - this.idUpperBound)
81+
82+
if(lower_long <= this.idUpperBound){
83+
debug('trace sampler TRUE')
84+
return true
85+
}else{
86+
debug('trace sampler FALSE')
87+
return false;
88+
}
89+
}
90+
91+
}

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { Clock } from '../../internal/clock'
1818
import { debug, randomSpanId } from '../../internal/util'
19+
import { Sampler } from '../model/sampler'
1920

2021

2122
export interface MapLabels { [propName: string]: string; }
@@ -30,6 +31,7 @@ export interface TraceContext {
3031
export interface TraceOptions {
3132
name:string;
3233
traceContext?:TraceContext;
34+
sampler?:Sampler;
3335
}
3436

3537
export interface OnEndSpanEventListener {
@@ -54,6 +56,7 @@ export abstract class SpanBaseModel {
5456
//links
5557
//TODO truncated
5658
private _truncated: boolean;
59+
private _sampler: Sampler;
5760

5861
constructor() {
5962
this._className = this.constructor.name;
@@ -66,6 +69,7 @@ export abstract class SpanBaseModel {
6669
this.setId(randomSpanId());
6770
}
6871

72+
6973
public get id(): string {
7074
return this._id;
7175
}
@@ -117,15 +121,22 @@ export abstract class SpanBaseModel {
117121
}
118122

119123
public get startTime(): Date {
120-
return this.clock.startTime;
124+
if(this.clock){
125+
return this.clock.startTime;
126+
}
127+
121128
}
122129

123130
public get endTime(): Date {
124-
return this.clock.endTime;
131+
if(this.clock){
132+
return this.clock.endTime;
133+
}
125134
}
126135

127136
public get duration(): number {
128-
return this.clock.duration;
137+
if(this.clock){
138+
return this.clock.duration;
139+
}
129140
}
130141

131142
public get traceContext(): TraceContext {
@@ -146,6 +157,16 @@ export abstract class SpanBaseModel {
146157
this.annotations[key] = value;
147158
}
148159

160+
public get sampler(){
161+
debug('tracetypes get sampler()')
162+
return this._sampler;
163+
}
164+
165+
public set sampler(sampler:Sampler){
166+
debug('tracetypes set sempler(sampler)')
167+
this._sampler = sampler;
168+
}
169+
149170
public start() {
150171
if (this.started) {
151172
debug('calling %s.start() on already started %s %o',
@@ -160,13 +181,16 @@ export abstract class SpanBaseModel {
160181
public end(): void {
161182
if (!this.started) {
162183
debug('calling %s.end() on un-started %s %o',
163-
this._className, this._className,
164-
{ id: this.id, name: this.name, type: this.type })
184+
this._className, this._className,
185+
{ id: this.id, name: this.name, type: this.type })
186+
this._started = false;
187+
this._ended = true;
188+
// this.clock.end();
165189
return
166190
} else if (this.ended) {
167191
debug('calling %s.end() on already ended %s %o',
168-
this._className, this._className,
169-
{ id: this.id, name: this.name, type: this.type })
192+
this._className, this._className,
193+
{ id: this.id, name: this.name, type: this.type })
170194
return
171195
}
172196
this._started = false;

0 commit comments

Comments
 (0)