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

Commit 780e190

Browse files
committed
Create the rootSpan and send the trace
1 parent 82464ed commit 780e190

14 files changed

Lines changed: 257 additions & 90 deletions

File tree

examples/user_interaction/client/src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class App extends React.Component {
9797
and calculate the amount of prime numbers in between 1 and 100000. These
9898
calculations are done using slow methods in order to measure the traces.</p>
9999

100-
<button onClick={this.handleClick}>Trace user interaction</button>
100+
<button id='trace_interaction' data-ocweb-id='Trace user interaction' onClick={this.handleClick}>Trace user interaction</button>
101101

102102
<p>The value of Pi is: <code>{this.state.pi.value}</code> and it took
103103
<code> {this.state.pi.time} ms </code> to compute this.</p>

packages/opencensus-web-all/package-lock.json

Lines changed: 0 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-web-core/karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = (config) => {
2626
browsers: ['ChromeHeadless'],
2727
frameworks: ['jasmine'],
2828
reporters: ['spec', 'coverage-istanbul'],
29-
files: ['test/index.ts'],
29+
files: ['test/index.ts', 'node_modules/zone.js/dist/zone.js'],
3030
preprocessors: {'test/index.ts': ['webpack']},
3131
// Use webpack so that tree-shaking will remove all Node.js dependencies of
3232
// the `@opencensus/web-types` library, since they are not actually used in this

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ export * from './common/id-util';
5454

5555
// Export global tracing instance.
5656
import { Tracing } from './trace/model/tracing';
57-
const tracing = new Tracing();
57+
const tracing = Tracing.instance;
5858
export { tracing };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,6 @@ export class TracerBase implements webTypes.TracerBase {
136136

137137
/** Sets the current root span. */
138138
setCurrentRootSpan(root: webTypes.Span) {
139-
// no-op, this is only required in case of tracer with cls.
139+
// no-op, this is only required in case of tracer with Zones.
140140
}
141141
}

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

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,65 @@ import * as webTypes from '@opencensus/web-types';
1818
import { RootSpan } from './root-span';
1919
import { Span } from './span';
2020
import { TracerBase } from './tracer-base';
21+
import { randomTraceId } from '../../common/id-util';
2122

2223
/** Tracer manages the current root span and trace header propagation. */
2324
export class Tracer extends TracerBase implements webTypes.Tracer {
24-
/** Get and set the currentRootSpan of the tracer. */
25-
currentRootSpan: Span = new RootSpan(this);
25+
/**
26+
* Gets the current root span associated to Zone.current.
27+
* If the current zone does not have a root span (e.g. root zone),
28+
* create a new root.
29+
*/
30+
get currentRootSpan(): Span {
31+
if (Zone.current.get('data')) {
32+
return Zone.current.get('data').rootSpan;
33+
}
34+
return new RootSpan(this);
35+
}
36+
37+
/**
38+
* Sets the current root span to the current Zone.
39+
* If the current zone does not have a 'data' property (e.g. root zone)
40+
* do not set the root span.
41+
*/
42+
set currentRootSpan(root: Span) {
43+
if (Zone.current.get('data')) {
44+
Zone.current.get('data')['rootSpan'] = root;
45+
}
46+
}
2647

2748
/**
28-
* Start a new RootSpan to currentRootSpan. Currently opencensus-web only
29-
* supports a single root span at a time, so this just sets `currentRootSpan`
30-
* to a new root span based on the given options and invokes the passed
31-
* function. Currently no sampling decisions are propagated or made here.
49+
* Creates a new Zone and start a new RootSpan to `currentRootSpan` associating
50+
* the new RootSpan to the new Zone. Thus, there might be several root spans
51+
* at the same time.
52+
* Currently no sampling decisions are propagated or made here.
3253
* @param options Options for tracer instance
3354
* @param fn Callback function
3455
* @returns The callback return
3556
*/
3657
startRootSpan<T>(options: webTypes.TraceOptions, fn: (root: Span) => T): T {
37-
return super.startRootSpan(options, root => {
38-
this.currentRootSpan = root;
39-
return fn(root);
58+
let traceId = randomTraceId();
59+
if (options.spanContext && options.spanContext.traceId) {
60+
traceId = options.spanContext.traceId;
61+
}
62+
// Create the new zone.
63+
const zoneSpec = {
64+
name: traceId,
65+
properties: {
66+
data: {
67+
isTracingZone: true,
68+
traceId,
69+
},
70+
},
71+
};
72+
const newZone = Zone.root.fork(zoneSpec);
73+
74+
return newZone.run(() => {
75+
super.startRootSpan(options, root => {
76+
// Set the currentRootSpan to the new created root span.
77+
this.currentRootSpan = root;
78+
return fn(root);
79+
});
4080
});
4181
}
4282

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ export class Tracing implements webTypes.Tracing {
3131
/** Whether tracing is active - always true for opencensus-web. */
3232
active = true;
3333

34+
/** Singleton instance */
35+
private static singletonInstance: Tracing;
36+
37+
/** Gets the tracing instance. */
38+
static get instance(): Tracing {
39+
return this.singletonInstance || (this.singletonInstance = new this());
40+
}
41+
3442
/** Sets tracer and exporter config. */
3543
start(config?: webTypes.Config): webTypes.Tracing {
3644
this.tracer.start(config || {});

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

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Tracer } from '../src/trace/model/tracer';
2020
describe('Tracer', () => {
2121
let tracer: Tracer;
2222
let listener: webTypes.SpanEventListener;
23+
const options = { name: 'test' };
2324

2425
beforeEach(() => {
2526
tracer = new Tracer();
@@ -30,19 +31,38 @@ describe('Tracer', () => {
3031
tracer.eventListeners = [listener];
3132
});
3233

34+
/** Should get/set the current RootSpan from tracer instance */
35+
describe('get/set currentRootSpan()', () => {
36+
it('should get the current RootSpan from tracer instance', () => {
37+
tracer.startRootSpan(options, root => {
38+
expect(root).toBeTruthy();
39+
expect(root).toBe(tracer.currentRootSpan);
40+
});
41+
});
42+
});
43+
3344
describe('startRootSpan', () => {
34-
it('sets current root span and calls function with it', () => {
35-
const onStartFn = jasmine.createSpy('onStartFn');
45+
it('should create a new RootSpan instance', () => {
46+
tracer.startRootSpan(options, rootSpan => {
47+
expect(rootSpan).toBeTruthy();
48+
});
49+
});
50+
it('sets current root span', () => {
3651
const oldRoot = tracer.currentRootSpan;
3752

38-
tracer.startRootSpan({ name: 'root1' }, onStartFn);
39-
40-
expect(onStartFn).toHaveBeenCalled();
41-
const onStartRoot = onStartFn.calls.argsFor(0)[0];
42-
expect(onStartRoot.name).toBe('root1');
43-
expect(onStartRoot).not.toBe(oldRoot);
44-
expect(tracer.currentRootSpan).toBe(onStartRoot);
45-
expect(listener.onStartSpan).toHaveBeenCalledWith(onStartRoot);
53+
tracer.startRootSpan(options, rootSpan => {
54+
expect(rootSpan.name).toBe('test');
55+
expect(rootSpan).not.toBe(oldRoot);
56+
expect(tracer.currentRootSpan).toBe(rootSpan);
57+
expect(listener.onStartSpan).toHaveBeenCalledWith(rootSpan);
58+
});
59+
});
60+
it('should create a new Zone and RootSpan an associated to the zone', () => {
61+
tracer.startRootSpan(options, rootSpan => {
62+
expect(rootSpan).toBeTruthy();
63+
expect(Zone.current).not.toBe(Zone.root);
64+
expect(Zone.current.get('data').rootSpan).toBe(rootSpan);
65+
});
4666
});
4767
});
4868

@@ -55,14 +75,19 @@ describe('Tracer', () => {
5575
});
5676

5777
describe('startChildSpan', () => {
78+
let rootSpanLocal: webTypes.Span;
79+
let span: webTypes.Span;
5880
it('starts a child span of the current root span', () => {
59-
spyOn(tracer.currentRootSpan, 'startChildSpan');
60-
tracer.startChildSpan({ name: 'child1', kind: webTypes.SpanKind.CLIENT });
61-
expect(tracer.currentRootSpan.startChildSpan).toHaveBeenCalledWith({
62-
childOf: tracer.currentRootSpan,
63-
name: 'child1',
64-
kind: webTypes.SpanKind.CLIENT,
81+
tracer.startRootSpan(options, rootSpan => {
82+
rootSpanLocal = rootSpan;
83+
span = tracer.startChildSpan({
84+
name: 'child1',
85+
kind: webTypes.SpanKind.CLIENT,
86+
});
6587
});
88+
expect(span).toBeTruthy();
89+
expect(rootSpanLocal.numberOfChildren).toBe(1);
90+
expect(rootSpanLocal.spans[0]).toBe(span);
6691
});
6792
});
6893

packages/opencensus-web-core/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@
1717
"test/*": ["./test/*"]
1818
}
1919
},
20-
"exclude": ["node_modules"],
21-
"include": ["src/**/*.ts", "test/**/*.ts"]
20+
"include": ["src/**/*.ts", "test/**/*.ts", "node_modules/zone.js/dist/zone.js.d.ts"]
2221
}

packages/opencensus-web-instrumentation-zone/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
"zone.js": "~0.9.1"
6666
},
6767
"dependencies": {
68-
"@opencensus/web-core": "^0.0.2"
68+
"@opencensus/web-core": "^0.0.2",
69+
"@opencensus/web-exporter-ocagent": "^0.0.2"
6970
},
7071
"peerDependencies": {
7172
"zone.js": "~0.9.1"

0 commit comments

Comments
 (0)