Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fixing documentation for web tracer provider, fixing examples … #906

Merged
merged 3 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 deletions examples/tracer-web/examples/document-load/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ const provider = new WebTracerProvider({
],
});
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.addSpanProcessor(new SimpleSpanProcessor(new CollectorExporter()));

const providerWithZone = new WebTracerProvider({
provider.register({
contextManager: new ZoneContextManager(),
plugins: [
new DocumentLoad(),
],
});
providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new CollectorExporter()));

const tracerWithZone = providerWithZone.getTracer('example-tracer-web');
let window;
console.log('Current span is window', tracerWithZone.getCurrentSpan() === window);
const tracer = provider.getTracer('example-tracer-web');

const getData = (url) => new Promise((resolve, reject) => {
// eslint-disable-next-line no-undef
Expand All @@ -45,37 +39,48 @@ const prepareClickEvent = () => {
const url1 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/package.json';
const url2 = 'https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/master/packages/opentelemetry-web/package.json';

let document;
const element = document.getElementById('button1');
const mainSpan = tracerWithZone.startSpan('main-span');
tracerWithZone.bind(element, mainSpan);

const onClick = () => {
const span1 = tracerWithZone.startSpan('files-series-info-1', {
parent: tracerWithZone.getCurrentSpan(),
});
let count = 0;

const span2 = tracerWithZone.startSpan('files-series-info-2', {
parent: tracerWithZone.getCurrentSpan(),
});
function finish() {
count++;
if (count === 2) {
mainSpan.end();
}
}

tracerWithZone.withSpan(span1, () => {
getData(url1).then((data) => {
console.log('current span is span1', tracerWithZone.getCurrentSpan() === span1);
console.log('info from package.json', data.description, data.version);
tracerWithZone.getCurrentSpan().addEvent('fetching-span1-completed');
span1.end();
const mainSpan = tracer.startSpan('click button');
tracer.withSpan(mainSpan, () => {
const span1 = tracer.startSpan('files-series-info-1', {
parent: tracer.getCurrentSpan(),
});

const span2 = tracer.startSpan('files-series-info-2', {
parent: tracer.getCurrentSpan(),
});
});

tracerWithZone.withSpan(span2, () => {
getData(url2).then((data) => {
setTimeout(() => {
console.log('current span is span2', tracerWithZone.getCurrentSpan() === span2);
tracer.withSpan(span1, () => {
getData(url1).then((data) => {
console.log('current span is span1', tracer.getCurrentSpan() === span1);
console.log('info from package.json', data.description, data.version);
tracerWithZone.getCurrentSpan().addEvent('fetching-span2-completed');
span2.end();
}, 100);
tracer.getCurrentSpan().addEvent('fetching-span1-completed');
span1.end();
finish();
});
});

tracer.withSpan(span2, () => {
getData(url2).then((data) => {
setTimeout(() => {
console.log('current span is span2', tracer.getCurrentSpan() === span2);
console.log('info from package.json', data.description, data.version);
tracer.getCurrentSpan().addEvent('fetching-span2-completed');
span2.end();
finish();
}, 100);
});
});
});
};
Expand Down
2 changes: 0 additions & 2 deletions examples/tracer-web/examples/xml-http-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const getData = (url) => new Promise((resolve, _reject) => {
const prepareClickEvent = () => {
const url1 = 'https://httpbin.org/get';

let document;
const element = document.getElementById('button1');

const onClick = () => {
Expand All @@ -61,5 +60,4 @@ const prepareClickEvent = () => {
element.addEventListener('click', onClick);
};

let window;
window.addEventListener('load', prepareClickEvent);
3 changes: 2 additions & 1 deletion packages/opentelemetry-context-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"precompile": "tsc --version",
"version:update": "node ../../scripts/version-update.js",
"compile": "npm run version:update && tsc -p .",
"prepare": "npm run compile"
"prepare": "npm run compile",
"watch": "tsc -w"
},
"keywords": [
"opentelemetry",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class DocumentLoad extends BasePlugin<unknown> {
) as PerformanceResourceTiming[];
if (resources) {
resources.forEach(resource => {
this._initResourceSpan(resource);
this._initResourceSpan(resource, { parent: rootSpan });
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/opentelemetry-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,20 @@ const provider = new WebTracerProvider({
});

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.register();

// Changing default contextManager to use ZoneContextManager - supports asynchronous operations
const providerWithZone = new WebTracerProvider({
contextManager: new ZoneContextManager(),
plugins: [
new DocumentLoad()
]
});
providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

// Changing default contextManager to use ZoneContextManager - supports asynchronous operations
providerWithZone.register({
contextManager: new ZoneContextManager(),
});

```

## Useful links
Expand Down
10 changes: 10 additions & 0 deletions packages/opentelemetry-web/src/WebTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export class WebTracerProvider extends BasicTracerProvider {
for (const plugin of config.plugins) {
plugin.enable([], this, this.logger);
}

if ((config as SDKRegistrationConfig).contextManager) {
throw 'contextManager should be defined in register method not in' +
' constructor';
}
if ((config as SDKRegistrationConfig).propagator) {
throw 'propagator should be defined in register method not in constructor';
}
}

/**
Expand All @@ -61,6 +69,8 @@ export class WebTracerProvider extends BasicTracerProvider {
register(config: SDKRegistrationConfig = {}) {
if (config.contextManager === undefined) {
config.contextManager = new StackContextManager();
}
if (config.contextManager) {
config.contextManager.enable();
}

Expand Down
37 changes: 35 additions & 2 deletions packages/opentelemetry-web/test/WebTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { context } from '@opentelemetry/api';
import { BasePlugin, NoopLogger } from '@opentelemetry/core';
import { B3Propagator, BasePlugin, NoopLogger } from '@opentelemetry/core';
import { ContextManager } from '@opentelemetry/context-base';
import { ZoneContextManager } from '@opentelemetry/context-zone';
import { Tracer, Span } from '@opentelemetry/tracing';
Expand Down Expand Up @@ -79,8 +79,40 @@ describe('WebTracerProvider', () => {
});
});

it('should throw error when context manager is passed in constructor', () => {
let error = '';
try {
new WebTracerProvider({
contextManager: new ZoneContextManager(),
} as any);
} catch (e) {
error = e;
}
assert.strictEqual(
error,
'contextManager should be defined in' +
' register method not in constructor'
);
});

it('should throw error when propagator is passed in constructor', () => {
let error = '';
try {
new WebTracerProvider({
propagator: new B3Propagator(),
} as any);
} catch (e) {
error = e;
}
assert.strictEqual(
error,
'propagator should be defined in register' +
' method not in constructor'
);
});

describe('when contextManager is "ZoneContextManager"', () => {
it('should correctly return the contexts for 2 parallel actions', () => {
it('should correctly return the contexts for 2 parallel actions', done => {
const webTracerWithZone = new WebTracerProvider().getTracer('default');

const rootSpan = webTracerWithZone.startSpan('rootSpan');
Expand Down Expand Up @@ -112,6 +144,7 @@ describe('WebTracerProvider', () => {
webTracerWithZone.getCurrentSpan() === concurrentSpan2,
'Current span is concurrentSpan2'
);
done();
}, 20);
});
});
Expand Down