diff --git a/examples/basic-tracer-node/index.js b/examples/basic-tracer-node/index.js index 45888437c21..bd4f3db1db7 100644 --- a/examples/basic-tracer-node/index.js +++ b/examples/basic-tracer-node/index.js @@ -37,9 +37,8 @@ exporter.shutdown(); function doWork(parent) { // Start another span. In this example, the main method already started a // span, so that'll be the parent span, and this will be a child span. - const span = tracer.startSpan('doWork', { - parent, - }); + const ctx = opentelemetry.setActiveSpan(opentelemetry.context.active(), parent); + const span = tracer.startSpan('doWork', undefined, ctx); // simulate some random work. for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) { diff --git a/examples/collector-exporter-node/tracing.js b/examples/collector-exporter-node/tracing.js index cb522c01ee0..0cd39074f34 100644 --- a/examples/collector-exporter-node/tracing.js +++ b/examples/collector-exporter-node/tracing.js @@ -39,9 +39,8 @@ setTimeout(() => { function doWork(parent) { // Start another span. In this example, the main method already started a // span, so that'll be the parent span, and this will be a child span. - const span = tracer.startSpan('doWork', { - parent, - }); + const ctx = opentelemetry.setActiveSpan(opentelemetry.context.active(), parent); + const span = tracer.startSpan('doWork', undefined, ctx); // simulate some random work. for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) { diff --git a/examples/grpc-js/client.js b/examples/grpc-js/client.js index ee4090260ed..e0e53d0b1b8 100644 --- a/examples/grpc-js/client.js +++ b/examples/grpc-js/client.js @@ -1,5 +1,6 @@ 'use strict'; +const api = require('@opentelemetry/api'); const tracer = require('./tracer')('example-grpc-client'); // eslint-disable-next-line import/order const grpc = require('@grpc/grpc-js'); @@ -14,7 +15,7 @@ function main() { // the span, which is created to track work that happens outside of the // request lifecycle entirely. const span = tracer.startSpan('client.js:main()'); - tracer.withSpan(span, () => { + api.context.with(api.setActiveSpan(api.context.active(), span), () => { console.log('Client traceId ', span.context().traceId); const client = new services.GreeterClient( `localhost:${PORT}`, diff --git a/examples/grpc-js/server.js b/examples/grpc-js/server.js index 1d71833ff1a..3c1aec9e55d 100644 --- a/examples/grpc-js/server.js +++ b/examples/grpc-js/server.js @@ -1,5 +1,6 @@ 'use strict'; +const api = require('@opentelemetry/api'); const tracer = require('./tracer')(('example-grpc-server')); // eslint-disable-next-line import/order const grpc = require('@grpc/grpc-js'); @@ -21,11 +22,10 @@ function startServer() { } function sayHello(call, callback) { - const currentSpan = tracer.getCurrentSpan(); + const currentSpan = api.getActiveSpan(api.context.active()); // display traceid in the terminal console.log(`traceid: ${currentSpan.context().traceId}`); const span = tracer.startSpan('server.js:sayHello()', { - parent: currentSpan, kind: 1, // server attributes: { key: 'value' }, }); diff --git a/examples/grpc/client.js b/examples/grpc/client.js index 5541113af78..587292f19de 100644 --- a/examples/grpc/client.js +++ b/examples/grpc/client.js @@ -1,5 +1,6 @@ 'use strict'; +const api = require('@opentelemetry/api'); const tracer = require('./tracer')('example-grpc-client'); // eslint-disable-next-line import/order const grpc = require('grpc'); @@ -14,7 +15,7 @@ function main() { // the span, which is created to track work that happens outside of the // request lifecycle entirely. const span = tracer.startSpan('client.js:main()'); - tracer.withSpan(span, () => { + api.context.with(api.setActiveSpan(api.context.active(), span), () => { console.log('Client traceId ', span.context().traceId); const client = new services.GreeterClient( `localhost:${PORT}`, diff --git a/examples/grpc/server.js b/examples/grpc/server.js index 1b9d5331728..0270955e9c9 100644 --- a/examples/grpc/server.js +++ b/examples/grpc/server.js @@ -1,5 +1,6 @@ 'use strict'; +const api = require('@opentelemetry/api'); const tracer = require('./tracer')(('example-grpc-server')); // eslint-disable-next-line import/order const grpc = require('grpc'); @@ -20,7 +21,7 @@ function startServer() { } function sayHello(call, callback) { - const currentSpan = tracer.getCurrentSpan(); + const currentSpan = api.getActiveSpan(api.context.active()); // display traceid in the terminal console.log(`traceid: ${currentSpan.context().traceId}`); const span = tracer.startSpan('server.js:sayHello()', { diff --git a/examples/http/client.js b/examples/http/client.js index 5a88497f668..b1ed45993a3 100644 --- a/examples/http/client.js +++ b/examples/http/client.js @@ -1,5 +1,6 @@ 'use strict'; +const api = require('@opentelemetry/api'); const tracer = require('./tracer')('example-http-client'); // eslint-disable-next-line import/order const http = require('http'); @@ -10,7 +11,7 @@ function makeRequest() { // the span, which is created to track work that happens outside of the // request lifecycle entirely. const span = tracer.startSpan('makeRequest'); - tracer.withSpan(span, () => { + api.context.with(api.setActiveSpan(api.context.active(), span), () => { http.get({ host: 'localhost', port: 8080, diff --git a/examples/http/server.js b/examples/http/server.js index d142badf83b..7c5f12e5753 100644 --- a/examples/http/server.js +++ b/examples/http/server.js @@ -1,5 +1,6 @@ 'use strict'; +const api = require('@opentelemetry/api'); const tracer = require('./tracer')('example-http-server'); // eslint-disable-next-line import/order const http = require('http'); @@ -19,11 +20,10 @@ function startServer(port) { /** A function which handles requests and send response. */ function handleRequest(request, response) { - const currentSpan = tracer.getCurrentSpan(); + const currentSpan = api.getActiveSpan(api.context.active()); // display traceid in the terminal console.log(`traceid: ${currentSpan.context().traceId}`); const span = tracer.startSpan('handleRequest', { - parent: currentSpan, kind: 1, // server attributes: { key: 'value' }, }); diff --git a/examples/https/client.js b/examples/https/client.js index e5b1825163e..4b56bdbe8ca 100644 --- a/examples/https/client.js +++ b/examples/https/client.js @@ -1,5 +1,6 @@ 'use strict'; +const api = require('@opentelemetry/api'); const tracer = require('./tracer')('example-https-client'); // eslint-disable-next-line import/order const https = require('https'); @@ -10,7 +11,7 @@ function makeRequest() { // the span, which is created to track work that happens outside of the // request lifecycle entirely. const span = tracer.startSpan('makeRequest'); - tracer.withSpan(span, () => { + api.context.with(api.setActiveSpan(api.context.active(), span), () => { https.get({ host: 'localhost', port: 443, diff --git a/examples/https/server.js b/examples/https/server.js index 105005fea8c..65764e88357 100644 --- a/examples/https/server.js +++ b/examples/https/server.js @@ -1,5 +1,6 @@ 'use strict'; +const api = require('@opentelemetry/api'); // eslint-disable-next-line import/order const tracer = require('./tracer')('example-https-server'); const fs = require('fs'); @@ -24,11 +25,10 @@ function startServer(port) { /** A function which handles requests and send response. */ function handleRequest(request, response) { - const currentSpan = tracer.getCurrentSpan(); + const currentSpan = api.getActiveSpan(api.context.active()); // display traceid in the terminal console.log(`traceid: ${currentSpan.context().traceId}`); const span = tracer.startSpan('handleRequest', { - parent: currentSpan, kind: 1, // server attributes: { key: 'value' }, }); diff --git a/examples/tracer-web/examples/document-load/index.js b/examples/tracer-web/examples/document-load/index.js index fe72746a5ef..739ac5a53bc 100644 --- a/examples/tracer-web/examples/document-load/index.js +++ b/examples/tracer-web/examples/document-load/index.js @@ -1,3 +1,4 @@ +import { context, getActiveSpan, setActiveSpan } from '@opentelemetry/api'; import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; import { WebTracerProvider } from '@opentelemetry/web'; import { DocumentLoad } from '@opentelemetry/plugin-document-load'; @@ -52,31 +53,29 @@ const prepareClickEvent = () => { } const mainSpan = tracer.startSpan('click button'); - tracer.withSpan(mainSpan, () => { - const span1 = tracer.startSpan('files-series-info-1', { - parent: tracer.getCurrentSpan(), - }); + context.with(setActiveSpan(context.active(), mainSpan), () => { + const span1 = tracer.startSpan('files-series-info-1'); - const span2 = tracer.startSpan('files-series-info-2', { - parent: tracer.getCurrentSpan(), - }); + const span2 = tracer.startSpan('files-series-info-2'); - tracer.withSpan(span1, () => { + context.with(setActiveSpan(context.active(), span1), () => { getData(url1).then((data) => { - console.log('current span is span1', tracer.getCurrentSpan() === span1); + const curSpan = getActiveSpan(context.active()); + console.log('current span is span1', curSpan === span1); console.log('info from package.json', data.description, data.version); - tracer.getCurrentSpan().addEvent('fetching-span1-completed'); + curSpan.addEvent('fetching-span1-completed'); span1.end(); finish(); }); }); - tracer.withSpan(span2, () => { + context.with(setActiveSpan(context.active(), span2), () => { getData(url2).then((data) => { setTimeout(() => { - console.log('current span is span2', tracer.getCurrentSpan() === span2); + const curSpan = getActiveSpan(context.active()); + console.log('current span is span2', curSpan === span2); console.log('info from package.json', data.description, data.version); - tracer.getCurrentSpan().addEvent('fetching-span2-completed'); + curSpan.addEvent('fetching-span2-completed'); span2.end(); finish(); }, 100); diff --git a/examples/tracer-web/examples/fetch/index.js b/examples/tracer-web/examples/fetch/index.js index 6717136beef..32c923373b7 100644 --- a/examples/tracer-web/examples/fetch/index.js +++ b/examples/tracer-web/examples/fetch/index.js @@ -1,5 +1,6 @@ 'use strict'; +import { context, getActiveSpan, setActiveSpan } from '@opentelemetry/api'; import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; import { CollectorTraceExporter } from '@opentelemetry/exporter-collector'; import { WebTracerProvider } from '@opentelemetry/web'; @@ -44,22 +45,18 @@ const prepareClickEvent = () => { const element = document.getElementById('button1'); const onClick = () => { - const singleSpan = webTracerWithZone.startSpan(`files-series-info`, { - parent: webTracerWithZone.getCurrentSpan(), - }); - webTracerWithZone.withSpan(singleSpan, () => { + const singleSpan = webTracerWithZone.startSpan(`files-series-info`); + context.with(setActiveSpan(context.active(), singleSpan), () => { getData(url).then((_data) => { - webTracerWithZone.getCurrentSpan().addEvent('fetching-single-span-completed'); + getActiveSpan(context.active()).addEvent('fetching-single-span-completed'); singleSpan.end(); }); }); for (let i = 0, j = 5; i < j; i += 1) { - const span = webTracerWithZone.startSpan(`files-series-info-${i}`, { - parent: webTracerWithZone.getCurrentSpan(), - }); - webTracerWithZone.withSpan(span, () => { + const span = webTracerWithZone.startSpan(`files-series-info-${i}`); + context.with(setActiveSpan(context.active(), span), () => { getData(url).then((_data) => { - webTracerWithZone.getCurrentSpan().addEvent(`fetching-span-${i}-completed`); + getActiveSpan(context.active()).addEvent(`fetching-span-${i}-completed`); span.end(); }); }); diff --git a/examples/tracer-web/examples/xml-http-request/index.js b/examples/tracer-web/examples/xml-http-request/index.js index 2532037bc99..fd06bc19cc1 100644 --- a/examples/tracer-web/examples/xml-http-request/index.js +++ b/examples/tracer-web/examples/xml-http-request/index.js @@ -1,3 +1,4 @@ +import { context, getActiveSpan, setActiveSpan } from '@opentelemetry/api'; import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; import { WebTracerProvider } from '@opentelemetry/web'; import { XMLHttpRequestInstrumentation } from '@opentelemetry/instrumentation-xml-http-request'; @@ -49,15 +50,13 @@ const prepareClickEvent = () => { const onClick = () => { for (let i = 0, j = 5; i < j; i += 1) { - const span1 = webTracerWithZone.startSpan(`files-series-info-${i}`, { - parent: webTracerWithZone.getCurrentSpan(), - }); - webTracerWithZone.withSpan(span1, () => { + const span1 = webTracerWithZone.startSpan(`files-series-info-${i}`); + context.with(setActiveSpan(context.active(), span1), () => { getData(url1).then((_data) => { - webTracerWithZone.getCurrentSpan().addEvent('fetching-span1-completed'); + getActiveSpan(context.active()).addEvent('fetching-span1-completed'); span1.end(); }, ()=> { - webTracerWithZone.getCurrentSpan().addEvent('fetching-error'); + getActiveSpan(context.active()).addEvent('fetching-error'); span1.end(); }); }); diff --git a/packages/opentelemetry-context-zone-peer-dep/README.md b/packages/opentelemetry-context-zone-peer-dep/README.md index 9314211b8da..db6bdf45d90 100644 --- a/packages/opentelemetry-context-zone-peer-dep/README.md +++ b/packages/opentelemetry-context-zone-peer-dep/README.md @@ -22,7 +22,7 @@ npm install --save @opentelemetry/context-zone-peer-dep ## Usage ```js -import { context } from '@opentelemetry/api'; +import { context, getActiveSpan, setActiveSpan } from '@opentelemetry/api'; import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; import { WebTracerProvider } from '@opentelemetry/web'; import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep'; @@ -37,20 +37,20 @@ providerWithZone.register({ const webTracerWithZone = providerWithZone.getTracer('default'); const span1 = webTracerWithZone.startSpan('foo1'); context.with(setActiveSpan(context.active(), span1, () => { - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + console.log('Current span is span1', getActiveSpan(context.active()) === span1); setTimeout(() => { const span2 = webTracerWithZone.startSpan('foo2'); - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + console.log('Current span is span1', getActiveSpan(context.active()) === span1); context.with(setActiveSpan(context.active(), span2, () => { - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span2', getActiveSpan(context.active()) === span2); setTimeout(() => { - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span2', getActiveSpan(context.active()) === span2); }, 500); }); // there is a timeout which still keeps span2 active - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span2', getActiveSpan(context.active()) === span2); }, 500); - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + console.log('Current span is span1', getActiveSpan(context.active()) === span1); }); ``` diff --git a/packages/opentelemetry-context-zone/README.md b/packages/opentelemetry-context-zone/README.md index 47936479251..bfc06e4f44d 100644 --- a/packages/opentelemetry-context-zone/README.md +++ b/packages/opentelemetry-context-zone/README.md @@ -19,7 +19,7 @@ npm install --save @opentelemetry/context-zone ## Usage ```js -import { context } from '@opentelemetry/api'; +import { context, getActiveSpan, setActiveSpan } from '@opentelemetry/api'; import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; import { WebTracerProvider } from '@opentelemetry/web'; import { ZoneContextManager } from '@opentelemetry/context-zone'; @@ -35,20 +35,20 @@ const webTracerWithZone = providerWithZone.getTracer('default'); const span1 = webTracerWithZone.startSpan('foo1'); context.with(setActiveSpan(context.active(), span1), () => { - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + console.log('Current span is span1', getActiveSpan(context.active()) === span1); setTimeout(() => { const span2 = webTracerWithZone.startSpan('foo2'); - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + console.log('Current span is span1', getActiveSpan(context.active()) === span1); context.with(setActiveSpan(context.active(), span2), () => { - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span2', getActiveSpan(context.active()) === span2); setTimeout(() => { - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span2', getActiveSpan(context.active()) === span2); }, 500); }); // there is a timeout which still keeps span2 active - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span2', getActiveSpan(context.active()) === span2); }, 500); - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + console.log('Current span is span1', getActiveSpan(context.active()) === span1); }); ```