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

Commit 89db611

Browse files
committed
feat: more general config interface
1 parent 84623b0 commit 89db611

10 files changed

Lines changed: 85 additions & 86 deletions

File tree

packages/opencensus-core/src/exporters/buffer.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ import {RootSpan} from '../trace/model/types';
2121
import {OnEndSpanEventListener} from '../trace/model/types';
2222

2323
import {Exporter} from './types';
24-
import {ExporterOptions} from './types';
24+
import {Config, BufferConfig} from '../trace/config/types';
2525

26-
// TODO: Implement default size based on application size
27-
const DEFAULT_BUFFER_SIZE = 3;
28-
const DEFAULT_BUFFER_TIMEOUT = 20000; // time in milliseconds
2926

3027
/**
3128
* Controls the sending of traces to exporters
@@ -42,10 +39,10 @@ export class Buffer {
4239
/** Indicates when the buffer timeout is running */
4340
private bufferTimeoutInProgress: boolean = false;
4441

45-
constructor(exporter: Exporter, bufferSize?: number, bufferTimeout?: number) {
46-
this.bufferSize = bufferSize || DEFAULT_BUFFER_SIZE;
47-
this.bufferTimeout = bufferTimeout || DEFAULT_BUFFER_TIMEOUT;
42+
constructor(exporter: Exporter, config: BufferConfig) {
4843
this.exporter = exporter;
44+
this.bufferSize = config.bufferSize;
45+
this.bufferTimeout = config.bufferTimeout;
4946
return this;
5047
}
5148

packages/opencensus-core/src/exporters/console-exporter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Exporter, ExporterOptions} from '../exporters/types';
17+
import {Exporter, ExporterConfig} from '../exporters/types';
1818
import {RootSpan} from '../trace/model/types';
19-
2019
import {Buffer} from './buffer';
2120

2221

