From 44c77a080ea448b0400214eedac6ce24a792d1a7 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Wed, 22 May 2024 16:16:42 +0200 Subject: [PATCH 1/8] feat(auto-instrumentations-node): add getPropagator to get propagators from enviornment --- .../auto-instrumentations-node/package.json | 7 +- .../auto-instrumentations-node/src/index.ts | 1 + .../src/register.ts | 2 + .../auto-instrumentations-node/src/utils.ts | 64 ++++++++++++++++++- .../test/utils.test.ts | 30 ++++++++- package-lock.json | 10 +++ 6 files changed, 111 insertions(+), 3 deletions(-) diff --git a/metapackages/auto-instrumentations-node/package.json b/metapackages/auto-instrumentations-node/package.json index 0a3ae85cc1..c3ef40bbcb 100644 --- a/metapackages/auto-instrumentations-node/package.json +++ b/metapackages/auto-instrumentations-node/package.json @@ -94,7 +94,12 @@ "@opentelemetry/resource-detector-container": "^0.3.9", "@opentelemetry/resource-detector-gcp": "^0.29.9", "@opentelemetry/resources": "^1.24.0", - "@opentelemetry/sdk-node": "^0.51.0" + "@opentelemetry/sdk-node": "^0.51.0", + "@opentelemetry/core": "^1.24.1", + "@opentelemetry/propagator-b3": "^1.24.1", + "@opentelemetry/propagator-jaeger": "^1.24.1", + "@opentelemetry/propagator-aws-xray": "^1.24.1", + "@opentelemetry/propagator-ot-trace": "^0.27.2" }, "files": [ "build/src/**/*.js", diff --git a/metapackages/auto-instrumentations-node/src/index.ts b/metapackages/auto-instrumentations-node/src/index.ts index 669066d0a9..5daf061b84 100644 --- a/metapackages/auto-instrumentations-node/src/index.ts +++ b/metapackages/auto-instrumentations-node/src/index.ts @@ -17,5 +17,6 @@ export { getNodeAutoInstrumentations, getResourceDetectorsFromEnv as getResourceDetectors, + getPropagator, InstrumentationConfigMap, } from './utils'; diff --git a/metapackages/auto-instrumentations-node/src/register.ts b/metapackages/auto-instrumentations-node/src/register.ts index a14538da66..f32d769382 100644 --- a/metapackages/auto-instrumentations-node/src/register.ts +++ b/metapackages/auto-instrumentations-node/src/register.ts @@ -17,6 +17,7 @@ import * as opentelemetry from '@opentelemetry/sdk-node'; import { diag, DiagConsoleLogger } from '@opentelemetry/api'; import { getNodeAutoInstrumentations, + getPropagator, getResourceDetectorsFromEnv, } from './utils'; @@ -28,6 +29,7 @@ diag.setLogger( const sdk = new opentelemetry.NodeSDK({ instrumentations: getNodeAutoInstrumentations(), resourceDetectors: getResourceDetectorsFromEnv(), + textMapPropagator: getPropagator(), }); try { diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index 04f4c1ed50..5e66461796 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { diag } from '@opentelemetry/api'; +import { diag, TextMapPropagator } from '@opentelemetry/api'; import { Instrumentation } from '@opentelemetry/instrumentation'; import { AmqplibInstrumentation } from '@opentelemetry/instrumentation-amqplib'; @@ -80,6 +80,15 @@ import { azureFunctionsDetector, azureVmDetector, } from '@opentelemetry/resource-detector-azure'; +import { + CompositePropagator, + W3CBaggagePropagator, + W3CTraceContextPropagator, +} from '@opentelemetry/core'; +import { B3InjectEncoding, B3Propagator } from '@opentelemetry/propagator-b3'; +import { JaegerPropagator } from '@opentelemetry/propagator-jaeger'; +import { OTTracePropagator } from '@opentelemetry/propagator-ot-trace'; +import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray'; const RESOURCE_DETECTOR_CONTAINER = 'container'; const RESOURCE_DETECTOR_ENVIRONMENT = 'env'; @@ -253,3 +262,56 @@ export function getResourceDetectorsFromEnv(): Array { return resourceDetector || []; }); } + +type PropagatorFactoryFunction = () => TextMapPropagator; + +const propagatorMap = new Map([ + ['tracecontext', () => new W3CTraceContextPropagator()], + ['baggage', () => new W3CTraceContextPropagator()], + [ + 'b3', + () => new B3Propagator({ injectEncoding: B3InjectEncoding.SINGLE_HEADER }), + ], + [ + 'b3multi', + () => new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER }), + ], + ['jaeger', () => new JaegerPropagator()], + ['xray', () => new AWSXRayPropagator()], + ['ottrace', () => new OTTracePropagator()], +]); + +/** + * Get a propagator based on the OTEL_PROPAGATORS env var. + */ +export function getPropagator(): TextMapPropagator { + if (process.env.OTEL_PROPAGATORS == null) { + return new CompositePropagator({ + propagators: [ + new W3CTraceContextPropagator(), + new W3CBaggagePropagator(), + ], + }); + } + + const propagatorsFromEnv = process.env.OTEL_PROPAGATORS?.split(',').map( + value => value.toLowerCase() + ); + + const propagators = propagatorsFromEnv.flatMap(propagatorName => { + const propagatorFactoryFunction = propagatorMap.get(propagatorName); + if (propagatorFactoryFunction == null) { + diag.error( + `Invalid propagator "${propagatorName}" specified in the environment variable OTEL_PROPAGATORS` + ); + return []; + } + return propagatorFactoryFunction(); + }); + + if (propagators.length === 1) { + return propagators[0]; + } + + return new CompositePropagator({ propagators }); +} diff --git a/metapackages/auto-instrumentations-node/test/utils.test.ts b/metapackages/auto-instrumentations-node/test/utils.test.ts index 0622022f36..56dd3fdc76 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -19,7 +19,8 @@ import { HttpInstrumentationConfig } from '@opentelemetry/instrumentation-http'; import * as assert from 'assert'; import * as sinon from 'sinon'; import { getNodeAutoInstrumentations } from '../src'; -import { getResourceDetectorsFromEnv } from '../src/utils'; +import { getPropagator, getResourceDetectorsFromEnv } from '../src/utils'; +import { CompositePropagator } from '@opentelemetry/core'; describe('utils', () => { describe('getNodeAutoInstrumentations', () => { @@ -161,4 +162,31 @@ describe('utils', () => { delete process.env.OTEL_NODE_RESOURCE_DETECTORS; }); }); + + describe('getPropagator', () => { + afterEach(() => { + delete process.env.OTEL_PROPAGATORS; + }); + + it('should return default when env var is unset', () => { + assert.deepStrictEqual(getPropagator().fields(), [ + 'traceparent', + 'tracestate', + 'baggage', + ]); + }); + + it('should return the selected propagator when one is in the list', () => { + process.env.OTEL_PROPAGATORS = 'tracecontext'; + assert.deepStrictEqual(getPropagator().fields(), [ + 'traceparent', + 'tracestate', + ]); + }); + + it('should return the selected propagators when multiple are in the list', () => { + process.env.OTEL_PROPAGATORS = 'b3,jaeger'; + assert.deepStrictEqual(getPropagator().fields(), ['b3', 'uber-trace-id']); + }); + }); }); diff --git a/package-lock.json b/package-lock.json index a9316e10ad..33b57da2e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -284,6 +284,7 @@ "version": "0.46.0", "license": "Apache-2.0", "dependencies": { + "@opentelemetry/core": "^1.24.1", "@opentelemetry/instrumentation": "^0.51.0", "@opentelemetry/instrumentation-amqplib": "^0.37.0", "@opentelemetry/instrumentation-aws-lambda": "^0.41.0", @@ -323,6 +324,10 @@ "@opentelemetry/instrumentation-tedious": "^0.10.1", "@opentelemetry/instrumentation-undici": "^0.2.0", "@opentelemetry/instrumentation-winston": "^0.37.0", + "@opentelemetry/propagator-aws-xray": "^1.24.1", + "@opentelemetry/propagator-b3": "^1.24.1", + "@opentelemetry/propagator-jaeger": "^1.24.1", + "@opentelemetry/propagator-ot-trace": "^0.27.2", "@opentelemetry/resource-detector-alibaba-cloud": "^0.28.9", "@opentelemetry/resource-detector-aws": "^1.4.2", "@opentelemetry/resource-detector-azure": "^0.2.6", @@ -46193,6 +46198,7 @@ "version": "file:metapackages/auto-instrumentations-node", "requires": { "@opentelemetry/api": "^1.4.1", + "@opentelemetry/core": "^1.24.1", "@opentelemetry/instrumentation": "^0.51.0", "@opentelemetry/instrumentation-amqplib": "^0.37.0", "@opentelemetry/instrumentation-aws-lambda": "^0.41.0", @@ -46232,6 +46238,10 @@ "@opentelemetry/instrumentation-tedious": "^0.10.1", "@opentelemetry/instrumentation-undici": "^0.2.0", "@opentelemetry/instrumentation-winston": "^0.37.0", + "@opentelemetry/propagator-aws-xray": "^1.24.1", + "@opentelemetry/propagator-b3": "^1.24.1", + "@opentelemetry/propagator-jaeger": "^1.24.1", + "@opentelemetry/propagator-ot-trace": "^0.27.2", "@opentelemetry/resource-detector-alibaba-cloud": "^0.28.9", "@opentelemetry/resource-detector-aws": "^1.4.2", "@opentelemetry/resource-detector-azure": "^0.2.6", From 65398082e0da84a8868a5335232e7f36d006405a Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Wed, 22 May 2024 17:07:37 +0200 Subject: [PATCH 2/8] fixup! feat(auto-instrumentations-node): add getPropagator to get propagators from enviornment --- metapackages/auto-instrumentations-node/test/utils.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/metapackages/auto-instrumentations-node/test/utils.test.ts b/metapackages/auto-instrumentations-node/test/utils.test.ts index 56dd3fdc76..654b1de5da 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -20,7 +20,6 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; import { getNodeAutoInstrumentations } from '../src'; import { getPropagator, getResourceDetectorsFromEnv } from '../src/utils'; -import { CompositePropagator } from '@opentelemetry/core'; describe('utils', () => { describe('getNodeAutoInstrumentations', () => { From c67eda1cd17374a7c7e56d4ce97c2c96c3024259 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 27 May 2024 14:09:54 +0200 Subject: [PATCH 3/8] fixup! feat(auto-instrumentations-node): add getPropagator to get propagators from enviornment --- metapackages/auto-instrumentations-node/src/utils.ts | 12 ++++++------ .../auto-instrumentations-node/test/utils.test.ts | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index 5e66461796..b679d008a8 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -294,8 +294,12 @@ export function getPropagator(): TextMapPropagator { }); } - const propagatorsFromEnv = process.env.OTEL_PROPAGATORS?.split(',').map( - value => value.toLowerCase() + const propagatorsFromEnv = Array.from( + new Set( + process.env.OTEL_PROPAGATORS?.split(',').map(value => + value.toLowerCase().trim() + ) + ) ); const propagators = propagatorsFromEnv.flatMap(propagatorName => { @@ -309,9 +313,5 @@ export function getPropagator(): TextMapPropagator { return propagatorFactoryFunction(); }); - if (propagators.length === 1) { - return propagators[0]; - } - return new CompositePropagator({ propagators }); } diff --git a/metapackages/auto-instrumentations-node/test/utils.test.ts b/metapackages/auto-instrumentations-node/test/utils.test.ts index 654b1de5da..76a4afe9e5 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -187,5 +187,10 @@ describe('utils', () => { process.env.OTEL_PROPAGATORS = 'b3,jaeger'; assert.deepStrictEqual(getPropagator().fields(), ['b3', 'uber-trace-id']); }); + + it('should return no-op propgator if propagator all propagators are unknown', () => { + process.env.OTEL_PROPAGATORS = 'my, unknown, propagators'; + assert.deepStrictEqual(getPropagator().fields(), []); + }) }); }); From d13fbdd70e2081fb553f36b86e4c6447b18f34e8 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 27 May 2024 14:31:57 +0200 Subject: [PATCH 4/8] fix: lint --- metapackages/auto-instrumentations-node/test/utils.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metapackages/auto-instrumentations-node/test/utils.test.ts b/metapackages/auto-instrumentations-node/test/utils.test.ts index 76a4afe9e5..1aaf1c5830 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -191,6 +191,6 @@ describe('utils', () => { it('should return no-op propgator if propagator all propagators are unknown', () => { process.env.OTEL_PROPAGATORS = 'my, unknown, propagators'; assert.deepStrictEqual(getPropagator().fields(), []); - }) + }); }); }); From f8b44802fdcef8a924759bebe4a2ef1588b19466 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 3 Jun 2024 16:16:54 +0200 Subject: [PATCH 5/8] Update metapackages/auto-instrumentations-node/test/utils.test.ts Co-authored-by: Trent Mick --- metapackages/auto-instrumentations-node/test/utils.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metapackages/auto-instrumentations-node/test/utils.test.ts b/metapackages/auto-instrumentations-node/test/utils.test.ts index 1aaf1c5830..66453be63a 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -188,7 +188,7 @@ describe('utils', () => { assert.deepStrictEqual(getPropagator().fields(), ['b3', 'uber-trace-id']); }); - it('should return no-op propgator if propagator all propagators are unknown', () => { + it('should return no-op propagator if all propagators are unknown', () => { process.env.OTEL_PROPAGATORS = 'my, unknown, propagators'; assert.deepStrictEqual(getPropagator().fields(), []); }); From cf3bb0d733fd56c2daa0ec183c62be28b8a34743 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Tue, 11 Jun 2024 16:16:36 +0200 Subject: [PATCH 6/8] fix(auto-instrumenations-node): ensure that empty values, all space value, and 'none' is handled --- .../auto-instrumentations-node/src/utils.ts | 9 +++++++- .../test/utils.test.ts | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/metapackages/auto-instrumentations-node/src/utils.ts b/metapackages/auto-instrumentations-node/src/utils.ts index b679d008a8..0766972170 100644 --- a/metapackages/auto-instrumentations-node/src/utils.ts +++ b/metapackages/auto-instrumentations-node/src/utils.ts @@ -285,7 +285,7 @@ const propagatorMap = new Map([ * Get a propagator based on the OTEL_PROPAGATORS env var. */ export function getPropagator(): TextMapPropagator { - if (process.env.OTEL_PROPAGATORS == null) { + if (process.env.OTEL_PROPAGATORS == null || process.env.OTEL_PROPAGATORS.trim() === '') { return new CompositePropagator({ propagators: [ new W3CTraceContextPropagator(), @@ -303,6 +303,13 @@ export function getPropagator(): TextMapPropagator { ); const propagators = propagatorsFromEnv.flatMap(propagatorName => { + if (propagatorName === 'none'){ + diag.info( + `Not selecting any propagator for value "none" specified in the environment variable OTEL_PROPAGATORS` + ); + return []; + } + const propagatorFactoryFunction = propagatorMap.get(propagatorName); if (propagatorFactoryFunction == null) { diag.error( diff --git a/metapackages/auto-instrumentations-node/test/utils.test.ts b/metapackages/auto-instrumentations-node/test/utils.test.ts index 66453be63a..82b67017c8 100644 --- a/metapackages/auto-instrumentations-node/test/utils.test.ts +++ b/metapackages/auto-instrumentations-node/test/utils.test.ts @@ -175,6 +175,24 @@ describe('utils', () => { ]); }); + it('should return default when env var is empty', () => { + process.env.OTEL_PROPAGATORS = ''; + assert.deepStrictEqual(getPropagator().fields(), [ + 'traceparent', + 'tracestate', + 'baggage', + ]); + }); + + it('should return default when env var is all spaces', () => { + process.env.OTEL_PROPAGATORS = ' '; + assert.deepStrictEqual(getPropagator().fields(), [ + 'traceparent', + 'tracestate', + 'baggage', + ]); + }); + it('should return the selected propagator when one is in the list', () => { process.env.OTEL_PROPAGATORS = 'tracecontext'; assert.deepStrictEqual(getPropagator().fields(), [ @@ -192,5 +210,10 @@ describe('utils', () => { process.env.OTEL_PROPAGATORS = 'my, unknown, propagators'; assert.deepStrictEqual(getPropagator().fields(), []); }); + + it('should return no-op propagator if "none" is selected', () => { + process.env.OTEL_PROPAGATORS = 'none'; + assert.deepStrictEqual(getPropagator().fields(), []); + }); }); }); From 1dbc8048d2c60aa3383368507a4258a0977721ab Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Tue, 11 Jun 2024 16:32:18 +0200 Subject: [PATCH 7/8] fix: sync package-lock.json --- package-lock.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/package-lock.json b/package-lock.json index bc853e7751..dc539e0ea1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -286,6 +286,7 @@ "version": "0.47.1", "license": "Apache-2.0", "dependencies": { + "@opentelemetry/core": "^1.25.0", "@opentelemetry/instrumentation": "^0.52.0", "@opentelemetry/instrumentation-amqplib": "^0.38.0", "@opentelemetry/instrumentation-aws-lambda": "^0.42.0", @@ -325,6 +326,10 @@ "@opentelemetry/instrumentation-tedious": "^0.11.0", "@opentelemetry/instrumentation-undici": "^0.3.0", "@opentelemetry/instrumentation-winston": "^0.38.0", + "@opentelemetry/propagator-aws-xray": "^1.25.0", + "@opentelemetry/propagator-b3": "^1.25.0", + "@opentelemetry/propagator-jaeger": "^1.25.0", + "@opentelemetry/propagator-ot-trace": "^0.27.2", "@opentelemetry/resource-detector-alibaba-cloud": "^0.28.10", "@opentelemetry/resource-detector-aws": "^1.5.1", "@opentelemetry/resource-detector-azure": "^0.2.9", @@ -50359,6 +50364,7 @@ "version": "file:metapackages/auto-instrumentations-node", "requires": { "@opentelemetry/api": "^1.4.1", + "@opentelemetry/core": "^1.25.0", "@opentelemetry/instrumentation": "^0.52.0", "@opentelemetry/instrumentation-amqplib": "^0.38.0", "@opentelemetry/instrumentation-aws-lambda": "^0.42.0", @@ -50398,6 +50404,10 @@ "@opentelemetry/instrumentation-tedious": "^0.11.0", "@opentelemetry/instrumentation-undici": "^0.3.0", "@opentelemetry/instrumentation-winston": "^0.38.0", + "@opentelemetry/propagator-aws-xray": "^1.25.0", + "@opentelemetry/propagator-b3": "^1.25.0", + "@opentelemetry/propagator-jaeger": "^1.25.0", + "@opentelemetry/propagator-ot-trace": "^0.27.2", "@opentelemetry/resource-detector-alibaba-cloud": "^0.28.10", "@opentelemetry/resource-detector-aws": "^1.5.1", "@opentelemetry/resource-detector-azure": "^0.2.9", From 2f635ffd8b4e7fe1269b113aa5076f3feb2878ed Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Tue, 11 Jun 2024 16:35:11 +0200 Subject: [PATCH 8/8] chore: fix formatting --- metapackages/auto-instrumentations-node/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metapackages/auto-instrumentations-node/package.json b/metapackages/auto-instrumentations-node/package.json index 06c0acdbc0..e1f6b379f9 100644 --- a/metapackages/auto-instrumentations-node/package.json +++ b/metapackages/auto-instrumentations-node/package.json @@ -48,7 +48,8 @@ "ts-mocha": "10.0.0", "typescript": "4.4.4" }, - "dependencies": { "@opentelemetry/instrumentation": "^0.52.0", + "dependencies": { + "@opentelemetry/instrumentation": "^0.52.0", "@opentelemetry/instrumentation-amqplib": "^0.38.0", "@opentelemetry/instrumentation-aws-lambda": "^0.42.0", "@opentelemetry/instrumentation-aws-sdk": "^0.42.0",