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

Commit 8cddf95

Browse files
committed
Add sampler
1 parent 9bdb21f commit 8cddf95

4 files changed

Lines changed: 125 additions & 17 deletions

File tree

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { Clock } from '../../internal/clock'
1919
import * as uuid from 'uuid';
2020
import { debug } from '../../internal/util'
2121
import { SpanBaseModel, TraceContext, OnEndSpanEventListener } from '../types/tracetypes'
22-
import { Tracer } from './tracer';
22+
import { Tracer } from './tracer'
23+
import { Sampler } from './sampler'
2324

2425
export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
2526

@@ -73,12 +74,18 @@ export class RootSpan extends SpanBaseModel implements OnEndSpanEventListener {
7374
}
7475

7576
public startSpan(name: string, type: string) {
76-
let newSpan = new Span(this);
77-
newSpan.name = name
78-
newSpan.type = type
79-
newSpan.start();
80-
this._spans.push(newSpan);
81-
return newSpan;
77+
if(!this.sampler == null || this.sampler.continue(this._traceId)){
78+
let newSpan = new Span(this);
79+
newSpan.name = name
80+
newSpan.type = type
81+
newSpan.start();
82+
this._spans.push(newSpan);
83+
return newSpan;
84+
}else{
85+
//TODO
86+
debug("ELDREY -> RootSpan return startSpan null")
87+
return
88+
}
8289
}
8390

8491
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import { Stackdriver } from '../../exporters/stackdriver/stackdriver'
2222
import { StackdriverOptions } from '../../exporters/stackdriver/options'
2323
import { Exporter } from '../../exporters/exporter'
2424
import { TraceContext, OnEndSpanEventListener } from '../types/tracetypes';
25-
import { TracerConfig, defaultConfig } from '../tracing';
25+
import { TracerConfig, defaultConfig } from '../tracing'
2626
import { Buffer } from '../../exporters/buffer'
27+
import { Sampler } from './sampler'
2728

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

@@ -38,7 +39,7 @@ export class Tracer implements OnEndSpanEventListener {
3839
private config: TracerConfig;
3940

4041
//TODO: simple solution - to be rewied in future
41-
private eventListeners: OnEndSpanEventListener[] = [];
42+
private eventListeners: OnEndSpanEventListener[] = [];
4243
//TODO: temp solution
4344
private endedTraces: RootSpan[] = [];
4445

@@ -73,9 +74,16 @@ export class Tracer implements OnEndSpanEventListener {
7374
return this._active;
7475
}
7576

76-
public startRootSpan(context?: TraceContext): RootSpan {
77+
public startRootSpan(context?: TraceContext, sampler?: Sampler): RootSpan {
7778
let newTrace = new RootSpan(this, context);
79+
debug("tracer startRootSpan ")
7880
this.setCurrentRootSpan(newTrace);
81+
if(sampler == null){
82+
sampler = new Sampler(newTrace.traceId);
83+
sampler.always();
84+
}
85+
debug("tracer startRootSpan ")
86+
newTrace.sampler = sampler;
7987
newTrace.start();
8088
return newTrace;
8189
}
@@ -86,7 +94,7 @@ export class Tracer implements OnEndSpanEventListener {
8694
return debug('cannot end trace - no active trace found')
8795
}
8896
if (this.currentRootSpan != root) {
89-
return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
97+
return debug('currentRootSpan != root on notifyEnd. Possbile implementation bug.')
9098
}
9199
this.notifyEndSpan(this.currentRootSpan);
92100
//this.clearCurrentTrace();
@@ -96,16 +104,11 @@ export class Tracer implements OnEndSpanEventListener {
96104
public runInContex<T>(fn: Func<T>): T {
97105
return this.contextManager.runAndReturn(fn)
98106
}
99-
107+
100108
public registerEndSpanListener(listener: OnEndSpanEventListener) {
101109
this.eventListeners.push(listener);
102110
//this.buffer.registerExporter(exporter)
103111
}
104-
105-
/*public registerExporter(exporter: Exporter) {
106-
//this.eventListeners.push(listner);
107-
this.buffer.registerExporter(exporter)
108-
}*/
109112

110113
private notifyEndSpan(root: RootSpan) {
111114
if (this.active) {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 = 1;
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(traceId.length - 4)
73+
let j;
74+
let lower_long: number
75+
for(j = 0; j < lower_bytes.length; j++) {
76+
lower_long = lower_bytes.charCodeAt(j);
77+
}
78+
79+
if(lower_long <= this.idUpperBound){
80+
return true
81+
}else{
82+
return false;
83+
}
84+
}
85+
86+
}

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

Lines changed: 12 additions & 0 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; }
@@ -49,6 +50,7 @@ export abstract class SpanBaseModel {
4950
//links
5051
//TODO truncated
5152
private _truncated: boolean;
53+
private _sampler: Sampler;
5254

5355
constructor() {
5456
this._className = this.constructor.name;
@@ -141,6 +143,16 @@ export abstract class SpanBaseModel {
141143
this.annotations[key] = value;
142144
}
143145

146+
public get sampler(){
147+
debug('tracetypes get sampler()')
148+
return this._sampler;
149+
}
150+
151+
public set sampler(sampler:Sampler){
152+
debug('tracetypes set sempler(sampler)')
153+
this._sampler = sampler;
154+
}
155+
144156
public start() {
145157
if (this.started) {
146158
debug('calling %s.start() on already started %s %o',

0 commit comments

Comments
 (0)