From 3013f0ab5ab05e9727a84edb36596e6cd2d4c404 Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Tue, 20 Apr 2021 13:15:28 +0200 Subject: [PATCH 1/2] chore: prefer use of global TracerProvider/MeterProvider Remove setting of TracerProvider in samples where it is actually not needed as the global TracerProvider should be prefered on default. --- examples/grpc-js/tracer.js | 1 - examples/grpc/tracer.js | 1 - examples/http/tracer.js | 1 - examples/https/tracer.js | 1 - examples/opentracing-shim/shim.js | 1 - examples/tracer-web/examples/fetch/index.js | 3 +-- .../examples/xml-http-request/index.js | 1 - getting-started/README.md | 6 ++---- getting-started/traced-example/tracing.js | 1 - getting-started/ts-example/README.md | 2 -- .../ts-example/traced-example/tracing.ts | 1 - .../README.md | 1 - .../README.md | 1 - .../README.md | 1 - .../README.md | 1 - .../opentelemetry-instrumentation/README.md | 20 ++++++++++++++----- packages/opentelemetry-node/README.md | 2 -- packages/opentelemetry-sdk-node/src/sdk.ts | 2 -- packages/opentelemetry-web/README.md | 1 - 19 files changed, 18 insertions(+), 30 deletions(-) diff --git a/examples/grpc-js/tracer.js b/examples/grpc-js/tracer.js index 21f371dcd71..5b794740bf0 100644 --- a/examples/grpc-js/tracer.js +++ b/examples/grpc-js/tracer.js @@ -16,7 +16,6 @@ module.exports = (serviceName) => { instrumentations: [ new GrpcInstrumentation(), ], - tracerProvider: provider, }); let exporter; diff --git a/examples/grpc/tracer.js b/examples/grpc/tracer.js index d880cb873b2..cf148e5232a 100644 --- a/examples/grpc/tracer.js +++ b/examples/grpc/tracer.js @@ -13,7 +13,6 @@ const EXPORTER = process.env.EXPORTER || ''; module.exports = (serviceName) => { const provider = new NodeTracerProvider(); registerInstrumentations({ - tracerProvider: provider, instrumentations: [ new GrpcInstrumentation(), ], diff --git a/examples/http/tracer.js b/examples/http/tracer.js index db7e2191329..f8df873b77d 100644 --- a/examples/http/tracer.js +++ b/examples/http/tracer.js @@ -13,7 +13,6 @@ const EXPORTER = process.env.EXPORTER || ''; module.exports = (serviceName) => { const provider = new NodeTracerProvider(); registerInstrumentations({ - tracerProvider: provider, // // when boostraping with lerna for testing purposes instrumentations: [ new HttpInstrumentation(), diff --git a/examples/https/tracer.js b/examples/https/tracer.js index c239467ea00..d5e2a3cc743 100644 --- a/examples/https/tracer.js +++ b/examples/https/tracer.js @@ -15,7 +15,6 @@ module.exports = (serviceName) => { let exporter; const provider = new NodeTracerProvider(); registerInstrumentations({ - tracerProvider: provider, instrumentations: [ new HttpInstrumentation(), ], diff --git a/examples/opentracing-shim/shim.js b/examples/opentracing-shim/shim.js index e5d02093207..ea77f30a3bc 100644 --- a/examples/opentracing-shim/shim.js +++ b/examples/opentracing-shim/shim.js @@ -10,7 +10,6 @@ const { TracerShim } = require('@opentelemetry/shim-opentracing'); function shim(serviceName) { const provider = new NodeTracerProvider(); registerInstrumentations({ - tracerProvider: provider, }); provider.addSpanProcessor(new SimpleSpanProcessor(getExporter(serviceName))); diff --git a/examples/tracer-web/examples/fetch/index.js b/examples/tracer-web/examples/fetch/index.js index ec5eb3e8c31..a3e5d6b3f90 100644 --- a/examples/tracer-web/examples/fetch/index.js +++ b/examples/tracer-web/examples/fetch/index.js @@ -20,10 +20,9 @@ registerInstrumentations({ 'https://cors-test.appspot.com/test', 'https://httpbin.org/get', ], - clearTimingResources: true + clearTimingResources: true, }), ], - tracerProvider: provider, }); provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); diff --git a/examples/tracer-web/examples/xml-http-request/index.js b/examples/tracer-web/examples/xml-http-request/index.js index 435a74a09cf..80f790798ad 100644 --- a/examples/tracer-web/examples/xml-http-request/index.js +++ b/examples/tracer-web/examples/xml-http-request/index.js @@ -18,7 +18,6 @@ registerInstrumentations({ ], }), ], - tracerProvider: providerWithZone, }); providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); diff --git a/getting-started/README.md b/getting-started/README.md index cdb31d53a3b..e3abdd231a8 100644 --- a/getting-started/README.md +++ b/getting-started/README.md @@ -4,8 +4,8 @@ This guide walks you through the setup and configuration process for a tracing b - [Getting started with OpenTelemetry JS](#getting-started-with-opentelemetry-js) - [Trace your application with OpenTelemetry](#trace-your-application-with-opentelemetry) - - [Set up a Tracing Backend](#set-up-a-tracing-backend) - - [Trace Your NodeJS Application](#trace-your-nodejs-application) + - [Set up a tracing backend](#set-up-a-tracing-backend) + - [Trace your NodeJS application](#trace-your-nodejs-application) - [Install the required OpenTelemetry libraries](#install-the-required-opentelemetry-libraries) - [Initialize a global tracer](#initialize-a-global-tracer) - [Initialize and register a trace exporter](#initialize-and-register-a-trace-exporter) @@ -92,7 +92,6 @@ registerInstrumentations({ new HttpInstrumentation(), new GrpcInstrumentation(), ], - tracerProvider: provider, }); ``` @@ -153,7 +152,6 @@ registerInstrumentations({ new HttpInstrumentation(), new GrpcInstrumentation(), ], - tracerProvider: provider, }); console.log("tracing initialized"); diff --git a/getting-started/traced-example/tracing.js b/getting-started/traced-example/tracing.js index bfd6ae60c48..5c3172682d7 100644 --- a/getting-started/traced-example/tracing.js +++ b/getting-started/traced-example/tracing.js @@ -24,7 +24,6 @@ provider.register(); // load old default plugins registerInstrumentations({ - tracerProvider: provider, instrumentations: [ new ExpressInstrumentation(), new HttpInstrumentation(), diff --git a/getting-started/ts-example/README.md b/getting-started/ts-example/README.md index cfb8394ca5c..326e0e56418 100644 --- a/getting-started/ts-example/README.md +++ b/getting-started/ts-example/README.md @@ -88,7 +88,6 @@ const provider: NodeTracerProvider = new NodeTracerProvider({ provider.register(); registerInstrumentations({ - tracerProvider: provider, instrumentations: [ new ExpressInstrumentation(), new HttpInstrumentation(), @@ -153,7 +152,6 @@ provider.addSpanProcessor( ); registerInstrumentations({ - tracerProvider: provider, instrumentations: [ new ExpressInstrumentation(), new HttpInstrumentation(), diff --git a/getting-started/ts-example/traced-example/tracing.ts b/getting-started/ts-example/traced-example/tracing.ts index ec7738f752e..21df0b15b80 100644 --- a/getting-started/ts-example/traced-example/tracing.ts +++ b/getting-started/ts-example/traced-example/tracing.ts @@ -27,7 +27,6 @@ provider.addSpanProcessor( ); registerInstrumentations({ - tracerProvider: provider, instrumentations: [ new ExpressInstrumentation(), new HttpInstrumentation(), diff --git a/packages/opentelemetry-instrumentation-fetch/README.md b/packages/opentelemetry-instrumentation-fetch/README.md index 2e139bd5eb1..33f2caec741 100644 --- a/packages/opentelemetry-instrumentation-fetch/README.md +++ b/packages/opentelemetry-instrumentation-fetch/README.md @@ -33,7 +33,6 @@ provider.register({ registerInstrumentations({ instrumentations: [new FetchInstrumentation()], - tracerProvider: provider, }); // or plugin can be also initialised separately and then set the tracer provider or meter provider diff --git a/packages/opentelemetry-instrumentation-grpc/README.md b/packages/opentelemetry-instrumentation-grpc/README.md index 1556863e700..65445766998 100644 --- a/packages/opentelemetry-instrumentation-grpc/README.md +++ b/packages/opentelemetry-instrumentation-grpc/README.md @@ -34,7 +34,6 @@ provider.register(); registerInstrumentations({ instrumentations: [new GrpcInstrumentation()] - tracerProvider: provider, }); ``` diff --git a/packages/opentelemetry-instrumentation-http/README.md b/packages/opentelemetry-instrumentation-http/README.md index 0755af4b63f..fb6b5e04bb9 100644 --- a/packages/opentelemetry-instrumentation-http/README.md +++ b/packages/opentelemetry-instrumentation-http/README.md @@ -35,7 +35,6 @@ provider.register(); registerInstrumentations({ instrumentations: [new HttpInstrumentation()], - tracerProvider: provider, }); ``` diff --git a/packages/opentelemetry-instrumentation-xml-http-request/README.md b/packages/opentelemetry-instrumentation-xml-http-request/README.md index c9333a04aa2..1281f0427cd 100644 --- a/packages/opentelemetry-instrumentation-xml-http-request/README.md +++ b/packages/opentelemetry-instrumentation-xml-http-request/README.md @@ -35,7 +35,6 @@ registerInstrumentations({ propagateTraceHeaderCorsUrls: ['http://localhost:8090'] }), ], - tracerProvider: provider, }); diff --git a/packages/opentelemetry-instrumentation/README.md b/packages/opentelemetry-instrumentation/README.md index 55dcad32dbc..e3beeb7f9d6 100644 --- a/packages/opentelemetry-instrumentation/README.md +++ b/packages/opentelemetry-instrumentation/README.md @@ -109,7 +109,7 @@ export class MyPlugin extends InstrumentationBase { // Later const myPLugin = new MyPlugin(); -myPLugin.setTracerProvider(provider); // this is optional +myPLugin.setTracerProvider(provider); // this is optional, only if global TracerProvider shouldn't be used myPLugin.setMeterProvider(meterProvider); // this is optional myPLugin.enable(); // or use Auto Loader @@ -151,8 +151,8 @@ export class MyPlugin extends InstrumentationBase { // Later const myPLugin = new MyPlugin(); -myPLugin.setTracerProvider(provider); -myPLugin.setMeterProvider(meterProvider); +myPLugin.setTracerProvider(provider); // this is optional, only if global TracerProvider shouldn't be used +myPLugin.setMeterProvider(meterProvider); // this is optional, only if global MeterProvider shouldn't be used myPLugin.enable(); // or use Auto Loader ``` @@ -221,7 +221,8 @@ registerInstrumentations({ instrumentations: [ new HttpInstrumentation(), ], - tracerProvider: tracerProvider, + //tracerProvider: tracerProvider, // optional, only if global TracerProvider shouldn't be used + //meterProvider: meterProvider, // optional, only if global MeterProvider shouldn't be used }); ``` @@ -249,10 +250,19 @@ registerInstrumentations({ ], }), ], - tracerProvider: tracerProvider, + //tracerProvider: tracerProvider, // optional, only if global TracerProvider shouldn't be used + //meterProvider: meterProvider, // optional, only if global MeterProvider shouldn't be used }); ``` +## Selection of the used TracerProvider/MeterProvider + +The `registerInstrumentations()` API allows to specify which `TracerProvider` and/or `MeterProvider` to use by the given options object. +If nothing is specified the global registered provider is used. Usually this is what most users want therefore it's recommended to keep this default. + +There might be usecase where someone has the need for more providers within an application. Please note that special care must be takes in such setups +to avoid leaking information from one provider to the other because there are a lot places where e.g. the global `ContextManager` or `Propagator` is used. + ## License Apache 2.0 - See [LICENSE][license-url] for more information. diff --git a/packages/opentelemetry-node/README.md b/packages/opentelemetry-node/README.md index 04a4b64abbb..ed0b0d00643 100644 --- a/packages/opentelemetry-node/README.md +++ b/packages/opentelemetry-node/README.md @@ -64,7 +64,6 @@ provider.register(); // register and load instrumentation and old plugins - old plugins will be loaded automatically as previously // but instrumentations needs to be added registerInstrumentations({ - tracerProvider: provider, }); // Your application code - http will automatically be instrumented if @@ -88,7 +87,6 @@ const provider = new NodeTracerProvider(); // register and load instrumentation and old plugins - old plugins will be loaded automatically as previously // but instrumentations needs to be added registerInstrumentations({ - tracerProvider: provider, instrumentations: [ new ExpressInstrumentation(), new HttpInstrumentation({ diff --git a/packages/opentelemetry-sdk-node/src/sdk.ts b/packages/opentelemetry-sdk-node/src/sdk.ts index d717415fd9f..77914611536 100644 --- a/packages/opentelemetry-sdk-node/src/sdk.ts +++ b/packages/opentelemetry-sdk-node/src/sdk.ts @@ -176,8 +176,6 @@ export class NodeSDK { registerInstrumentations({ instrumentations: this._instrumentations, - tracerProvider: this._tracerProvider, - meterProvider: this._meterProvider, }); } diff --git a/packages/opentelemetry-web/README.md b/packages/opentelemetry-web/README.md index 8cae93ecf70..9c925f5ed2e 100644 --- a/packages/opentelemetry-web/README.md +++ b/packages/opentelemetry-web/README.md @@ -52,7 +52,6 @@ registerInstrumentations({ instrumentations: [ new DocumentLoad(), ], - tracerProvider: provider, }); ``` From 2a0d59d41b45456f35273e82799bfaef3a20d39b Mon Sep 17 00:00:00 2001 From: Gerhard Stoebich <18708370+Flarna@users.noreply.github.com> Date: Thu, 22 Apr 2021 08:45:25 +0200 Subject: [PATCH 2/2] chore: align provider.register() and registerInstrumentations() sequence --- examples/grpc-js/tracer.js | 11 ++++++----- examples/grpc/tracer.js | 11 ++++++----- examples/http/tracer.js | 13 +++++++------ examples/https/tracer.js | 11 ++++++----- examples/opentracing-shim/shim.js | 5 +++-- examples/tracer-web/examples/fetch/index.js | 13 ++++++------- .../tracer-web/examples/xml-http-request/index.js | 15 +++++++-------- getting-started/ts-example/README.md | 4 ++-- .../ts-example/traced-example/tracing.ts | 4 +--- packages/opentelemetry-node/README.md | 2 ++ 10 files changed, 46 insertions(+), 43 deletions(-) diff --git a/examples/grpc-js/tracer.js b/examples/grpc-js/tracer.js index 5b794740bf0..7710dff1d88 100644 --- a/examples/grpc-js/tracer.js +++ b/examples/grpc-js/tracer.js @@ -12,11 +12,6 @@ const EXPORTER = process.env.EXPORTER || ''; module.exports = (serviceName) => { const provider = new NodeTracerProvider(); - registerInstrumentations({ - instrumentations: [ - new GrpcInstrumentation(), - ], - }); let exporter; if (EXPORTER.toLowerCase().startsWith('z')) { @@ -34,5 +29,11 @@ module.exports = (serviceName) => { // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); + registerInstrumentations({ + instrumentations: [ + new GrpcInstrumentation(), + ], + }); + return opentelemetry.trace.getTracer('grpc-js-example'); }; diff --git a/examples/grpc/tracer.js b/examples/grpc/tracer.js index cf148e5232a..5c108de48df 100644 --- a/examples/grpc/tracer.js +++ b/examples/grpc/tracer.js @@ -12,11 +12,6 @@ const EXPORTER = process.env.EXPORTER || ''; module.exports = (serviceName) => { const provider = new NodeTracerProvider(); - registerInstrumentations({ - instrumentations: [ - new GrpcInstrumentation(), - ], - }); let exporter; if (EXPORTER.toLowerCase().startsWith('z')) { @@ -34,5 +29,11 @@ module.exports = (serviceName) => { // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); + registerInstrumentations({ + instrumentations: [ + new GrpcInstrumentation(), + ], + }); + return opentelemetry.trace.getTracer('grpc-example'); }; diff --git a/examples/http/tracer.js b/examples/http/tracer.js index f8df873b77d..25e97ccc061 100644 --- a/examples/http/tracer.js +++ b/examples/http/tracer.js @@ -12,12 +12,6 @@ const EXPORTER = process.env.EXPORTER || ''; module.exports = (serviceName) => { const provider = new NodeTracerProvider(); - registerInstrumentations({ - // // when boostraping with lerna for testing purposes - instrumentations: [ - new HttpInstrumentation(), - ], - }); let exporter; if (EXPORTER.toLowerCase().startsWith('z')) { @@ -35,5 +29,12 @@ module.exports = (serviceName) => { // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); + registerInstrumentations({ + // // when boostraping with lerna for testing purposes + instrumentations: [ + new HttpInstrumentation(), + ], + }); + return opentelemetry.trace.getTracer('http-example'); }; diff --git a/examples/https/tracer.js b/examples/https/tracer.js index d5e2a3cc743..dbee05b0998 100644 --- a/examples/https/tracer.js +++ b/examples/https/tracer.js @@ -14,11 +14,6 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; module.exports = (serviceName) => { let exporter; const provider = new NodeTracerProvider(); - registerInstrumentations({ - instrumentations: [ - new HttpInstrumentation(), - ], - }); if (EXPORTER.toLowerCase().startsWith('z')) { exporter = new ZipkinExporter({ @@ -35,5 +30,11 @@ module.exports = (serviceName) => { // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); + registerInstrumentations({ + instrumentations: [ + new HttpInstrumentation(), + ], + }); + return opentelemetry.trace.getTracer('https-example'); }; diff --git a/examples/opentracing-shim/shim.js b/examples/opentracing-shim/shim.js index ea77f30a3bc..31a71d9a21b 100644 --- a/examples/opentracing-shim/shim.js +++ b/examples/opentracing-shim/shim.js @@ -9,13 +9,14 @@ const { TracerShim } = require('@opentelemetry/shim-opentracing'); function shim(serviceName) { const provider = new NodeTracerProvider(); - registerInstrumentations({ - }); provider.addSpanProcessor(new SimpleSpanProcessor(getExporter(serviceName))); // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings provider.register(); + registerInstrumentations({ + }); + return new TracerShim(provider.getTracer('opentracing-shim')); } diff --git a/examples/tracer-web/examples/fetch/index.js b/examples/tracer-web/examples/fetch/index.js index a3e5d6b3f90..97f9910d2d3 100644 --- a/examples/tracer-web/examples/fetch/index.js +++ b/examples/tracer-web/examples/fetch/index.js @@ -11,6 +11,12 @@ import { B3Propagator } from '@opentelemetry/propagator-b3'; import { registerInstrumentations } from '@opentelemetry/instrumentation'; const provider = new WebTracerProvider(); +provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +provider.addSpanProcessor(new SimpleSpanProcessor(new CollectorTraceExporter())); +provider.register({ + contextManager: new ZoneContextManager(), + propagator: new B3Propagator(), +}); registerInstrumentations({ instrumentations: [ @@ -25,13 +31,6 @@ registerInstrumentations({ ], }); -provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); -provider.addSpanProcessor(new SimpleSpanProcessor(new CollectorTraceExporter())); -provider.register({ - contextManager: new ZoneContextManager(), - propagator: new B3Propagator(), -}); - const webTracerWithZone = provider.getTracer('example-tracer-web'); const getData = (url) => fetch(url, { diff --git a/examples/tracer-web/examples/xml-http-request/index.js b/examples/tracer-web/examples/xml-http-request/index.js index 80f790798ad..c3990efe7a3 100644 --- a/examples/tracer-web/examples/xml-http-request/index.js +++ b/examples/tracer-web/examples/xml-http-request/index.js @@ -8,6 +8,13 @@ import { B3Propagator } from '@opentelemetry/propagator-b3'; import { registerInstrumentations } from '@opentelemetry/instrumentation'; const providerWithZone = new WebTracerProvider(); +providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); +providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new CollectorTraceExporter())); + +providerWithZone.register({ + contextManager: new ZoneContextManager(), + propagator: new B3Propagator(), +}); registerInstrumentations({ instrumentations: [ @@ -20,14 +27,6 @@ registerInstrumentations({ ], }); -providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); -providerWithZone.addSpanProcessor(new SimpleSpanProcessor(new CollectorTraceExporter())); - -providerWithZone.register({ - contextManager: new ZoneContextManager(), - propagator: new B3Propagator(), -}); - const webTracerWithZone = providerWithZone.getTracer('example-tracer-web'); const getData = (url) => new Promise((resolve, reject) => { diff --git a/getting-started/ts-example/README.md b/getting-started/ts-example/README.md index 326e0e56418..7c2b28d8f41 100644 --- a/getting-started/ts-example/README.md +++ b/getting-started/ts-example/README.md @@ -136,8 +136,6 @@ const provider: NodeTracerProvider = new NodeTracerProvider({ logLevel: LogLevel.ERROR, }); -provider.register(); - provider.addSpanProcessor( new SimpleSpanProcessor( new ZipkinExporter({ @@ -151,6 +149,8 @@ provider.addSpanProcessor( ), ); +provider.register(); + registerInstrumentations({ instrumentations: [ new ExpressInstrumentation(), diff --git a/getting-started/ts-example/traced-example/tracing.ts b/getting-started/ts-example/traced-example/tracing.ts index 21df0b15b80..4598578ef2d 100644 --- a/getting-started/ts-example/traced-example/tracing.ts +++ b/getting-started/ts-example/traced-example/tracing.ts @@ -10,9 +10,6 @@ const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-expre const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); const provider: NodeTracerProvider = new NodeTracerProvider(); - -provider.register(); - provider.addSpanProcessor( new SimpleSpanProcessor( new ZipkinExporter({ @@ -25,6 +22,7 @@ provider.addSpanProcessor( }), ), ); +provider.register(); registerInstrumentations({ instrumentations: [ diff --git a/packages/opentelemetry-node/README.md b/packages/opentelemetry-node/README.md index ed0b0d00643..374261c6354 100644 --- a/packages/opentelemetry-node/README.md +++ b/packages/opentelemetry-node/README.md @@ -79,10 +79,12 @@ In the following example: - the http instrumentation has a custom config for a `requestHook` ```javascript +const { registerInstrumentations } = require('@opentelemetry/instrumentation'); const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express'); const provider = new NodeTracerProvider(); +provider.register(); // register and load instrumentation and old plugins - old plugins will be loaded automatically as previously // but instrumentations needs to be added