diff --git a/README.md b/README.md index a5dda53e57..4f55cd5259 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,16 @@ To request automatic tracing support for a module not on this list, please [file |----------------------------------------------------------|-----------------------------------------------------------------------------------------| | [@opentelemetry/shim-opentracing][otel-shim-opentracing] | OpenTracing shim allows existing OpenTracing instrumentation to report to OpenTelemetry | +## Upgrade guidelines + +### 0.14.0 to 0.15.0 + +[PR-1764](https://github.com/open-telemetry/opentelemetry-js/pull/1764) removed some APIs from `Tracer`: + +- `Tracer.getCurrentSpan()`: use `api.getSpan(api.context.active())` +- `Tracer.withSpan(span)`: use `api.context.with(api.setSpan(api.context.active(), span))` +- `Tracer.bind(target)`: use `api.context.bind(target)` + ## Useful links - For more information on OpenTelemetry, visit: diff --git a/benchmark/tracer.js b/benchmark/tracer.js index ab636e924e..274cd6de99 100644 --- a/benchmark/tracer.js +++ b/benchmark/tracer.js @@ -2,10 +2,9 @@ const benchmark = require('./benchmark'); const opentelemetry = require('../packages/opentelemetry-api'); -const { NoopLogger } = require('../packages/opentelemetry-core'); const { BasicTracerProvider, BatchSpanProcessor, InMemorySpanExporter, SimpleSpanProcessor } = require('../packages/opentelemetry-tracing'); -const logger = new NoopLogger(); +const logger = new opentelemetry.NoopLogger(); const setups = [ { diff --git a/examples/basic-tracer-node/index.js b/examples/basic-tracer-node/index.js index 45888437c2..fd046c0d45 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.setSpan(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 cb522c01ee..af9b617b04 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.setSpan(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 ee4090260e..958f29ef11 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.setSpan(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 1d71833ff1..6bd451b918 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.getSpan(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 5541113af7..5bb4b630aa 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.setSpan(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 1b9d533172..40db771365 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.getSpan(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 5a88497f66..a27ba0355e 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.setSpan(api.context.active(), span), () => { http.get({ host: 'localhost', port: 8080, diff --git a/examples/http/server.js b/examples/http/server.js index d142badf83..d7a90310fb 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.getSpan(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 e5b1825163..d842becef3 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.setSpan(api.context.active(), span), () => { https.get({ host: 'localhost', port: 443, diff --git a/examples/https/server.js b/examples/https/server.js index 105005fea8..a3d480f568 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.getSpan(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 fe72746a5e..148bbe6c64 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, getSpan, setSpan } 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(setSpan(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(setSpan(context.active(), span1), () => { getData(url1).then((data) => { - console.log('current span is span1', tracer.getCurrentSpan() === span1); + const curSpan = getSpan(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(setSpan(context.active(), span2), () => { getData(url2).then((data) => { setTimeout(() => { - console.log('current span is span2', tracer.getCurrentSpan() === span2); + const curSpan = getSpan(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 6717136bee..3ee96052b6 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, getSpan, setSpan } 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(setSpan(context.active(), singleSpan), () => { getData(url).then((_data) => { - webTracerWithZone.getCurrentSpan().addEvent('fetching-single-span-completed'); + getSpan(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(setSpan(context.active(), span), () => { getData(url).then((_data) => { - webTracerWithZone.getCurrentSpan().addEvent(`fetching-span-${i}-completed`); + getSpan(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 2532037bc9..cc8a8a4839 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, getSpan, setSpan } 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(setSpan(context.active(), span1), () => { getData(url1).then((_data) => { - webTracerWithZone.getCurrentSpan().addEvent('fetching-span1-completed'); + getSpan(context.active()).addEvent('fetching-span1-completed'); span1.end(); }, ()=> { - webTracerWithZone.getCurrentSpan().addEvent('fetching-error'); + getSpan(context.active()).addEvent('fetching-error'); span1.end(); }); }); diff --git a/integration-tests/propagation-validation-server/validation-server.js b/integration-tests/propagation-validation-server/validation-server.js index 4818fb2761..badcddb769 100644 --- a/integration-tests/propagation-validation-server/validation-server.js +++ b/integration-tests/propagation-validation-server/validation-server.js @@ -1,7 +1,7 @@ const axios = require("axios"); const { HttpTraceContext } = require("@opentelemetry/core"); const { BasicTracerProvider } = require("@opentelemetry/tracing"); -const { context, propagation, trace, ROOT_CONTEXT } = require("@opentelemetry/api"); +const { context, propagation, setSpan, trace, ROOT_CONTEXT } = require("@opentelemetry/api"); const { AsyncHooksContextManager, } = require("@opentelemetry/context-async-hooks"); @@ -36,7 +36,7 @@ app.post("/verify-tracecontext", (req, res) => { req.body.map((action) => { const span = tracer.startSpan("propagate-w3c"); let promise; - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { const headers = {}; propagation.inject(context.active(), headers); promise = axios diff --git a/packages/opentelemetry-api/README.md b/packages/opentelemetry-api/README.md index 180ab4c7e2..0f91bb0bca 100644 --- a/packages/opentelemetry-api/README.md +++ b/packages/opentelemetry-api/README.md @@ -160,7 +160,7 @@ const api = require("@opentelemetry/api"); const tracer = api.trace.getTracer("my-library-name", "0.2.3"); async function doSomething() { - const span = tracer.startSpan("doSomething", { parent: tracer.getCurrentSpan() }); + const span = tracer.startSpan("doSomething"); try { const result = await doSomethingElse(); span.end(); diff --git a/packages/opentelemetry-api/src/api/global-utils.ts b/packages/opentelemetry-api/src/api/global-utils.ts index 297836e009..5d6ad7de3e 100644 --- a/packages/opentelemetry-api/src/api/global-utils.ts +++ b/packages/opentelemetry-api/src/api/global-utils.ts @@ -65,4 +65,4 @@ export function makeGetter( * version. If the global API is not compatible with the API package * attempting to get it, a NOOP API implementation will be returned. */ -export const API_BACKWARDS_COMPATIBILITY_VERSION = 2; +export const API_BACKWARDS_COMPATIBILITY_VERSION = 3; diff --git a/packages/opentelemetry-api/src/trace/NoopTracer.ts b/packages/opentelemetry-api/src/trace/NoopTracer.ts index 239db89b22..bb25bcbe1d 100644 --- a/packages/opentelemetry-api/src/trace/NoopTracer.ts +++ b/packages/opentelemetry-api/src/trace/NoopTracer.ts @@ -24,10 +24,6 @@ import { getSpanContext } from '../context/context'; * No-op implementations of {@link Tracer}. */ export class NoopTracer implements Tracer { - getCurrentSpan(): Span { - return NOOP_SPAN; - } - // startSpan starts a noop span. startSpan(name: string, options?: SpanOptions, context?: Context): Span { const root = Boolean(options?.root); @@ -46,17 +42,6 @@ export class NoopTracer implements Tracer { return NOOP_SPAN; } } - - withSpan ReturnType>( - span: Span, - fn: T - ): ReturnType { - return fn(); - } - - bind(target: T, _span?: Span): T { - return target; - } } function isSpanContext(spanContext: any): spanContext is SpanContext { diff --git a/packages/opentelemetry-api/src/trace/ProxyTracer.ts b/packages/opentelemetry-api/src/trace/ProxyTracer.ts index e2216eed5e..c6e22433e7 100644 --- a/packages/opentelemetry-api/src/trace/ProxyTracer.ts +++ b/packages/opentelemetry-api/src/trace/ProxyTracer.ts @@ -31,25 +31,10 @@ export class ProxyTracer implements Tracer { public readonly version?: string ) {} - getCurrentSpan(): Span | undefined { - return this._getTracer().getCurrentSpan(); - } - startSpan(name: string, options?: SpanOptions): Span { return this._getTracer().startSpan(name, options); } - withSpan ReturnType>( - span: Span, - fn: T - ): ReturnType { - return this._getTracer().withSpan(span, fn); - } - - bind(target: T, span?: Span): T { - return this._getTracer().bind(target, span); - } - /** * Try to get a tracer from the proxy tracer provider. * If the proxy tracer provider has no delegate, return a noop tracer. diff --git a/packages/opentelemetry-api/src/trace/tracer.ts b/packages/opentelemetry-api/src/trace/tracer.ts index 6ba051cfee..abc72a53f3 100644 --- a/packages/opentelemetry-api/src/trace/tracer.ts +++ b/packages/opentelemetry-api/src/trace/tracer.ts @@ -27,24 +27,9 @@ import { SpanOptions } from './SpanOptions'; */ export interface Tracer { /** - * Returns the current Span from the current context if available. + * Starts a new {@link Span}. Start the span without setting it on context. * - * If there is no Span associated with the current context, `undefined` is - * returned. - * - * To install a {@link Span} to the current Context use - * {@link Tracer.withSpan}. - * - * @returns Span The currently active Span - */ - getCurrentSpan(): Span | undefined; - - /** - * Starts a new {@link Span}. Start the span without setting it as the current - * span in this tracer's context. - * - * This method do NOT modify the current Context. To install a {@link - * Span} to the current Context use {@link Tracer.withSpan}. + * This method do NOT modify the current Context. * * @param name The name of the span * @param [options] SpanOptions used for span creation @@ -56,32 +41,4 @@ export interface Tracer { * span.end(); */ startSpan(name: string, options?: SpanOptions, context?: Context): Span; - - /** - * Executes the function given by fn within the context provided by Span. - * - * This is a convenience method for creating spans attached to the tracer's - * context. Applications that need more control over the span lifetime should - * use {@link Tracer.startSpan} instead. - * - * @param span The span that provides the context - * @param fn The function to be executed inside the provided context - * @example - * tracer.withSpan(span, () => { - * tracer.getCurrentSpan().addEvent("parent's event"); - * doSomeOtherWork(); // Here "span" is the current Span. - * }); - */ - withSpan ReturnType>( - span: Span, - fn: T - ): ReturnType; - - /** - * Bind a span as the target's context or propagate the current one. - * - * @param target Any object to which a context need to be set - * @param [context] Optionally specify the context which you want to bind - */ - bind(target: T, context?: Span): T; } diff --git a/packages/opentelemetry-api/test/api/api.test.ts b/packages/opentelemetry-api/test/api/api.test.ts index d2ca5daab5..01aaf32df6 100644 --- a/packages/opentelemetry-api/test/api/api.test.ts +++ b/packages/opentelemetry-api/test/api/api.test.ts @@ -36,8 +36,6 @@ import api, { } from '../../src'; describe('API', () => { - const functions = ['getCurrentSpan', 'startSpan', 'withSpan']; - it('should expose a tracer provider via getTracerProvider', () => { const tracer = api.trace.getTracerProvider(); assert.ok(tracer); @@ -59,20 +57,6 @@ describe('API', () => { metrics.disable(); }); - it('should not crash', () => { - functions.forEach(fn => { - const tracer = api.trace.getTracerProvider(); - try { - ((tracer as unknown) as { [fn: string]: Function })[fn](); // Try to run the function - assert.ok(true, fn); - } catch (err) { - if (err.message !== 'Method not implemented.') { - assert.ok(true, fn); - } - } - }); - }); - it('should use the global tracer provider', () => { api.trace.setGlobalTracerProvider(new TestTracerProvider()); const tracer = api.trace.getTracerProvider().getTracer('name'); diff --git a/packages/opentelemetry-api/test/noop-implementations/noop-tracer.test.ts b/packages/opentelemetry-api/test/noop-implementations/noop-tracer.test.ts index 232428e48d..3730968323 100644 --- a/packages/opentelemetry-api/test/noop-implementations/noop-tracer.test.ts +++ b/packages/opentelemetry-api/test/noop-implementations/noop-tracer.test.ts @@ -40,24 +40,6 @@ describe('NoopTracer', () => { }), NOOP_SPAN ); - - assert.deepStrictEqual(tracer.getCurrentSpan(), NOOP_SPAN); - }); - - it('should not crash when .withSpan()', done => { - const tracer = new NoopTracer(); - tracer.withSpan(NOOP_SPAN, () => { - return done(); - }); - }); - - it('should not crash when .bind()', done => { - const tracer = new NoopTracer(); - const fn = () => { - return done(); - }; - const patchedFn = tracer.bind(fn, NOOP_SPAN); - return patchedFn(); }); it('should propagate valid spanContext on the span (from context)', () => { diff --git a/packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts b/packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts index 7a110cbfa3..41db382f3d 100644 --- a/packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts +++ b/packages/opentelemetry-api/test/proxy-implementations/proxy-tracer.test.ts @@ -56,8 +56,6 @@ describe('ProxyTracer', () => { }), NOOP_SPAN ); - - assert.deepStrictEqual(tracer.getCurrentSpan(), NOOP_SPAN); }); }); @@ -96,18 +94,9 @@ describe('ProxyTracer', () => { beforeEach(() => { delegateSpan = new NoopSpan(); delegateTracer = { - bind(target) { - return target; - }, - getCurrentSpan() { - return delegateSpan; - }, startSpan() { return delegateSpan; }, - withSpan(span, fn) { - return fn(); - }, }; tracer = provider.getTracer('test'); diff --git a/packages/opentelemetry-context-zone-peer-dep/README.md b/packages/opentelemetry-context-zone-peer-dep/README.md index 15edbcb0dd..421fa937a2 100644 --- a/packages/opentelemetry-context-zone-peer-dep/README.md +++ b/packages/opentelemetry-context-zone-peer-dep/README.md @@ -22,6 +22,7 @@ npm install --save @opentelemetry/context-zone-peer-dep ## Usage ```js +import { context, getSpan, setSpan } from '@opentelemetry/api'; import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; import { WebTracerProvider } from '@opentelemetry/web'; import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep'; @@ -35,21 +36,21 @@ providerWithZone.register({ // Example how the ZoneContextManager keeps the reference to the correct context during async operations const webTracerWithZone = providerWithZone.getTracer('default'); const span1 = webTracerWithZone.startSpan('foo1'); -webTracerWithZone.withSpan(span1, () => { - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); +context.with(setSpan(context.active(), span1, () => { + console.log('Current span is span1', getSpan(context.active()) === span1); setTimeout(() => { const span2 = webTracerWithZone.startSpan('foo2'); - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); - webTracerWithZone.withSpan(span2, () => { - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span1', getSpan(context.active()) === span1); + context.with(setSpan(context.active(), span2, () => { + console.log('Current span is span2', getSpan(context.active()) === span2); setTimeout(() => { - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span2', getSpan(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', getSpan(context.active()) === span2); }, 500); - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + console.log('Current span is span1', getSpan(context.active()) === span1); }); ``` diff --git a/packages/opentelemetry-context-zone/README.md b/packages/opentelemetry-context-zone/README.md index 4e26999004..e2c3ae72e1 100644 --- a/packages/opentelemetry-context-zone/README.md +++ b/packages/opentelemetry-context-zone/README.md @@ -19,6 +19,7 @@ npm install --save @opentelemetry/context-zone ## Usage ```js +import { context, getSpan, setSpan } from '@opentelemetry/api'; import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; import { WebTracerProvider } from '@opentelemetry/web'; import { ZoneContextManager } from '@opentelemetry/context-zone'; @@ -32,21 +33,22 @@ provider.register({ // Example how the ZoneContextManager keeps the reference to the correct context during async operations const webTracerWithZone = providerWithZone.getTracer('default'); const span1 = webTracerWithZone.startSpan('foo1'); -webTracerWithZone.withSpan(span1, () => { - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + +context.with(setSpan(context.active(), span1), () => { + console.log('Current span is span1', getSpan(context.active()) === span1); setTimeout(() => { const span2 = webTracerWithZone.startSpan('foo2'); - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); - webTracerWithZone.withSpan(span2, () => { - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span1', getSpan(context.active()) === span1); + context.with(setSpan(context.active(), span2), () => { + console.log('Current span is span2', getSpan(context.active()) === span2); setTimeout(() => { - console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2); + console.log('Current span is span2', getSpan(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', getSpan(context.active()) === span2); }, 500); - console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1); + console.log('Current span is span1', getSpan(context.active()) === span1); }); ``` diff --git a/packages/opentelemetry-core/src/common/NoopLogger.ts b/packages/opentelemetry-core/src/common/NoopLogger.ts deleted file mode 100644 index b3db42c994..0000000000 --- a/packages/opentelemetry-core/src/common/NoopLogger.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { Logger } from '@opentelemetry/api'; - -/** No-op implementation of Logger */ -export class NoopLogger implements Logger { - // By default does nothing - debug(_message: string, ..._args: unknown[]) {} - - // By default does nothing - error(_message: string, ..._args: unknown[]) {} - - // By default does nothing - warn(_message: string, ..._args: unknown[]) {} - - // By default does nothing - info(_message: string, ..._args: unknown[]) {} -} diff --git a/packages/opentelemetry-core/src/context/propagation/composite.ts b/packages/opentelemetry-core/src/context/propagation/composite.ts index 7048c3e7d4..27b4101ee4 100644 --- a/packages/opentelemetry-core/src/context/propagation/composite.ts +++ b/packages/opentelemetry-core/src/context/propagation/composite.ts @@ -20,8 +20,8 @@ import { TextMapPropagator, Logger, TextMapSetter, + NoopLogger, } from '@opentelemetry/api'; -import { NoopLogger } from '../../common/NoopLogger'; import { CompositePropagatorConfig } from './types'; /** Combines multiple propagators into a single propagator. */ diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index a8167a61d9..6430c84cbd 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -18,7 +18,6 @@ export * from './common/attributes'; export * from './common/ConsoleLogger'; export * from './common/global-error-handler'; export * from './common/logging-error-handler'; -export * from './common/NoopLogger'; export * from './common/time'; export * from './common/types'; export * from './ExportResult'; diff --git a/packages/opentelemetry-core/test/platform/BasePlugin.test.ts b/packages/opentelemetry-core/test/platform/BasePlugin.test.ts index 912d4bc4a1..b55fde8850 100644 --- a/packages/opentelemetry-core/test/platform/BasePlugin.test.ts +++ b/packages/opentelemetry-core/test/platform/BasePlugin.test.ts @@ -14,10 +14,10 @@ * limitations under the License. */ -import { NoopTracerProvider } from '@opentelemetry/api'; +import { NoopTracerProvider, NoopLogger } from '@opentelemetry/api'; import * as assert from 'assert'; import * as path from 'path'; -import { BasePlugin, NoopLogger } from '../../src'; +import { BasePlugin } from '../../src'; import * as types from '../trace/fixtures/test-package/foo/bar/internal'; const provider = new NoopTracerProvider(); diff --git a/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts b/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts index 202d74e350..919acbba1b 100644 --- a/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts +++ b/packages/opentelemetry-core/test/platform/browser/BasePlugin.test.ts @@ -14,9 +14,13 @@ * limitations under the License. */ -import { NOOP_TRACER, NoopTracerProvider } from '@opentelemetry/api'; +import { + NOOP_TRACER, + NoopTracerProvider, + NoopLogger, +} from '@opentelemetry/api'; import * as assert from 'assert'; -import { BasePlugin, NoopLogger } from '../../../src'; +import { BasePlugin } from '../../../src'; const provider = new NoopTracerProvider(); const logger = new NoopLogger(); diff --git a/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts index 572947bd12..2547dfeac1 100644 --- a/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector-proto/test/CollectorMetricExporter.test.ts @@ -20,7 +20,6 @@ import { } from '@opentelemetry/exporter-collector'; import * as api from '@opentelemetry/api'; import * as metrics from '@opentelemetry/metrics'; -import * as core from '@opentelemetry/core'; import * as http from 'http'; import * as assert from 'assert'; import * as sinon from 'sinon'; @@ -64,7 +63,7 @@ describe('CollectorMetricExporter - node with proto over http', () => { foo: 'bar', }, hostname: 'foo', - logger: new core.NoopLogger(), + logger: new api.NoopLogger(), serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', diff --git a/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts index 6120493c93..e6f8367aa1 100644 --- a/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts @@ -19,7 +19,7 @@ import { CollectorExporterNodeConfigBase, } from '@opentelemetry/exporter-collector'; -import * as core from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import { ReadableSpan } from '@opentelemetry/tracing'; import * as http from 'http'; import * as assert from 'assert'; @@ -59,7 +59,7 @@ describe('CollectorTraceExporter - node with proto over http', () => { foo: 'bar', }, hostname: 'foo', - logger: new core.NoopLogger(), + logger: new NoopLogger(), serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', diff --git a/packages/opentelemetry-exporter-collector/src/CollectorExporterBase.ts b/packages/opentelemetry-exporter-collector/src/CollectorExporterBase.ts index cb4d535d94..fc75a2a806 100644 --- a/packages/opentelemetry-exporter-collector/src/CollectorExporterBase.ts +++ b/packages/opentelemetry-exporter-collector/src/CollectorExporterBase.ts @@ -14,12 +14,8 @@ * limitations under the License. */ -import { Attributes, Logger } from '@opentelemetry/api'; -import { - ExportResult, - ExportResultCode, - NoopLogger, -} from '@opentelemetry/core'; +import { Attributes, Logger, NoopLogger } from '@opentelemetry/api'; +import { ExportResult, ExportResultCode } from '@opentelemetry/core'; import { CollectorExporterError, CollectorExporterConfigBase, diff --git a/packages/opentelemetry-exporter-collector/src/util.ts b/packages/opentelemetry-exporter-collector/src/util.ts index 1cb1b18aae..739698f0e0 100644 --- a/packages/opentelemetry-exporter-collector/src/util.ts +++ b/packages/opentelemetry-exporter-collector/src/util.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { Logger } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { Logger, NoopLogger } from '@opentelemetry/api'; /** * Parses headers from config leaving only those that have defined values diff --git a/packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts index 934dc72c15..9a8d39491d 100644 --- a/packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts @@ -15,7 +15,7 @@ */ import * as api from '@opentelemetry/api'; -import { ExportResultCode, NoopLogger } from '@opentelemetry/core'; +import { ExportResultCode } from '@opentelemetry/core'; import * as assert from 'assert'; import * as sinon from 'sinon'; import { CollectorMetricExporter } from '../../src/platform/browser/index'; @@ -85,7 +85,7 @@ describe('CollectorMetricExporter - web', () => { describe('when "sendBeacon" is available', () => { beforeEach(() => { collectorExporter = new CollectorMetricExporter({ - logger: new NoopLogger(), + logger: new api.NoopLogger(), url: 'http://foo.bar.com', serviceName: 'bar', }); @@ -196,7 +196,7 @@ describe('CollectorMetricExporter - web', () => { beforeEach(() => { (window.navigator as any).sendBeacon = false; collectorExporter = new CollectorMetricExporter({ - logger: new NoopLogger(), + logger: new api.NoopLogger(), url: 'http://foo.bar.com', serviceName: 'bar', }); @@ -330,7 +330,7 @@ describe('CollectorMetricExporter - web', () => { beforeEach(() => { collectorExporterConfig = { - logger: new NoopLogger(), + logger: new api.NoopLogger(), headers: customHeaders, }; server = sinon.fakeServer.create(); diff --git a/packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts index 776a01c04b..968b3ae8f5 100644 --- a/packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts @@ -14,7 +14,8 @@ * limitations under the License. */ -import { NoopLogger, ExportResultCode } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; +import { ExportResultCode } from '@opentelemetry/core'; import { ReadableSpan } from '@opentelemetry/tracing'; import * as assert from 'assert'; import * as sinon from 'sinon'; diff --git a/packages/opentelemetry-exporter-collector/test/common/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector/test/common/CollectorMetricExporter.test.ts index 82b8186ba6..fb1b7bb446 100644 --- a/packages/opentelemetry-exporter-collector/test/common/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/common/CollectorMetricExporter.test.ts @@ -15,7 +15,7 @@ */ import * as api from '@opentelemetry/api'; -import { ExportResultCode, NoopLogger } from '@opentelemetry/core'; +import { ExportResultCode } from '@opentelemetry/core'; import * as assert from 'assert'; import * as sinon from 'sinon'; import { CollectorExporterBase } from '../../src/CollectorExporterBase'; @@ -62,7 +62,7 @@ describe('CollectorMetricExporter - common', () => { onInitSpy = sinon.stub(CollectorMetricExporter.prototype, 'onInit'); collectorExporterConfig = { hostname: 'foo', - logger: new NoopLogger(), + logger: new api.NoopLogger(), serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', @@ -126,7 +126,7 @@ describe('CollectorMetricExporter - common', () => { }); it('should set default logger', () => { - assert.ok(collectorExporter.logger instanceof NoopLogger); + assert.ok(collectorExporter.logger instanceof api.NoopLogger); }); }); }); @@ -212,7 +212,7 @@ describe('CollectorMetricExporter - common', () => { ); collectorExporterConfig = { hostname: 'foo', - logger: new NoopLogger(), + logger: new api.NoopLogger(), serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', diff --git a/packages/opentelemetry-exporter-collector/test/common/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector/test/common/CollectorTraceExporter.test.ts index fa08f1b185..7dc89886ac 100644 --- a/packages/opentelemetry-exporter-collector/test/common/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/common/CollectorTraceExporter.test.ts @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -import { ExportResultCode, NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; +import { ExportResultCode } from '@opentelemetry/core'; import { ReadableSpan } from '@opentelemetry/tracing'; import * as assert from 'assert'; import * as sinon from 'sinon'; diff --git a/packages/opentelemetry-exporter-collector/test/common/utils.test.ts b/packages/opentelemetry-exporter-collector/test/common/utils.test.ts index b5fb8d3507..3f31225de1 100644 --- a/packages/opentelemetry-exporter-collector/test/common/utils.test.ts +++ b/packages/opentelemetry-exporter-collector/test/common/utils.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import { parseHeaders } from '../../src/util'; describe('utils', () => { diff --git a/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts index 1d65447481..42c2297682 100644 --- a/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts @@ -81,7 +81,7 @@ describe('CollectorMetricExporter - node with json over http', () => { foo: 'bar', }, hostname: 'foo', - logger: new core.NoopLogger(), + logger: new api.NoopLogger(), serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', diff --git a/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts index e9c2de8fb5..69a7b1fe57 100644 --- a/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import { NoopLogger } from '@opentelemetry/api'; import * as core from '@opentelemetry/core'; import { ReadableSpan } from '@opentelemetry/tracing'; import * as http from 'http'; @@ -71,7 +72,7 @@ describe('CollectorTraceExporter - node with json over http', () => { foo: 'bar', }, hostname: 'foo', - logger: new core.NoopLogger(), + logger: new NoopLogger(), serviceName: 'bar', attributes: {}, url: 'http://foo.bar.com', diff --git a/packages/opentelemetry-exporter-jaeger/src/jaeger.ts b/packages/opentelemetry-exporter-jaeger/src/jaeger.ts index f9bac2207a..8ae8890b63 100644 --- a/packages/opentelemetry-exporter-jaeger/src/jaeger.ts +++ b/packages/opentelemetry-exporter-jaeger/src/jaeger.ts @@ -15,11 +15,7 @@ */ import * as api from '@opentelemetry/api'; -import { - ExportResult, - ExportResultCode, - NoopLogger, -} from '@opentelemetry/core'; +import { ExportResult, ExportResultCode } from '@opentelemetry/core'; import { ReadableSpan, SpanExporter } from '@opentelemetry/tracing'; import { Socket } from 'dgram'; import { spanToThrift } from './transform'; @@ -39,7 +35,7 @@ export class JaegerExporter implements SpanExporter { constructor(config: jaegerTypes.ExporterConfig) { const localConfig = Object.assign({}, config); - this._logger = localConfig.logger || new NoopLogger(); + this._logger = localConfig.logger || new api.NoopLogger(); const tags: jaegerTypes.Tag[] = localConfig.tags || []; this._onShutdownFlushTimeout = typeof localConfig.flushTimeout === 'number' diff --git a/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts b/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts index 35311875bb..122759e018 100644 --- a/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts +++ b/packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts @@ -16,11 +16,7 @@ import * as assert from 'assert'; import { JaegerExporter } from '../src'; -import { - ExportResult, - ExportResultCode, - NoopLogger, -} from '@opentelemetry/core'; +import { ExportResult, ExportResultCode } from '@opentelemetry/core'; import * as api from '@opentelemetry/api'; import { ThriftProcess } from '../src/types'; import { ReadableSpan } from '@opentelemetry/tracing'; @@ -48,7 +44,7 @@ describe('JaegerExporter', () => { serviceName: 'opentelemetry', host: 'remotehost', port: 8080, - logger: new NoopLogger(), + logger: new api.NoopLogger(), tags: [{ key: 'opentelemetry-exporter-jaeger', value: '0.1.0' }], }); assert.ok(typeof exporter.export === 'function'); diff --git a/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts b/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts index 9bc14117e6..d47745980c 100644 --- a/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts +++ b/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts @@ -17,7 +17,6 @@ import * as api from '@opentelemetry/api'; import { ExportResult, - NoopLogger, globalErrorHandler, ExportResultCode, } from '@opentelemetry/core'; @@ -55,7 +54,7 @@ export class PrometheusExporter implements MetricExporter { * @param callback Callback to be called after a server was started */ constructor(config: ExporterConfig = {}, callback?: () => void) { - this._logger = config.logger || new NoopLogger(); + this._logger = config.logger || new api.NoopLogger(); this._port = config.port || PrometheusExporter.DEFAULT_OPTIONS.port; this._prefix = config.prefix || PrometheusExporter.DEFAULT_OPTIONS.prefix; this._appendTimestamp = diff --git a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts index cceb8d7d19..93a8fe3006 100644 --- a/packages/opentelemetry-exporter-zipkin/src/zipkin.ts +++ b/packages/opentelemetry-exporter-zipkin/src/zipkin.ts @@ -15,11 +15,7 @@ */ import * as api from '@opentelemetry/api'; -import { - ExportResult, - ExportResultCode, - NoopLogger, -} from '@opentelemetry/core'; +import { ExportResult, ExportResultCode } from '@opentelemetry/core'; import { SpanExporter, ReadableSpan } from '@opentelemetry/tracing'; import { prepareSend } from './platform/index'; import * as zipkinTypes from './types'; @@ -46,7 +42,7 @@ export class ZipkinExporter implements SpanExporter { constructor(config: zipkinTypes.ExporterConfig = {}) { const urlStr = config.url || ZipkinExporter.DEFAULT_URL; - this._logger = config.logger || new NoopLogger(); + this._logger = config.logger || new api.NoopLogger(); this._send = prepareSend(this._logger, urlStr, config.headers); this._serviceName = config.serviceName; this._statusCodeTagName = config.statusCodeTagName || statusCodeTagName; diff --git a/packages/opentelemetry-exporter-zipkin/test/browser/zipkin.test.ts b/packages/opentelemetry-exporter-zipkin/test/browser/zipkin.test.ts index 0ed5ca6b0c..8deac84c17 100644 --- a/packages/opentelemetry-exporter-zipkin/test/browser/zipkin.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/browser/zipkin.test.ts @@ -14,8 +14,8 @@ * limitations under the License. */ +import { NoopLogger } from '@opentelemetry/api'; import { - NoopLogger, setGlobalErrorHandler, loggingErrorHandler, } from '@opentelemetry/core'; diff --git a/packages/opentelemetry-exporter-zipkin/test/common/transform.test.ts b/packages/opentelemetry-exporter-zipkin/test/common/transform.test.ts index 0c6773d227..99617f4bd1 100644 --- a/packages/opentelemetry-exporter-zipkin/test/common/transform.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/common/transform.test.ts @@ -18,7 +18,6 @@ import * as api from '@opentelemetry/api'; import { hrTimeDuration, hrTimeToMicroseconds, - NoopLogger, VERSION, } from '@opentelemetry/core'; import { Resource, TELEMETRY_SDK_RESOURCE } from '@opentelemetry/resources'; @@ -32,7 +31,7 @@ import { _toZipkinTags, } from '../../src/transform'; import * as zipkinTypes from '../../src/types'; -const logger = new NoopLogger(); +const logger = new api.NoopLogger(); const tracer = new BasicTracerProvider({ logger, }).getTracer('default'); diff --git a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts index 2d22232179..aa2e357b71 100644 --- a/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts +++ b/packages/opentelemetry-exporter-zipkin/test/node/zipkin.test.ts @@ -19,7 +19,6 @@ import * as nock from 'nock'; import { ReadableSpan } from '@opentelemetry/tracing'; import { ExportResult, - NoopLogger, hrTimeToMicroseconds, ExportResultCode, } from '@opentelemetry/core'; @@ -77,7 +76,7 @@ describe('Zipkin Exporter - node', () => { it('should construct an exporter with logger', () => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), }); assert.ok(typeof exporter.export === 'function'); assert.ok(typeof exporter.shutdown === 'function'); @@ -112,7 +111,7 @@ describe('Zipkin Exporter - node', () => { it('should skip send with empty array', () => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), }); exporter.export([], (result: ExportResult) => { @@ -188,7 +187,7 @@ describe('Zipkin Exporter - node', () => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), }); exporter.export([span1, span2], (result: ExportResult) => { @@ -244,7 +243,7 @@ describe('Zipkin Exporter - node', () => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), url: 'https://localhost:9411/api/v2/spans', }); @@ -261,7 +260,7 @@ describe('Zipkin Exporter - node', () => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), }); exporter.export([getReadableSpan()], (result: ExportResult) => { @@ -277,7 +276,7 @@ describe('Zipkin Exporter - node', () => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), }); exporter.export([getReadableSpan()], (result: ExportResult) => { @@ -293,7 +292,7 @@ describe('Zipkin Exporter - node', () => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), }); exporter.export([getReadableSpan()], (result: ExportResult) => { @@ -305,7 +304,7 @@ describe('Zipkin Exporter - node', () => { it('should return failed result after shutdown', done => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), }); exporter.shutdown(); @@ -473,7 +472,7 @@ describe('Zipkin Exporter - node', () => { const exporter = new ZipkinExporter({ serviceName: 'my-service', - logger: new NoopLogger(), + logger: new api.NoopLogger(), }); exporter.export([getReadableSpan()], (result: ExportResult) => { diff --git a/packages/opentelemetry-grpc-utils/test/grpcUtils.test.ts b/packages/opentelemetry-grpc-utils/test/grpcUtils.test.ts index c2f877455a..f53055e620 100644 --- a/packages/opentelemetry-grpc-utils/test/grpcUtils.test.ts +++ b/packages/opentelemetry-grpc-utils/test/grpcUtils.test.ts @@ -19,9 +19,11 @@ import { NoopTracerProvider, SpanKind, propagation, + NoopLogger, + getSpan, + setSpan, } from '@opentelemetry/api'; import { - NoopLogger, HttpTraceContext, BasePlugin, PluginConfig, @@ -527,8 +529,8 @@ export const runTests = ( const span = provider .getTracer('default') .startSpan('TestSpan', { kind: SpanKind.PRODUCER }); - return provider.getTracer('default').withSpan(span, async () => { - const rootSpan = provider.getTracer('default').getCurrentSpan(); + return context.with(setSpan(context.active(), span), async () => { + const rootSpan = getSpan(context.active()); if (!rootSpan) { return assert.ok(false); } @@ -623,8 +625,8 @@ export const runTests = ( const span = provider .getTracer('default') .startSpan('TestSpan', { kind: SpanKind.PRODUCER }); - return provider.getTracer('default').withSpan(span, async () => { - const rootSpan = provider.getTracer('default').getCurrentSpan(); + return context.with(setSpan(context.active(), span), async () => { + const rootSpan = getSpan(context.active()); if (!rootSpan) { return assert.ok(false); } diff --git a/packages/opentelemetry-instrumentation-http/src/http.ts b/packages/opentelemetry-instrumentation-http/src/http.ts index ffc037b26f..7e97214387 100644 --- a/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/packages/opentelemetry-instrumentation-http/src/http.ts @@ -25,6 +25,7 @@ import { SpanContext, TraceFlags, ROOT_CONTEXT, + getSpan, } from '@opentelemetry/api'; import { NoRecordingSpan } from '@opentelemetry/core'; import type * as http from 'http'; @@ -304,7 +305,7 @@ export class HttpInstrumentation extends InstrumentationBase { this._callResponseHook(span, response); } - this.tracer.bind(response); + context.bind(response); this._logger.debug('outgoingRequest on response()'); response.on('end', () => { this._logger.debug('outgoingRequest on end()'); @@ -410,7 +411,7 @@ export class HttpInstrumentation extends InstrumentationBase { spanOptions ); - return instrumentation.tracer.withSpan(span, () => { + return context.with(setSpan(context.active(), span), () => { context.bind(request); context.bind(response); @@ -559,7 +560,7 @@ export class HttpInstrumentation extends InstrumentationBase { '%s instrumentation outgoingRequest', component ); - instrumentation.tracer.bind(request); + context.bind(request); return instrumentation._traceClientRequest( component, request, @@ -580,7 +581,7 @@ export class HttpInstrumentation extends InstrumentationBase { : this._getConfig().requireParentforIncomingSpans; let span: Span; - const currentSpan = this.tracer.getCurrentSpan(); + const currentSpan = getSpan(context.active()); if (requireParent === true && currentSpan === undefined) { // TODO: Refactor this when a solution is found in diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/http-disable.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/http-disable.test.ts index 7a962a56e3..1fff7ea5a0 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/http-disable.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/http-disable.test.ts @@ -13,8 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { NoopTracerProvider, NOOP_TRACER } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { + NoopTracerProvider, + NoopLogger, + NOOP_TRACER, +} from '@opentelemetry/api'; import * as assert from 'assert'; import { HttpInstrumentation } from '../../src/http'; import { AddressInfo } from 'net'; @@ -55,7 +58,6 @@ describe('HttpInstrumentation', () => { beforeEach(() => { NOOP_TRACER.startSpan = sinon.spy(); - NOOP_TRACER.withSpan = sinon.spy(); }); afterEach(() => { @@ -79,11 +81,6 @@ describe('HttpInstrumentation', () => { (NOOP_TRACER.startSpan as sinon.SinonSpy).called, false ); - - assert.strictEqual( - (NOOP_TRACER.withSpan as sinon.SinonSpy).called, - false - ); }); }); }); diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts index 38f0a2a8b6..4cf1c175fa 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts @@ -19,9 +19,10 @@ import { propagation, Span as ISpan, SpanKind, + NoopLogger, getSpan, + setSpan, } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { NodeTracerProvider } from '@opentelemetry/node'; import { InMemorySpanExporter, @@ -321,7 +322,7 @@ describe('HttpInstrumentation', () => { doNock(hostname, testPath, 200, 'Ok'); const name = 'TestRootSpan'; const span = provider.getTracer('default').startSpan(name); - return provider.getTracer('default').withSpan(span, async () => { + return context.with(setSpan(context.active(), span), async () => { const result = await httpRequest.get( `${protocol}://${hostname}${testPath}` ); @@ -364,7 +365,7 @@ describe('HttpInstrumentation', () => { ); const name = 'TestRootSpan'; const span = provider.getTracer('default').startSpan(name); - return provider.getTracer('default').withSpan(span, async () => { + return context.with(setSpan(context.active(), span), async () => { const result = await httpRequest.get( `${protocol}://${hostname}${testPath}` ); @@ -403,7 +404,7 @@ describe('HttpInstrumentation', () => { doNock(hostname, testPath, 200, 'Ok', num); const name = 'TestRootSpan'; const span = provider.getTracer('default').startSpan(name); - await provider.getTracer('default').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { for (let i = 0; i < num; i++) { await httpRequest.get(`${protocol}://${hostname}${testPath}`); const spans = memoryExporter.getFinishedSpans(); @@ -794,7 +795,7 @@ describe('HttpInstrumentation', () => { const span = tracer.startSpan('parentSpan', { kind: SpanKind.INTERNAL, }); - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { httpRequest .get(`${protocol}://${hostname}:${serverPort}${testPath}`) .then(result => { diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts index f98c24baf6..4ea82d2765 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/http-package.test.ts @@ -14,9 +14,14 @@ * limitations under the License. */ -import { context, SpanKind, Span, propagation } from '@opentelemetry/api'; +import { + context, + SpanKind, + Span, + propagation, + NoopLogger, +} from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; -import { NoopLogger } from '@opentelemetry/core'; import { NodeTracerProvider } from '@opentelemetry/node'; import { InMemorySpanExporter, diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/https-disable.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/https-disable.test.ts index f0eb60b38f..795b304de6 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/https-disable.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/https-disable.test.ts @@ -14,8 +14,11 @@ * limitations under the License. */ -import { NoopTracerProvider, NOOP_TRACER } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { + NoopTracerProvider, + NoopLogger, + NOOP_TRACER, +} from '@opentelemetry/api'; import * as assert from 'assert'; import * as fs from 'fs'; import type { AddressInfo } from 'net'; @@ -64,7 +67,6 @@ describe('HttpsInstrumentation', () => { beforeEach(() => { NOOP_TRACER.startSpan = sinon.spy(); - NOOP_TRACER.withSpan = sinon.spy(); }); afterEach(() => { @@ -88,10 +90,6 @@ describe('HttpsInstrumentation', () => { ); assert.strictEqual(isWrapped(https.Server.prototype.emit), false); - assert.strictEqual( - (NOOP_TRACER.withSpan as sinon.SinonSpy).called, - false - ); }); }); }); diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts index e8a7a18655..7e5c6a4ef2 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts @@ -20,8 +20,9 @@ import { propagation, Span as ISpan, SpanKind, + NoopLogger, + setSpan, } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { ContextManager } from '@opentelemetry/context-base'; import { @@ -308,7 +309,7 @@ describe('HttpsInstrumentation', () => { doNock(hostname, testPath, 200, 'Ok'); const name = 'TestRootSpan'; const span = tracer.startSpan(name); - return tracer.withSpan(span, async () => { + return context.with(setSpan(context.active(), span), async () => { const result = await httpsRequest.get( `${protocol}://${hostname}${testPath}` ); @@ -351,7 +352,7 @@ describe('HttpsInstrumentation', () => { ); const name = 'TestRootSpan'; const span = tracer.startSpan(name); - return tracer.withSpan(span, async () => { + return context.with(setSpan(context.active(), span), async () => { const result = await httpsRequest.get( `${protocol}://${hostname}${testPath}` ); @@ -390,7 +391,7 @@ describe('HttpsInstrumentation', () => { doNock(hostname, testPath, 200, 'Ok', num); const name = 'TestRootSpan'; const span = tracer.startSpan(name); - await tracer.withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { for (let i = 0; i < num; i++) { await httpsRequest.get(`${protocol}://${hostname}${testPath}`); const spans = memoryExporter.getFinishedSpans(); diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts index 826792953b..6169ff1b32 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/https-package.test.ts @@ -14,9 +14,14 @@ * limitations under the License. */ -import { context, SpanKind, propagation, Span } from '@opentelemetry/api'; +import { + context, + SpanKind, + propagation, + NoopLogger, + Span, +} from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; -import { NoopLogger } from '@opentelemetry/core'; import { NodeTracerProvider } from '@opentelemetry/node'; import { InMemorySpanExporter, diff --git a/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts b/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts index 4d3396ee48..da59b4ea0a 100644 --- a/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -19,8 +19,8 @@ import { ROOT_CONTEXT, SpanKind, TraceFlags, + NoopLogger, } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { BasicTracerProvider, Span } from '@opentelemetry/tracing'; import { HttpAttribute } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; diff --git a/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts b/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts index 86df32f56e..f0ba3ec005 100644 --- a/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/integrations/http-enable.test.ts @@ -14,8 +14,13 @@ * limitations under the License. */ -import { NoopLogger } from '@opentelemetry/core'; -import { SpanKind, Span, context, propagation } from '@opentelemetry/api'; +import { + SpanKind, + Span, + context, + propagation, + NoopLogger, +} from '@opentelemetry/api'; import { HttpAttribute, GeneralAttribute, diff --git a/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts b/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts index b9d68ea8d1..880c2ec0f6 100644 --- a/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts +++ b/packages/opentelemetry-instrumentation-http/test/integrations/https-enable.test.ts @@ -14,8 +14,13 @@ * limitations under the License. */ -import { NoopLogger } from '@opentelemetry/core'; -import { SpanKind, Span, context, propagation } from '@opentelemetry/api'; +import { + SpanKind, + Span, + context, + propagation, + NoopLogger, +} from '@opentelemetry/api'; import { HttpAttribute, GeneralAttribute, diff --git a/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts b/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts index 1e5fe3e1fd..b3f96e7f43 100644 --- a/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts +++ b/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts @@ -125,7 +125,7 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase { + api.context.with(api.setSpan(api.context.active(), span), () => { const childSpan = this.tracer.startSpan('CORS Preflight', { startTime: corsPreFlightRequest[PTN.FETCH_START], }); @@ -437,25 +437,28 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase { - plugin._tasksCount++; - xhrMem.sendStartTime = hrTime(); - currentSpan.addEvent(EventNames.METHOD_SEND); - - this.addEventListener('abort', onAbort); - this.addEventListener('error', onError); - this.addEventListener('load', onLoad); - this.addEventListener('timeout', onTimeout); - - xhrMem.callbackToRemoveEvents = () => { - unregister(this); - if (xhrMem.createdResources) { - xhrMem.createdResources.observer.disconnect(); - } - }; - plugin._addHeaders(this, spanUrl); - plugin._addResourceObserver(this, spanUrl); - }); + api.context.with( + api.setSpan(api.context.active(), currentSpan), + () => { + plugin._tasksCount++; + xhrMem.sendStartTime = hrTime(); + currentSpan.addEvent(EventNames.METHOD_SEND); + + this.addEventListener('abort', onAbort); + this.addEventListener('error', onError); + this.addEventListener('load', onLoad); + this.addEventListener('timeout', onTimeout); + + xhrMem.callbackToRemoveEvents = () => { + unregister(this); + if (xhrMem.createdResources) { + xhrMem.createdResources.observer.disconnect(); + } + }; + plugin._addHeaders(this, spanUrl); + plugin._addResourceObserver(this, spanUrl); + } + ); } return original.apply(this, args); }; diff --git a/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts b/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts index cc3c4875cf..f5acb4ebf8 100644 --- a/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts +++ b/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts @@ -213,7 +213,7 @@ describe('xhr', () => { ); rootSpan = webTracerWithZone.startSpan('root'); - webTracerWithZone.withSpan(rootSpan, () => { + api.context.with(api.setSpan(api.context.active(), rootSpan), () => { getData( new XMLHttpRequest(), fileUrl, @@ -626,46 +626,52 @@ describe('xhr', () => { }) ); const reusableReq = new XMLHttpRequest(); - webTracerWithZone.withSpan(rootSpan, () => { - getData( - reusableReq, - firstUrl, - () => { - fakeNow = 100; - }, - testAsync - ).then(() => { - fakeNow = 0; - sandbox.clock.tick(1000); - }); - }); - - webTracerWithZone.withSpan(rootSpan, () => { - getData( - reusableReq, - secondUrl, - () => { - fakeNow = 100; - }, - testAsync - ).then(() => { - fakeNow = 0; - sandbox.clock.tick(1000); - done(); - }); - - assert.strictEqual( - requests.length, - 1, - 'first request not called' - ); + api.context.with( + api.setSpan(api.context.active(), rootSpan), + () => { + getData( + reusableReq, + firstUrl, + () => { + fakeNow = 100; + }, + testAsync + ).then(() => { + fakeNow = 0; + sandbox.clock.tick(1000); + }); + } + ); - requests[0].respond( - 200, - { 'Content-Type': 'application/json' }, - '{"foo":"bar"}' - ); - }); + api.context.with( + api.setSpan(api.context.active(), rootSpan), + () => { + getData( + reusableReq, + secondUrl, + () => { + fakeNow = 100; + }, + testAsync + ).then(() => { + fakeNow = 0; + sandbox.clock.tick(1000); + done(); + }); + + assert.strictEqual( + requests.length, + 1, + 'first request not called' + ); + + requests[0].respond( + 200, + { 'Content-Type': 'application/json' }, + '{"foo":"bar"}' + ); + } + ); }); it('should clear previous span information', () => { @@ -738,26 +744,29 @@ describe('xhr', () => { describe('when request loads and receives an error code', () => { beforeEach(done => { - webTracerWithZone.withSpan(rootSpan, () => { - getData( - new XMLHttpRequest(), - url, - () => { - fakeNow = 100; - }, - testAsync - ).then(() => { - fakeNow = 0; - sandbox.clock.tick(1000); - done(); - }); - assert.strictEqual(requests.length, 1, 'request not called'); - requests[0].respond( - 400, - { 'Content-Type': 'text/plain' }, - 'Bad Request' - ); - }); + api.context.with( + api.setSpan(api.context.active(), rootSpan), + () => { + getData( + new XMLHttpRequest(), + url, + () => { + fakeNow = 100; + }, + testAsync + ).then(() => { + fakeNow = 0; + sandbox.clock.tick(1000); + done(); + }); + assert.strictEqual(requests.length, 1, 'request not called'); + requests[0].respond( + 400, + { 'Content-Type': 'text/plain' }, + 'Bad Request' + ); + } + ); }); it('span should have correct attributes', () => { const span: tracing.ReadableSpan = exportSpy.args[0][0][0]; @@ -872,18 +881,21 @@ describe('xhr', () => { describe('when request encounters a network error', () => { beforeEach(done => { - webTracerWithZone.withSpan(rootSpan, () => { - getData(new XMLHttpRequest(), url, () => {}, testAsync).then( - () => { - fakeNow = 0; - sandbox.clock.tick(1000); - done(); - } - ); - - assert.strictEqual(requests.length, 1, 'request not called'); - requests[0].error(); - }); + api.context.with( + api.setSpan(api.context.active(), rootSpan), + () => { + getData(new XMLHttpRequest(), url, () => {}, testAsync).then( + () => { + fakeNow = 0; + sandbox.clock.tick(1000); + done(); + } + ); + + assert.strictEqual(requests.length, 1, 'request not called'); + requests[0].error(); + } + ); }); it('span should have correct attributes', () => { @@ -961,18 +973,21 @@ describe('xhr', () => { }); beforeEach(done => { - webTracerWithZone.withSpan(rootSpan, () => { - getData(new XMLHttpRequest(), url, () => {}, testAsync).then( - () => { - fakeNow = 0; - sandbox.clock.tick(1000); - done(); - } - ); - - assert.strictEqual(requests.length, 1, 'request not called'); - requests[0].abort(); - }); + api.context.with( + api.setSpan(api.context.active(), rootSpan), + () => { + getData(new XMLHttpRequest(), url, () => {}, testAsync).then( + () => { + fakeNow = 0; + sandbox.clock.tick(1000); + done(); + } + ); + + assert.strictEqual(requests.length, 1, 'request not called'); + requests[0].abort(); + } + ); }); it('span should have correct attributes', () => { @@ -1050,20 +1065,23 @@ describe('xhr', () => { }); beforeEach(done => { - webTracerWithZone.withSpan(rootSpan, () => { - getData( - new XMLHttpRequest(), - url, - () => { - sandbox.clock.tick(XHR_TIMEOUT); - }, - testAsync - ).then(() => { - fakeNow = 0; - sandbox.clock.tick(1000); - done(); - }); - }); + api.context.with( + api.setSpan(api.context.active(), rootSpan), + () => { + getData( + new XMLHttpRequest(), + url, + () => { + sandbox.clock.tick(XHR_TIMEOUT); + }, + testAsync + ).then(() => { + fakeNow = 0; + sandbox.clock.tick(1000); + done(); + }); + } + ); }); it('span should have correct attributes', () => { diff --git a/packages/opentelemetry-metrics/src/Metric.ts b/packages/opentelemetry-metrics/src/Metric.ts index e492115cbe..525c166d76 100644 --- a/packages/opentelemetry-metrics/src/Metric.ts +++ b/packages/opentelemetry-metrics/src/Metric.ts @@ -14,7 +14,6 @@ * limitations under the License. */ import * as api from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { Resource } from '@opentelemetry/resources'; import { BaseBoundInstrument } from './BoundInstrument'; import { MetricDescriptor, MetricKind, MetricRecord } from './export/types'; @@ -43,7 +42,7 @@ export abstract class Metric typeof _options.valueType === 'number' ? _options.valueType : api.ValueType.DOUBLE; - this._logger = _options.logger ?? new NoopLogger(); + this._logger = _options.logger ?? new api.NoopLogger(); this._boundaries = _options.boundaries; this._descriptor = this._getMetricDescriptor(); } diff --git a/packages/opentelemetry-metrics/test/Meter.test.ts b/packages/opentelemetry-metrics/test/Meter.test.ts index c03b8ab369..2a9562cd87 100644 --- a/packages/opentelemetry-metrics/test/Meter.test.ts +++ b/packages/opentelemetry-metrics/test/Meter.test.ts @@ -34,7 +34,7 @@ import { Histogram, } from '../src'; import * as api from '@opentelemetry/api'; -import { NoopLogger, hrTime, hrTimeToNanoseconds } from '@opentelemetry/core'; +import { hrTime, hrTimeToNanoseconds } from '@opentelemetry/core'; import { BatchObserverResult } from '../src/BatchObserverResult'; import { SumAggregator } from '../src/export/aggregators'; import { SumObserverMetric } from '../src/SumObserverMetric'; @@ -81,7 +81,7 @@ describe('Meter', () => { beforeEach(() => { meter = new MeterProvider({ - logger: new NoopLogger(), + logger: new api.NoopLogger(), }).getMeter('test-meter'); }); diff --git a/packages/opentelemetry-metrics/test/MeterProvider.test.ts b/packages/opentelemetry-metrics/test/MeterProvider.test.ts index ace5b0b472..f92af4458c 100644 --- a/packages/opentelemetry-metrics/test/MeterProvider.test.ts +++ b/packages/opentelemetry-metrics/test/MeterProvider.test.ts @@ -17,7 +17,7 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import { MeterProvider, Meter, CounterMetric } from '../src'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; describe('MeterProvider', () => { describe('constructor', () => { diff --git a/packages/opentelemetry-metrics/test/Processor.test.ts b/packages/opentelemetry-metrics/test/Processor.test.ts index f7d1ce9c11..d14bd4fe5a 100644 --- a/packages/opentelemetry-metrics/test/Processor.test.ts +++ b/packages/opentelemetry-metrics/test/Processor.test.ts @@ -16,7 +16,6 @@ import * as assert from 'assert'; import * as api from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { Meter, MeterProvider } from '../src'; describe('Processor', () => { @@ -27,7 +26,7 @@ describe('Processor', () => { let counter: api.Counter; beforeEach(() => { meter = new MeterProvider({ - logger: new NoopLogger(), + logger: new api.NoopLogger(), interval: 10000, }).getMeter('test-meter'); counter = meter.createCounter('ungrouped-processor-test'); diff --git a/packages/opentelemetry-node/test/NodeTracerProvider.test.ts b/packages/opentelemetry-node/test/NodeTracerProvider.test.ts index c5de5a4a68..851091b0ac 100644 --- a/packages/opentelemetry-node/test/NodeTracerProvider.test.ts +++ b/packages/opentelemetry-node/test/NodeTracerProvider.test.ts @@ -17,13 +17,14 @@ import { context, TraceFlags, + NoopLogger, setSpan, setSpanContext, + getSpan, } from '@opentelemetry/api'; import { AlwaysOnSampler, AlwaysOffSampler, - NoopLogger, NoRecordingSpan, } from '@opentelemetry/core'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; @@ -203,49 +204,27 @@ describe('NodeTracerProvider', () => { }); }); - describe('.getCurrentSpan()', () => { - it('should return undefined with AsyncHooksContextManager when no span started', () => { - provider = new NodeTracerProvider({}); - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - undefined - ); - }); - }); - describe('.withSpan()', () => { it('should run context with AsyncHooksContextManager context manager', done => { provider = new NodeTracerProvider({}); const span = provider.getTracer('default').startSpan('my-span'); - provider.getTracer('default').withSpan(span, () => { - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - span - ); + context.with(setSpan(context.active(), span), () => { + assert.deepStrictEqual(getSpan(context.active()), span); return done(); }); - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - undefined - ); + assert.deepStrictEqual(getSpan(context.active()), undefined); }); it('should run context with AsyncHooksContextManager context manager with multiple spans', done => { provider = new NodeTracerProvider({}); const span = provider.getTracer('default').startSpan('my-span'); - provider.getTracer('default').withSpan(span, () => { - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - span - ); + context.with(setSpan(context.active(), span), () => { + assert.deepStrictEqual(getSpan(context.active()), span); const span1 = provider.getTracer('default').startSpan('my-span1'); - provider.getTracer('default').withSpan(span1, () => { - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - span1 - ); + context.with(setSpan(context.active(), span1), () => { + assert.deepStrictEqual(getSpan(context.active()), span1); assert.deepStrictEqual( span1.context().traceId, span.context().traceId @@ -255,29 +234,20 @@ describe('NodeTracerProvider', () => { }); // when span ended. // @todo: below check is not running. - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - undefined - ); + assert.deepStrictEqual(getSpan(context.active()), undefined); }); it('should find correct context with promises', async () => { provider = new NodeTracerProvider(); const span = provider.getTracer('default').startSpan('my-span'); - await provider.getTracer('default').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { for (let i = 0; i < 3; i++) { await sleep(5).then(() => { - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - span - ); + assert.deepStrictEqual(getSpan(context.active()), span); }); } }); - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - undefined - ); + assert.deepStrictEqual(getSpan(context.active()), undefined); }); }); @@ -286,10 +256,7 @@ describe('NodeTracerProvider', () => { const provider = new NodeTracerProvider({}); const span = provider.getTracer('default').startSpan('my-span'); const fn = () => { - assert.deepStrictEqual( - provider.getTracer('default').getCurrentSpan(), - span - ); + assert.deepStrictEqual(getSpan(context.active()), span); return done(); }; const patchedFn = context.bind(fn, setSpan(context.active(), span)); diff --git a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts index 954cf7f8ed..8594b1c8b4 100644 --- a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts +++ b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { NoopTracerProvider } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopTracerProvider, NoopLogger } from '@opentelemetry/api'; import * as assert from 'assert'; import * as path from 'path'; import { diff --git a/packages/opentelemetry-node/test/instrumentation/utils.test.ts b/packages/opentelemetry-node/test/instrumentation/utils.test.ts index 0f2eb4f7ac..06ccdd12e0 100644 --- a/packages/opentelemetry-node/test/instrumentation/utils.test.ts +++ b/packages/opentelemetry-node/test/instrumentation/utils.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import * as assert from 'assert'; import * as path from 'path'; import * as utils from '../../src/instrumentation/utils'; diff --git a/packages/opentelemetry-plugin-fetch/src/fetch.ts b/packages/opentelemetry-plugin-fetch/src/fetch.ts index d366c9a356..419a80cb40 100644 --- a/packages/opentelemetry-plugin-fetch/src/fetch.ts +++ b/packages/opentelemetry-plugin-fetch/src/fetch.ts @@ -299,16 +299,19 @@ export class FetchPlugin extends core.BasePlugin> { } return new Promise((resolve, reject) => { - return plugin._tracer.withSpan(span, () => { - plugin._addHeaders(options, url); - plugin._tasksCount++; - return original - .apply(this, [url, options]) - .then( - onSuccess.bind(this, span, resolve), - onError.bind(this, span, reject) - ); - }); + return api.context.with( + api.setSpan(api.context.active(), span), + () => { + plugin._addHeaders(options, url); + plugin._tasksCount++; + return original + .apply(this, [url, options]) + .then( + onSuccess.bind(this, span, resolve), + onError.bind(this, span, reject) + ); + } + ); }); }; }; diff --git a/packages/opentelemetry-plugin-fetch/test/fetch.test.ts b/packages/opentelemetry-plugin-fetch/test/fetch.test.ts index 647537f6c3..80c3000b1f 100644 --- a/packages/opentelemetry-plugin-fetch/test/fetch.test.ts +++ b/packages/opentelemetry-plugin-fetch/test/fetch.test.ts @@ -178,7 +178,7 @@ describe('fetch', () => { ); rootSpan = webTracerWithZone.startSpan('root'); - webTracerWithZone.withSpan(rootSpan, () => { + api.context.with(api.setSpan(api.context.active(), rootSpan), () => { fakeNow = 0; getData(fileUrl, method).then( response => { diff --git a/packages/opentelemetry-plugin-grpc-js/src/client/utils.ts b/packages/opentelemetry-plugin-grpc-js/src/client/utils.ts index 9332f7ad76..70632b6ac8 100644 --- a/packages/opentelemetry-plugin-grpc-js/src/client/utils.ts +++ b/packages/opentelemetry-plugin-grpc-js/src/client/utils.ts @@ -23,6 +23,7 @@ import { Status, propagation, context, + setSpan, } from '@opentelemetry/api'; import { RpcAttribute } from '@opentelemetry/semantic-conventions'; import type * as grpcJs from '@grpc/grpc-js'; @@ -80,8 +81,8 @@ export function getPatchedClientMethods( const span = plugin.tracer.startSpan(name, { kind: SpanKind.CLIENT, }); - return plugin.tracer.withSpan(span, () => - makeGrpcClientRemoteCall(original, args, metadata, this, plugin)(span) + return context.with(setSpan(context.active(), span), () => + makeGrpcClientRemoteCall(original, args, metadata, this)(span) ); }; }; @@ -95,8 +96,7 @@ export function makeGrpcClientRemoteCall( original: GrpcClientFunc, args: unknown[], metadata: grpcJs.Metadata, - self: grpcJs.Client, - plugin: GrpcJsPlugin + self: grpcJs.Client ): (span: Span) => EventEmitter { /** * Patches a callback so that the current span for this trace is also ended @@ -130,7 +130,7 @@ export function makeGrpcClientRemoteCall( span.end(); callback(err, res); }; - return plugin.tracer.bind(wrappedFn); + return context.bind(wrappedFn); } return (span: Span) => { @@ -166,7 +166,7 @@ export function makeGrpcClientRemoteCall( spanEnded = true; } }; - plugin.tracer.bind(call); + context.bind(call); call.on('error', (err: grpcJs.ServiceError) => { if (call[CALL_SPAN_ENDED]) { return; diff --git a/packages/opentelemetry-plugin-grpc-js/src/server/clientStreamAndUnary.ts b/packages/opentelemetry-plugin-grpc-js/src/server/clientStreamAndUnary.ts index 2e92334cb0..bef98b2590 100644 --- a/packages/opentelemetry-plugin-grpc-js/src/server/clientStreamAndUnary.ts +++ b/packages/opentelemetry-plugin-grpc-js/src/server/clientStreamAndUnary.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Span, StatusCode } from '@opentelemetry/api'; +import { context, Span, StatusCode } from '@opentelemetry/api'; import type { ServerCallWithMeta, SendUnaryDataCallback } from '../types'; import { grpcStatusCodeToOpenTelemetryStatusCode } from '../utils'; import { RpcAttribute } from '@opentelemetry/semantic-conventions'; @@ -61,6 +61,6 @@ export function clientStreamAndUnaryHandler( return callback(err, value); }; - plugin.tracer.bind(call); + context.bind(call); return (original as Function).call({}, call, patchedCallback); } diff --git a/packages/opentelemetry-plugin-grpc-js/src/server/patchServer.ts b/packages/opentelemetry-plugin-grpc-js/src/server/patchServer.ts index e9e8ed6f29..efad948e9c 100644 --- a/packages/opentelemetry-plugin-grpc-js/src/server/patchServer.ts +++ b/packages/opentelemetry-plugin-grpc-js/src/server/patchServer.ts @@ -30,6 +30,7 @@ import { propagation, Span, ROOT_CONTEXT, + setSpan, } from '@opentelemetry/api'; import { RpcAttribute } from '@opentelemetry/semantic-conventions'; import { clientStreamAndUnaryHandler } from './clientStreamAndUnary'; @@ -113,7 +114,7 @@ export function patchServer( [RpcAttribute.GRPC_KIND]: spanOptions.kind, }); - plugin.tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { handleServerFunction.call( self, plugin, diff --git a/packages/opentelemetry-plugin-grpc-js/src/server/serverStreamAndBidi.ts b/packages/opentelemetry-plugin-grpc-js/src/server/serverStreamAndBidi.ts index 138d98e9c9..311097996e 100644 --- a/packages/opentelemetry-plugin-grpc-js/src/server/serverStreamAndBidi.ts +++ b/packages/opentelemetry-plugin-grpc-js/src/server/serverStreamAndBidi.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Span, StatusCode } from '@opentelemetry/api'; +import { context, Span, StatusCode } from '@opentelemetry/api'; import { RpcAttribute } from '@opentelemetry/semantic-conventions'; import type * as grpcJs from '@grpc/grpc-js'; import type { GrpcJsPlugin } from '../grpcJs'; @@ -43,7 +43,7 @@ export function serverStreamAndBidiHandler( } }; - plugin.tracer.bind(call); + context.bind(call); call.on('finish', () => { // @grpc/js does not expose a way to check if this call also emitted an error, // e.g. call.status.code !== 0 diff --git a/packages/opentelemetry-plugin-grpc/src/grpc.ts b/packages/opentelemetry-plugin-grpc/src/grpc.ts index f27492438e..a53941c9d9 100644 --- a/packages/opentelemetry-plugin-grpc/src/grpc.ts +++ b/packages/opentelemetry-plugin-grpc/src/grpc.ts @@ -23,6 +23,7 @@ import { SpanOptions, Status, ROOT_CONTEXT, + setSpan, } from '@opentelemetry/api'; import { RpcAttribute } from '@opentelemetry/semantic-conventions'; import { BasePlugin } from '@opentelemetry/core'; @@ -194,7 +195,7 @@ export class GrpcPlugin extends BasePlugin { [RpcAttribute.GRPC_KIND]: spanOptions.kind, }); - plugin._tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { switch (type) { case 'unary': case 'client_stream': @@ -286,7 +287,7 @@ export class GrpcPlugin extends BasePlugin { return callback(err, value, trailer, flags); } - plugin._tracer.bind(call); + context.bind(call); return (original as Function).call(self, call, patchedCallback); } @@ -305,7 +306,7 @@ export class GrpcPlugin extends BasePlugin { } }; - plugin._tracer.bind(call); + context.bind(call); call.on('finish', () => { span.setStatus(_grpcStatusCodeToSpanStatus(call.status.code)); span.setAttribute( @@ -392,7 +393,7 @@ export class GrpcPlugin extends BasePlugin { const span = plugin._tracer.startSpan(name, { kind: SpanKind.CLIENT, }); - return plugin._tracer.withSpan(span, () => + return context.with(setSpan(context.active(), span), () => plugin._makeGrpcClientRemoteCall( original, args, @@ -448,7 +449,7 @@ export class GrpcPlugin extends BasePlugin { span.end(); callback(err, res); }; - return plugin._tracer.bind(wrappedFn); + return context.bind(wrappedFn); } return (span: Span) => { @@ -490,7 +491,7 @@ export class GrpcPlugin extends BasePlugin { spanEnded = true; } }; - plugin._tracer.bind(call); + context.bind(call); ((call as unknown) as events.EventEmitter).on( 'error', (err: grpcTypes.ServiceError) => { diff --git a/packages/opentelemetry-plugin-http/src/http.ts b/packages/opentelemetry-plugin-http/src/http.ts index a88ce6e348..c4eecfb3b1 100644 --- a/packages/opentelemetry-plugin-http/src/http.ts +++ b/packages/opentelemetry-plugin-http/src/http.ts @@ -25,6 +25,7 @@ import { TraceFlags, setSpan, ROOT_CONTEXT, + getSpan, } from '@opentelemetry/api'; import { BasePlugin, NoRecordingSpan } from '@opentelemetry/core'; import type { @@ -214,7 +215,7 @@ export class HttpPlugin extends BasePlugin { this._callResponseHook(span, response); } - this._tracer.bind(response); + context.bind(response); this._logger.debug('outgoingRequest on response()'); response.on('end', () => { this._logger.debug('outgoingRequest on end()'); @@ -310,7 +311,7 @@ export class HttpPlugin extends BasePlugin { return context.with(propagation.extract(ROOT_CONTEXT, headers), () => { const span = plugin._startHttpSpan(`HTTP ${method}`, spanOptions); - return plugin._tracer.withSpan(span, () => { + return context.with(setSpan(context.active(), span), () => { context.bind(request); context.bind(response); @@ -426,7 +427,7 @@ export class HttpPlugin extends BasePlugin { ); plugin._logger.debug('%s plugin outgoingRequest', plugin.moduleName); - plugin._tracer.bind(request); + context.bind(request); return plugin._traceClientRequest(request, optionsParsed, span); }; } @@ -442,7 +443,7 @@ export class HttpPlugin extends BasePlugin { : this._config.requireParentforIncomingSpans; let span: Span; - const currentSpan = this._tracer.getCurrentSpan(); + const currentSpan = getSpan(context.active()); if (requireParent === true && currentSpan === undefined) { // TODO: Refactor this when a solution is found in diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-disable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-disable.test.ts index 3b80fe84ba..9ed917b509 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-disable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-disable.test.ts @@ -13,8 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { NoopTracerProvider, NOOP_TRACER } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; +import { + NoopTracerProvider, + NoopLogger, + NOOP_TRACER, +} from '@opentelemetry/api'; import * as assert from 'assert'; import * as http from 'http'; import { AddressInfo } from 'net'; @@ -49,7 +52,6 @@ describe('HttpPlugin', () => { beforeEach(() => { NOOP_TRACER.startSpan = sinon.spy(); - NOOP_TRACER.withSpan = sinon.spy(); }); afterEach(() => { @@ -73,10 +75,6 @@ describe('HttpPlugin', () => { ); assert.strictEqual(http.Server.prototype.emit.__wrapped, undefined); - assert.strictEqual( - (NOOP_TRACER.withSpan as sinon.SinonSpy).called, - false - ); }); }); }); diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts index 52ed5a4bc3..b34816f76a 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts @@ -19,9 +19,10 @@ import { propagation, Span as ISpan, SpanKind, + NoopLogger, getSpan, + setSpan, } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { NodeTracerProvider } from '@opentelemetry/node'; import { InMemorySpanExporter, @@ -338,7 +339,7 @@ describe('HttpPlugin', () => { doNock(hostname, testPath, 200, 'Ok'); const name = 'TestRootSpan'; const span = provider.getTracer('default').startSpan(name); - return provider.getTracer('default').withSpan(span, async () => { + return context.with(setSpan(context.active(), span), async () => { const result = await httpRequest.get( `${protocol}://${hostname}${testPath}` ); @@ -381,7 +382,7 @@ describe('HttpPlugin', () => { ); const name = 'TestRootSpan'; const span = provider.getTracer('default').startSpan(name); - return provider.getTracer('default').withSpan(span, async () => { + return context.with(setSpan(context.active(), span), async () => { const result = await httpRequest.get( `${protocol}://${hostname}${testPath}` ); @@ -420,7 +421,7 @@ describe('HttpPlugin', () => { doNock(hostname, testPath, 200, 'Ok', num); const name = 'TestRootSpan'; const span = provider.getTracer('default').startSpan(name); - await provider.getTracer('default').withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { for (let i = 0; i < num; i++) { await httpRequest.get(`${protocol}://${hostname}${testPath}`); const spans = memoryExporter.getFinishedSpans(); @@ -810,7 +811,7 @@ describe('HttpPlugin', () => { const span = tracer.startSpan('parentSpan', { kind: SpanKind.INTERNAL, }); - tracer.withSpan(span, () => { + context.with(setSpan(context.active(), span), () => { httpRequest .get(`${protocol}://${hostname}:${serverPort}${testPath}`) .then(result => { diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts index e1d3cb72a4..31bb06f48a 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-package.test.ts @@ -14,9 +14,8 @@ * limitations under the License. */ -import { context, SpanKind } from '@opentelemetry/api'; +import { context, SpanKind, NoopLogger } from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; -import { NoopLogger } from '@opentelemetry/core'; import { NodeTracerProvider } from '@opentelemetry/node'; import { InMemorySpanExporter, diff --git a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts index 9bfd6126ed..c145413e9a 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts @@ -19,8 +19,8 @@ import { ROOT_CONTEXT, SpanKind, TraceFlags, + NoopLogger, } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { BasicTracerProvider, Span } from '@opentelemetry/tracing'; import { HttpAttribute } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; diff --git a/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts index 8ea38fea17..383a4b3b45 100644 --- a/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/integrations/http-enable.test.ts @@ -14,8 +14,7 @@ * limitations under the License. */ -import { NoopLogger } from '@opentelemetry/core'; -import { SpanKind, Span, context } from '@opentelemetry/api'; +import { SpanKind, Span, context, NoopLogger } from '@opentelemetry/api'; import { HttpAttribute, GeneralAttribute, diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-disable.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-disable.test.ts index 9d0e8dece6..01b9b71900 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-disable.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-disable.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import { Http } from '@opentelemetry/plugin-http'; import * as assert from 'assert'; import * as fs from 'fs'; @@ -64,7 +64,6 @@ describe('HttpsPlugin', () => { beforeEach(() => { tracer.startSpan = sinon.spy(); - tracer.withSpan = sinon.spy(); }); afterEach(() => { @@ -88,7 +87,6 @@ describe('HttpsPlugin', () => { ); assert.strictEqual(https.Server.prototype.emit.__wrapped, undefined); - assert.strictEqual((tracer.withSpan as sinon.SinonSpy).called, false); }); }); }); diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts index 10cc3aeb53..4123a046a6 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-enable.test.ts @@ -20,8 +20,9 @@ import { propagation, Span as ISpan, SpanKind, + NoopLogger, + setSpan, } from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import { NodeTracerProvider } from '@opentelemetry/node'; import { Http, HttpPluginConfig } from '@opentelemetry/plugin-http'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; @@ -335,7 +336,7 @@ describe('HttpsPlugin', () => { doNock(hostname, testPath, 200, 'Ok'); const name = 'TestRootSpan'; const span = tracer.startSpan(name); - return tracer.withSpan(span, async () => { + return context.with(setSpan(context.active(), span), async () => { const result = await httpsRequest.get( `${protocol}://${hostname}${testPath}` ); @@ -378,7 +379,7 @@ describe('HttpsPlugin', () => { ); const name = 'TestRootSpan'; const span = tracer.startSpan(name); - return tracer.withSpan(span, async () => { + return context.with(setSpan(context.active(), span), async () => { const result = await httpsRequest.get( `${protocol}://${hostname}${testPath}` ); @@ -417,7 +418,7 @@ describe('HttpsPlugin', () => { doNock(hostname, testPath, 200, 'Ok', num); const name = 'TestRootSpan'; const span = tracer.startSpan(name); - await tracer.withSpan(span, async () => { + await context.with(setSpan(context.active(), span), async () => { for (let i = 0; i < num; i++) { await httpsRequest.get(`${protocol}://${hostname}${testPath}`); const spans = memoryExporter.getFinishedSpans(); diff --git a/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts b/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts index ff7430afb3..daf3ac7e9e 100644 --- a/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts +++ b/packages/opentelemetry-plugin-https/test/functionals/https-package.test.ts @@ -14,9 +14,8 @@ * limitations under the License. */ -import { context, Span, SpanKind } from '@opentelemetry/api'; +import { context, Span, SpanKind, NoopLogger } from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; -import { NoopLogger } from '@opentelemetry/core'; import { NodeTracerProvider } from '@opentelemetry/node'; import { Http } from '@opentelemetry/plugin-http'; import { diff --git a/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts b/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts index ef291281f5..3fc48cdcad 100644 --- a/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts +++ b/packages/opentelemetry-plugin-https/test/integrations/https-enable.test.ts @@ -14,9 +14,8 @@ * limitations under the License. */ -import { NoopLogger } from '@opentelemetry/core'; import { HttpPluginConfig, Http } from '@opentelemetry/plugin-http'; -import { SpanKind, Span, context } from '@opentelemetry/api'; +import { SpanKind, Span, context, NoopLogger } from '@opentelemetry/api'; import { HttpAttribute, GeneralAttribute, diff --git a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts index 9ccebd7066..f132a8015c 100644 --- a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts +++ b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsBeanstalkDetector.test.ts @@ -21,7 +21,7 @@ import { assertEmptyResource, assertServiceResource, } from '@opentelemetry/resources/test/util/resource-assertions'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; describe('BeanstalkResourceDetector', () => { const err = new Error('failed to read config file'); diff --git a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEc2Detector.test.ts b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEc2Detector.test.ts index 99461164fd..340328551b 100644 --- a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEc2Detector.test.ts +++ b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEc2Detector.test.ts @@ -22,7 +22,7 @@ import { assertCloudResource, assertHostResource, } from '@opentelemetry/resources/test/util/resource-assertions'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; const AWS_HOST = 'http://' + awsEc2Detector.AWS_IDMS_ENDPOINT; const AWS_TOKEN_PATH = awsEc2Detector.AWS_INSTANCE_TOKEN_DOCUMENT_PATH; diff --git a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEcsDetector.test.ts b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEcsDetector.test.ts index e8bed2b3e5..857e40603d 100644 --- a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEcsDetector.test.ts +++ b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEcsDetector.test.ts @@ -24,7 +24,7 @@ import { assertEmptyResource, assertContainerResource, } from '@opentelemetry/resources/test/util/resource-assertions'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import * as os from 'os'; describe('BeanstalkResourceDetector', () => { diff --git a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEksDetector.test.ts b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEksDetector.test.ts index ce350e2084..7356240bc3 100644 --- a/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEksDetector.test.ts +++ b/packages/opentelemetry-resource-detector-aws/test/detectors/AwsEksDetector.test.ts @@ -24,7 +24,7 @@ import { assertContainerResource, assertEmptyResource, } from '@opentelemetry/resources/test/util/resource-assertions'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; const K8S_SVC_URL = awsEksDetector.K8S_SVC_URL; const AUTH_CONFIGMAP_PATH = awsEksDetector.AUTH_CONFIGMAP_PATH; diff --git a/packages/opentelemetry-resource-detector-gcp/package.json b/packages/opentelemetry-resource-detector-gcp/package.json index f954cfa376..7679978937 100644 --- a/packages/opentelemetry-resource-detector-gcp/package.json +++ b/packages/opentelemetry-resource-detector-gcp/package.json @@ -54,6 +54,7 @@ "typescript": "3.9.7" }, "dependencies": { + "@opentelemetry/api": "^0.13.0", "@opentelemetry/resources": "^0.14.0", "gcp-metadata": "^4.1.4", "semver": "7.3.4" diff --git a/packages/opentelemetry-resource-detector-gcp/test/detectors/GcpDetector.test.ts b/packages/opentelemetry-resource-detector-gcp/test/detectors/GcpDetector.test.ts index 4d227919f2..f64e684d08 100644 --- a/packages/opentelemetry-resource-detector-gcp/test/detectors/GcpDetector.test.ts +++ b/packages/opentelemetry-resource-detector-gcp/test/detectors/GcpDetector.test.ts @@ -32,7 +32,7 @@ import { assertContainerResource, assertEmptyResource, } from '@opentelemetry/resources/test/util/resource-assertions'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; import { Resource } from '@opentelemetry/resources'; const HEADERS = { diff --git a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts index 37fa35278d..cb29dc7c40 100644 --- a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts +++ b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts @@ -19,9 +19,8 @@ import { ResourceDetectionConfig, ResourceDetectionConfigWithLogger, } from '../../config'; -import { Logger } from '@opentelemetry/api'; +import { Logger, NoopLogger } from '@opentelemetry/api'; import * as util from 'util'; -import { NoopLogger } from '@opentelemetry/core'; /** * Runs all resource detectors and returns the results merged into a single diff --git a/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts b/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts index df45725cb9..c2056d0031 100644 --- a/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts @@ -19,7 +19,7 @@ import { assertK8sResource, assertEmptyResource, } from '../util/resource-assertions'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; describe('envDetector()', () => { describe('with valid env', () => { diff --git a/packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts b/packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts index 9290ef5f63..c4f8ce9569 100644 --- a/packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/ProcessDetector.test.ts @@ -19,7 +19,7 @@ import { assertProcessResource, assertEmptyResource, } from '../util/resource-assertions'; -import { NoopLogger } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; describe('processDetector()', () => { let sandbox: sinon.SinonSandbox; diff --git a/packages/opentelemetry-shim-opentracing/src/shim.ts b/packages/opentelemetry-shim-opentracing/src/shim.ts index 3d21b1705f..c6b41c9a9b 100644 --- a/packages/opentelemetry-shim-opentracing/src/shim.ts +++ b/packages/opentelemetry-shim-opentracing/src/shim.ts @@ -15,7 +15,6 @@ */ import * as api from '@opentelemetry/api'; -import { NoopLogger } from '@opentelemetry/core'; import * as opentracing from 'opentracing'; import { Attributes, AttributeValue } from '@opentelemetry/api'; @@ -126,7 +125,7 @@ export class TracerShim extends opentracing.Tracer { super(); this._tracer = tracer; - this._logger = logger || new NoopLogger(); + this._logger = logger || new api.NoopLogger(); } startSpan( diff --git a/packages/opentelemetry-tracing/src/Tracer.ts b/packages/opentelemetry-tracing/src/Tracer.ts index 9cf15e1d3a..fefdb00dfd 100644 --- a/packages/opentelemetry-tracing/src/Tracer.ts +++ b/packages/opentelemetry-tracing/src/Tracer.ts @@ -122,38 +122,6 @@ export class Tracer implements api.Tracer { return span; } - /** - * Returns the current Span from the current context. - * - * If there is no Span associated with the current context, undefined is returned. - */ - getCurrentSpan(): api.Span | undefined { - const ctx = api.context.active(); - // Get the current Span from the context or null if none found. - return api.getSpan(ctx); - } - - /** - * Enters the context of code where the given Span is in the current context. - */ - withSpan ReturnType>( - span: api.Span, - fn: T - ): ReturnType { - // Set given span to context. - return api.context.with(api.setSpan(api.context.active(), span), fn); - } - - /** - * Bind a span (or the current one) to the target's context - */ - bind(target: T, span?: api.Span): T { - return api.context.bind( - target, - span ? api.setSpan(api.context.active(), span) : api.context.active() - ); - } - /** Returns the active {@link TraceParams}. */ getActiveTraceParams(): TraceParams { return this._traceParams; diff --git a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts b/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts index 71086c2ddd..91e3332008 100644 --- a/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts +++ b/packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts @@ -18,15 +18,15 @@ import { context, SpanContext, TraceFlags, - ContextManager, ROOT_CONTEXT, + NoopLogger, setSpan, setSpanContext, + getSpan, } from '@opentelemetry/api'; import { AlwaysOnSampler, AlwaysOffSampler, - NoopLogger, NoRecordingSpan, TraceState, } from '@opentelemetry/core'; @@ -291,24 +291,12 @@ describe('BasicTracerProvider', () => { }); }); - describe('.getCurrentSpan()', () => { - it('should return current span when it exists', () => { - context.setGlobalContextManager({ - active: () => setSpan(ROOT_CONTEXT, ('foo' as any) as Span), - disable: () => {}, - } as ContextManager); - - const tracer = new BasicTracerProvider().getTracer('default'); - assert.deepStrictEqual(tracer.getCurrentSpan(), 'foo'); - }); - }); - describe('.withSpan()', () => { it('should run context with NoopContextManager context manager', done => { const tracer = new BasicTracerProvider().getTracer('default'); const span = tracer.startSpan('my-span'); - tracer.withSpan(span, () => { - assert.deepStrictEqual(tracer.getCurrentSpan(), undefined); + context.with(setSpan(context.active(), span), () => { + assert.deepStrictEqual(getSpan(context.active()), undefined); return done(); }); }); @@ -319,10 +307,10 @@ describe('BasicTracerProvider', () => { const tracer = new BasicTracerProvider().getTracer('default'); const span = tracer.startSpan('my-span'); const fn = () => { - assert.deepStrictEqual(tracer.getCurrentSpan(), undefined); + assert.deepStrictEqual(getSpan(context.active()), undefined); return done(); }; - const patchedFn = tracer.bind(fn, span); + const patchedFn = context.bind(fn, setSpan(context.active(), span)); return patchedFn(); }); }); diff --git a/packages/opentelemetry-tracing/test/Span.test.ts b/packages/opentelemetry-tracing/test/Span.test.ts index 1b4819a43d..0ea733373f 100644 --- a/packages/opentelemetry-tracing/test/Span.test.ts +++ b/packages/opentelemetry-tracing/test/Span.test.ts @@ -22,13 +22,13 @@ import { SpanContext, SpanKind, TraceFlags, + NoopLogger, } from '@opentelemetry/api'; import { hrTime, hrTimeDuration, hrTimeToMilliseconds, hrTimeToNanoseconds, - NoopLogger, } from '@opentelemetry/core'; import { ExceptionAttribute } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; diff --git a/packages/opentelemetry-tracing/test/Tracer.test.ts b/packages/opentelemetry-tracing/test/Tracer.test.ts index 9e28fa46f8..288a823742 100644 --- a/packages/opentelemetry-tracing/test/Tracer.test.ts +++ b/packages/opentelemetry-tracing/test/Tracer.test.ts @@ -23,11 +23,11 @@ import { TraceFlags, ROOT_CONTEXT, suppressInstrumentation, + NoopLogger, } from '@opentelemetry/api'; import { BasicTracerProvider, Tracer, Span } from '../src'; import { InstrumentationLibrary, - NoopLogger, AlwaysOnSampler, AlwaysOffSampler, } from '@opentelemetry/core'; diff --git a/packages/opentelemetry-tracing/test/export/TestTracingSpanExporter.ts b/packages/opentelemetry-tracing/test/export/TestTracingSpanExporter.ts index a5804e8af5..cff7c0e9f4 100644 --- a/packages/opentelemetry-tracing/test/export/TestTracingSpanExporter.ts +++ b/packages/opentelemetry-tracing/test/export/TestTracingSpanExporter.ts @@ -21,7 +21,8 @@ import { Tracer, SpanProcessor, } from '../../src'; -import { ExportResult, NoopLogger, AlwaysOnSampler } from '@opentelemetry/core'; +import { ExportResult, AlwaysOnSampler } from '@opentelemetry/core'; +import { NoopLogger } from '@opentelemetry/api'; /** * A test-only span exporter that naively simulates triggering instrumentation diff --git a/packages/opentelemetry-web/test/WebTracerProvider.test.ts b/packages/opentelemetry-web/test/WebTracerProvider.test.ts index 281f901fc5..7cf43c2767 100644 --- a/packages/opentelemetry-web/test/WebTracerProvider.test.ts +++ b/packages/opentelemetry-web/test/WebTracerProvider.test.ts @@ -14,10 +14,10 @@ * limitations under the License. */ -import { context } from '@opentelemetry/api'; +import { context, NoopLogger, getSpan, setSpan } from '@opentelemetry/api'; import { ContextManager } from '@opentelemetry/context-base'; import { ZoneContextManager } from '@opentelemetry/context-zone'; -import { BasePlugin, NoopLogger } from '@opentelemetry/core'; +import { BasePlugin } from '@opentelemetry/core'; import { InstrumentationBase } from '@opentelemetry/instrumentation'; import { B3Propagator } from '@opentelemetry/propagator-b3'; import { Resource, TELEMETRY_SDK_RESOURCE } from '@opentelemetry/resources'; @@ -133,9 +133,9 @@ describe('WebTracerProvider', () => { const rootSpan = webTracerWithZone.startSpan('rootSpan'); - webTracerWithZone.withSpan(rootSpan, () => { + context.with(setSpan(context.active(), rootSpan), () => { assert.ok( - webTracerWithZone.getCurrentSpan() === rootSpan, + getSpan(context.active()) === rootSpan, 'Current span is rootSpan' ); const concurrentSpan1 = webTracerWithZone.startSpan( @@ -145,19 +145,19 @@ describe('WebTracerProvider', () => { 'concurrentSpan2' ); - webTracerWithZone.withSpan(concurrentSpan1, () => { + context.with(setSpan(context.active(), concurrentSpan1), () => { setTimeout(() => { assert.ok( - webTracerWithZone.getCurrentSpan() === concurrentSpan1, + getSpan(context.active()) === concurrentSpan1, 'Current span is concurrentSpan1' ); }, 10); }); - webTracerWithZone.withSpan(concurrentSpan2, () => { + context.with(setSpan(context.active(), concurrentSpan2), () => { setTimeout(() => { assert.ok( - webTracerWithZone.getCurrentSpan() === concurrentSpan2, + getSpan(context.active()) === concurrentSpan2, 'Current span is concurrentSpan2' ); done();