22+
2323
/** Do not send span data */
2424
export class NoopExporter implements Exporter {
2525
onEndSpan(root: RootSpan) {}
@@ -30,8 +30,8 @@ export class NoopExporter implements Exporter {
3030
export class ConsoleLogExporter implements Exporter {
3131
private buffer: Buffer;
3232

33-
constructor(options: ExporterOptions) {
34-
this.buffer = new Buffer(this, options.bufferSize, options.bufferTimeout);
33+
constructor(config: ExporterConfig) {
34+
this.buffer = new Buffer(this, config);
3535
}
3636

3737
onEndSpan(root: RootSpan) {

packages/opencensus-core/src/exporters/types.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616

1717

1818
import {OnEndSpanEventListener, RootSpan} from '../trace/model/types';
19+
import { BufferConfig } from '../trace/config/types';
1920

2021
export interface Exporter extends OnEndSpanEventListener {
2122
publish(rootSpans: RootSpan[]);
2223
}
2324

24-
/**
25-
* TODO: Interface to exporters options
26-
*/
27-
export interface ExporterOptions {
28-
bufferSize?: number;
29-
bufferTimeout?: number;
30-
}
25+
export type ExporterConfig = BufferConfig;

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
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-
*/
1+
import {Exporter} from '../../exporters/types';
2+
import {PluginNames} from '../instrumentation/types';
163

174
/**
185
* This interface represent the probability of a tracer.
@@ -49,3 +36,31 @@ export interface Sampler {
4936
*/
5037
shouldSample(traceId: string): boolean;
5138
}
39+
40+
/**
41+
* Interface configuration for a buffer
42+
*/
43+
export interface BufferConfig {
44+
bufferSize?: number;
45+
bufferTimeout?: number
46+
}
47+
48+
/** Defines tracer configuration parameters */
49+
export interface TracerConfig {
50+
/** Determines the samplin rate. Ranges from 0.0 to 1.0 */
51+
samplingRate?: number;
52+
/** Determines the ignored (or blacklisted) URLs */
53+
ignoreUrls?: Array<string|RegExp>;
54+
}
55+
56+
/** Available configuration options. */
57+
export interface TracingConfig {
58+
logLevel?: number;
59+
maximumLabelValueSize?: number;
60+
plugins?: PluginNames;
61+
exporter?: Exporter;
62+
}
63+
64+
export type Config = TracingConfig&TracerConfig&BufferConfig;
65+
66+

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export interface Plugin {
2323
applyPatch(module: {}, tracer: Tracer, version: string): any;
2424
applyUnpatch(): void;
2525
}
26+
27+
export type PluginNames = {[pluginName: string]: string;};

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ export abstract class SpanBaseModel implements Span {
5151
type: string = null;
5252
/** A final status for this span */
5353
status: number;
54-
/** A sampler that will decide if the span will be sampled or not */
55-
sampler: Sampler;
5654

5755
/** Constructs a new SpanBaseModel instance. */
5856
constructor() {

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import * as cls from '../../internal/cls';
1818
import {debug} from '../../internal/util';
1919
import {RootSpan, Span} from './types';
20-
import {TraceOptions, TracerConfig, defaultConfig, Tracer} from './types';
20+
import {TraceOptions, Tracer} from './types';
2121
import {OnEndSpanEventListener, Func } from './types';
22-
import {Sampler} from '../config/types';
22+
import {Sampler, TracerConfig} from '../config/types';
23+
import {Config} from '../config/types';
2324
import {SpanImpl} from './span';
2425
import {SamplerImpl} from '../config/sampler'
2526
import {RootSpanImpl} from './rootspan';
@@ -31,12 +32,14 @@ export class TracerImpl implements Tracer {
3132
private activeLocal: boolean;
3233
private contextManager: cls.Namespace;
3334
private config: TracerConfig;
35+
sampler: Sampler;
3436

3537
//TODO: simple solution - to be rewied in future
3638
private eventListenersLocal: OnEndSpanEventListener[] = [];
3739
//TODO: temp solution
3840
private endedTraces: RootSpan[] = [];
39-
41+
42+
4043
samplingRate: number;
4144

4245
constructor() {
@@ -52,9 +55,10 @@ export class TracerImpl implements Tracer {
5255
this.contextManager.set('rootspan', root);
5356
}
5457

55-
start(config?: TracerConfig): Tracer {
58+
start(config: TracerConfig): Tracer {
5659
this.activeLocal = true;
57-
this.config = config || defaultConfig;
60+
this.config = config;
61+
this.sampler = new SamplerImpl().probability(config.samplingRate);
5862
return this;
5963
}
6064

@@ -72,22 +76,19 @@ export class TracerImpl implements Tracer {
7276

7377
startRootSpan<T>(options: TraceOptions, fn: (root: RootSpan) => T): T {
7478
return this.contextManager.runAndReturn((root) => {
75-
const newRoot = new RootSpanImpl (this, options);
76-
this.currentRootSpan = newRoot;
77-
if (!options) {
78-
options = {} as TraceOptions;
79-
}
80-
// if (!options.sampler) {
81-
// options.sampler = new SamplerImpl(newRoot.traceId);
82-
// options.sampler.always();
83-
// }
84-
// newRoot.sampler = options.sampler;
85-
newRoot.sampler = new SamplerImpl().probability(this.samplingRate);
86-
if (newRoot.sampler.shouldSample(newRoot.traceId)) {
87-
newRoot.start();
88-
return fn(newRoot);
79+
let newRoot = null;
80+
if(this.active) {
81+
newRoot = new RootSpanImpl (this, options);
82+
this.currentRootSpan = newRoot;
83+
84+
if (this.sampler.shouldSample(newRoot.traceId)) {
85+
newRoot.start();
86+
return fn(newRoot);
87+
}
88+
} else {
89+
debug("Tracer is inactive, can't start new RootSpan")
8990
}
90-
return fn(null);
91+
return fn(newRoot);
9192
});
9293
}
9394

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Sampler } from '../config/types';
17+
import { Sampler, Config, TracerConfig } from '../config/types';
1818

1919
/** Default type for functions */
2020
export type Func<T> = (...args: any[]) => T;
@@ -55,27 +55,13 @@ export interface Link {
5555
attributes: Attributes
5656
}
5757

58-
/** Defines tracer configuration parameters */
59-
export interface TracerConfig {
60-
/** Determines the samplin rate. Ranges from 0.0 to 1.0 */
61-
sampleRate?: number;
62-
/** Determines the ignored (or blacklisted) URLs */
63-
ignoreUrls?: Array<string|RegExp>;
64-
}
65-
66-
/** Defines a default tracer configuration */
67-
export const defaultConfig: TracerConfig = {
68-
sampleRate: 1.0
69-
};
7058

7159
/** Defines the trace options */
7260
export interface TraceOptions {
7361
/** Root span name */
7462
name: string;
7563
/** Trace context */
7664
traceContext?: TraceContext;
77-
/** Sampler */
78-
sampler?: Sampler;
7965
/** Span type */
8066
type?: string;
8167
}
@@ -124,8 +110,6 @@ export interface Span {
124110
messageEvents: MessageEvent[];
125111
/** Pointers from the current span to another span */
126112
links: Link[];
127-
/** A sampler that will decide if the span will be sampled or not */
128-
sampler: Sampler;
129113
/** Constructs a new SpanBaseModel instance. */
130114
readonly traceId: string;
131115
/** Indicates if span was started. */
@@ -202,10 +186,12 @@ export interface RootSpan extends Span, OnEndSpanEventListener {
202186

203187
/** Interface for Tracer */
204188
export interface Tracer {
189+
205190
/** Get and set the currentRootSpan to tracer instance */
206191
currentRootSpan: RootSpan;
207-
208-
samplingRate: number;
192+
193+
/** A sampler that will decide if the span will be sampled or not */
194+
sampler: Sampler;
209195

210196
/** Get the eventListeners from tracer instance */
211197
readonly eventListeners: OnEndSpanEventListener[];
@@ -217,7 +203,7 @@ export interface Tracer {
217203
* @param config Configuration for tracer instace
218204
* @returns A tracer instance started
219205
*/
220-
start(config?: TracerConfig): Tracer;
206+
start(config: TracerConfig): Tracer;
221207

222208
/** Stop the tracer instance */
223209
stop(): void;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
import {Tracer} from './model/types';
1818
import {Sampler} from './config/types';
1919
import {Exporter} from '../exporters/types';
20+
import {Config} from './config/types';
2021

2122

2223
export interface Tracing {
2324
readonly tracer: Tracer;
2425
// readonly sampler: Sampler;
2526
readonly exporter: Exporter;
26-
start(opstion?:OptionsTracing): Tracing;
27+
start(config?:Config): Tracing;
2728
stop(): void;
29+
registerExporter(exporter: Exporter): Tracing;
2830
}
29-
export interface OptionsTracing {
30-
samplingRate ?: number;
31-
}
31+

packages/opencensus-core/test/test-tracer.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ import * as assert from 'assert';
1818
import * as mocha from 'mocha';
1919

2020
import {Span,RootSpan,Tracer} from '../src/trace/model/types';
21-
import { Exporter } from '../src/exporters/types';
22-
import { TracerImpl } from '../src/trace/model/tracer';
23-
import { RootSpanImpl } from '../src/trace/model/rootspan';
24-
import { SpanImpl } from '../src/trace/model/span';
21+
import {Exporter} from '../src/exporters/types';
22+
import {TracerImpl} from '../src/trace/model/tracer';
23+
import {RootSpanImpl} from '../src/trace/model/rootspan';
24+
import {SpanImpl} from '../src/trace/model/span';
25+
import {TracerConfig} from '../src/trace/config/types';
26+
27+
const defaultConfig: TracerConfig = {
28+
samplingRate: 1
29+
}
2530

2631

2732
describe('Tracer', function () {
@@ -38,13 +43,13 @@ describe('Tracer', function () {
3843
describe('start()', function () {
3944
it('should return a tracer instance', function () {
4045
let tracer = new TracerImpl();
41-
let tracerStarted = tracer.start();
46+
let tracerStarted = tracer.start(defaultConfig);
4247
assert.ok(tracerStarted instanceof TracerImpl);
4348
});
4449

4550
it('the trace was started', function () {
4651
let tracer = new TracerImpl();
47-
let tracerStarted = tracer.start();
52+
let tracerStarted = tracer.start(defaultConfig);
4853
assert.ok(tracerStarted.active);
4954
});
5055
});
@@ -53,7 +58,7 @@ describe('Tracer', function () {
5358

5459
it('should start the rootSpan', function () {
5560
let tracer = new TracerImpl();
56-
tracer.start();
61+
tracer.start(defaultConfig);
5762
let rootSpan = tracer.startRootSpan(options, callback);
5863

5964
assert.ok(rootSpan.started);

0 commit comments

Comments
 (0)