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

Commit b1a7b09

Browse files
crdgonzalezcadraffensperger
authored andcommitted
Update types to commit 92edaa9 (#79)
1 parent 64081eb commit b1a7b09

10 files changed

Lines changed: 243 additions & 159 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { Span } from './span';
2222
export class RootSpan extends Span {
2323
constructor(
2424
/** Trace associated with this root span. */
25-
private readonly tracer: webTypes.Tracer,
25+
private readonly tracer: webTypes.TracerBase,
2626
/** A trace options object to build the root span. */
2727
context?: webTypes.TraceOptions
2828
) {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
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 * as webTypes from '@opencensus/web-types';
18+
import { NoHeadersPropagation } from '../propagation/no_headers_propagation';
19+
import { AlwaysSampler } from '../sampler/sampler';
20+
import { RootSpan } from './root-span';
21+
import { Span } from './span';
22+
23+
const NO_HEADERS_PROPAGATION = new NoHeadersPropagation();
24+
25+
/** TracerBase represents a tracer */
26+
export class TracerBase implements webTypes.TracerBase {
27+
/**
28+
* A sampler used to make trace sample decisions. In the case of
29+
* opencensus-web, ultimate sampling decisions will likely be made by the
30+
* server or agent/collector. So this defaults to sampling every trace.
31+
*/
32+
sampler = new AlwaysSampler();
33+
34+
/** An object to log information to. Logs to the JS console by default. */
35+
logger: webTypes.Logger = console;
36+
37+
/** Trace context header propagation behavior. */
38+
propagation = NO_HEADERS_PROPAGATION;
39+
40+
/** Event listeners for spans managed by the tracer. */
41+
eventListeners: webTypes.SpanEventListener[] = [];
42+
43+
/**
44+
* Active status from tracer instance - this is always true for
45+
* opencensus-web for code simplicity purposes.
46+
*/
47+
active = true;
48+
49+
/**
50+
* Trace parameter configuration. Not used by OpenCensus Web, but
51+
* kept for interface compatibility with @opencensus/web-types.
52+
*/
53+
readonly activeTraceParams = {};
54+
55+
/**
56+
* Starts the tracer. This makes the tracer active and sets `logger` and
57+
* `propagation` based on the given config. The `samplingRate` property of
58+
* `config` is currently ignored.
59+
*/
60+
start(config: webTypes.TracerConfig): this {
61+
this.logger = config.logger || console;
62+
this.propagation = config.propagation || NO_HEADERS_PROPAGATION;
63+
return this;
64+
}
65+
66+
/** Stops the tracer. This is a no-op with opencensus-web. */
67+
stop(): this {
68+
return this;
69+
}
70+
71+
/**
72+
* Start a new RootSpan to currentRootSpan. Currently no sampling decisions are propagated or made here.
73+
* @param options Options for tracer instance
74+
* @param fn Callback function
75+
* @returns The callback return
76+
*/
77+
startRootSpan<T>(options: webTypes.TraceOptions, fn: (root: Span) => T): T {
78+
const rootSpan = new RootSpan(this, options);
79+
rootSpan.start();
80+
return fn(rootSpan);
81+
}
82+
83+
/** Notifies listeners of the span start. */
84+
onStartSpan(root: webTypes.Span) {
85+
for (const listener of this.eventListeners) {
86+
listener.onStartSpan(root);
87+
}
88+
}
89+
90+
/** Notifies listeners of the span end. */
91+
onEndSpan(root: webTypes.Span) {
92+
for (const listener of this.eventListeners) {
93+
listener.onEndSpan(root);
94+
}
95+
}
96+
97+
registerSpanEventListener(listener: webTypes.SpanEventListener) {
98+
this.eventListeners.push(listener);
99+
}
100+
101+
unregisterSpanEventListener(listener: webTypes.SpanEventListener) {
102+
this.eventListeners = this.eventListeners.filter(l => l !== listener);
103+
}
104+
105+
/**
106+
* Start a new Span.
107+
* @param name SpanOptions object.
108+
* @returns The new Span instance started
109+
*/
110+
startChildSpan(options?: webTypes.SpanOptions): Span {
111+
let span = new Span();
112+
if (options && options.childOf) {
113+
span = options.childOf as Span;
114+
}
115+
return span.startChildSpan(options);
116+
}
117+
}

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

Lines changed: 11 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,15 @@
1515
*/
1616

1717
import * as webTypes from '@opencensus/web-types';
18-
import { NoHeadersPropagation } from '../propagation/no_headers_propagation';
19-
import { AlwaysSampler } from '../sampler/sampler';
2018
import { RootSpan } from './root-span';
2119
import { Span } from './span';
22-
23-
const NO_HEADERS_PROPAGATION = new NoHeadersPropagation();
20+
import { TracerBase } from './tracer-base';
2421

2522
/** Tracer manages the current root span and trace header propagation. */
26-
export class Tracer implements webTypes.Tracer {
23+
export class Tracer extends TracerBase implements webTypes.Tracer {
2724
/** Get and set the currentRootSpan of the tracer. */
2825
currentRootSpan: Span = new RootSpan(this);
2926

30-
/**
31-
* A sampler used to make trace sample decisions. In the case of
32-
* opencensus-web, ultimate sampling decisions will likely be made by the
33-
* server or agent/collector. So this defaults to sampling every trace.
34-
*/
35-
sampler = new AlwaysSampler();
36-
37-
/** An object to log information to. Logs to the JS console by default. */
38-
logger: webTypes.Logger = console;
39-
40-
/** Trace context header propagation behavior. */
41-
propagation = NO_HEADERS_PROPAGATION;
42-
43-
/** Event listeners for spans managed by the tracer. */
44-
eventListeners: webTypes.SpanEventListener[] = [];
45-
46-
/**
47-
* Active status from tracer instance - this is always true for
48-
* opencensus-web for code simplicity purposes.
49-
*/
50-
active = true;
51-
52-
/**
53-
* Trace parameter configuration. Not used by OpenCensus Web, but
54-
* kept for interface compatibility with @opencensus/web-types.
55-
*/
56-
readonly activeTraceParams = {};
57-
58-
/**
59-
* Starts the tracer. This makes the tracer active and sets `logger` and
60-
* `propagation` based on the given config. The `samplingRate` property of
61-
* `config` is currently ignored.
62-
*/
63-
start(config: webTypes.TracerConfig): Tracer {
64-
this.logger = config.logger || console;
65-
this.propagation = config.propagation || NO_HEADERS_PROPAGATION;
66-
return this;
67-
}
68-
69-
/** Stops the tracer. This is a no-op with opencensus-web. */
70-
stop(): Tracer {
71-
return this;
72-
}
73-
7427
/**
7528
* Start a new RootSpan to currentRootSpan. Currently opencensus-web only
7629
* supports a single root span at a time, so this just sets `currentRootSpan`
@@ -81,33 +34,13 @@ export class Tracer implements webTypes.Tracer {
8134
* @returns The callback return
8235
*/
8336
startRootSpan<T>(options: webTypes.TraceOptions, fn: (root: Span) => T): T {
84-
this.currentRootSpan = new RootSpan(this, options);
85-
this.currentRootSpan.start();
86-
return fn(this.currentRootSpan);
87-
}
88-
89-
/** Notifies listeners of the span start. */
90-
onStartSpan(root: webTypes.Span) {
91-
for (const listener of this.eventListeners) {
92-
listener.onStartSpan(root);
93-
}
94-
}
95-
96-
/** Notifies listeners of the span end. */
97-
onEndSpan(root: webTypes.Span) {
98-
for (const listener of this.eventListeners) {
99-
listener.onEndSpan(root);
100-
}
101-
}
102-
103-
registerSpanEventListener(listener: webTypes.SpanEventListener) {
104-
this.eventListeners.push(listener);
105-
}
106-
107-
unregisterSpanEventListener(listener: webTypes.SpanEventListener) {
108-
this.eventListeners = this.eventListeners.filter(l => l !== listener);
37+
return super.startRootSpan(options, root => {
38+
this.currentRootSpan = root;
39+
return fn(root);
40+
});
10941
}
11042

43+
/** Clears the current root span. */
11144
clearCurrentTrace() {
11245
this.currentRootSpan = new RootSpan(this);
11346
}
@@ -118,11 +51,10 @@ export class Tracer implements webTypes.Tracer {
11851
* @param kind Span kind
11952
* @returns The new Span instance started
12053
*/
121-
startChildSpan(
122-
nameOrOptions?: string | webTypes.SpanOptions,
123-
kind?: webTypes.SpanKind
124-
): Span {
125-
return this.currentRootSpan.startChildSpan(nameOrOptions, kind);
54+
startChildSpan(options?: webTypes.SpanOptions): Span {
55+
return super.startChildSpan(
56+
Object.assign({ childOf: this.currentRootSpan }, options)
57+
);
12658
}
12759

12860
/**

packages/opencensus-web-core/test/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ import './test-trace-model-util';
2525
import './test-tracer';
2626
import './test-tracing';
2727
import './test-url-util';
28+
import './test-tracer-base';
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright 2019, OpenCensus Authors
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 * as webTypes from '@opencensus/web-types';
18+
import { RootSpan } from '../src/trace/model/root-span';
19+
import { TracerBase } from '../src/trace/model/tracer-base';
20+
21+
describe('TracerBase', () => {
22+
let tracer: TracerBase;
23+
let listener: webTypes.SpanEventListener;
24+
25+
beforeEach(() => {
26+
tracer = new TracerBase();
27+
listener = jasmine.createSpyObj<webTypes.SpanEventListener>('listener', [
28+
'onStartSpan',
29+
'onEndSpan',
30+
]);
31+
tracer.eventListeners = [listener];
32+
});
33+
34+
describe('start', () => {
35+
it('sets logger and propagation based on config', () => {
36+
const mockLogger = jasmine.createSpyObj<webTypes.Logger>('logger', [
37+
'info',
38+
]);
39+
const mockPropagation = jasmine.createSpyObj<webTypes.Propagation>(
40+
'propagation',
41+
['generate']
42+
);
43+
44+
const result = tracer.start({
45+
logger: mockLogger,
46+
propagation: mockPropagation,
47+
});
48+
49+
expect(result).toBe(tracer);
50+
expect(tracer.logger).toBe(mockLogger);
51+
expect(tracer.propagation).toBe(mockPropagation);
52+
});
53+
});
54+
55+
describe('onStartSpan', () => {
56+
it('notifies span listeners', () => {
57+
const newRoot = new RootSpan(tracer);
58+
tracer.onStartSpan(newRoot);
59+
expect(listener.onStartSpan).toHaveBeenCalledWith(newRoot);
60+
});
61+
});
62+
63+
describe('onEndSpan', () => {
64+
it('notifies span listeners', () => {
65+
const newRoot = new RootSpan(tracer);
66+
tracer.onEndSpan(newRoot);
67+
expect(listener.onEndSpan).toHaveBeenCalledWith(newRoot);
68+
});
69+
});
70+
71+
describe('registerSpanEventListener', () => {
72+
it('adds to listeners', () => {
73+
const newListener = jasmine.createSpyObj<webTypes.SpanEventListener>(
74+
'newListener',
75+
['onStartSpan', 'onEndSpan']
76+
);
77+
tracer.registerSpanEventListener(newListener);
78+
expect(tracer.eventListeners).toEqual([listener, newListener]);
79+
});
80+
});
81+
82+
describe('unregisterSpanEventListener', () => {
83+
it('removes from listeners', () => {
84+
tracer.unregisterSpanEventListener(listener);
85+
expect(tracer.eventListeners).toEqual([]);
86+
});
87+
});
88+
});

0 commit comments

Comments
 (0)