Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(auto-instrumentations-node): add getPropagator to get propagators from environment #2232

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion metapackages/auto-instrumentations-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@
"@opentelemetry/resource-detector-container": "^0.3.11",
"@opentelemetry/resource-detector-gcp": "^0.29.10",
"@opentelemetry/resources": "^1.24.0",
"@opentelemetry/sdk-node": "^0.52.0"
"@opentelemetry/sdk-node": "^0.52.0",
"@opentelemetry/core": "^1.25.0",
"@opentelemetry/propagator-b3": "^1.25.0",
"@opentelemetry/propagator-jaeger": "^1.25.0",
"@opentelemetry/propagator-aws-xray": "^1.25.0",
"@opentelemetry/propagator-ot-trace": "^0.27.2"
},
"files": [
"build/src/**/*.js",
Expand Down
1 change: 1 addition & 0 deletions metapackages/auto-instrumentations-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
export {
getNodeAutoInstrumentations,
getResourceDetectorsFromEnv as getResourceDetectors,
getPropagator,
InstrumentationConfigMap,
} from './utils';
2 changes: 2 additions & 0 deletions metapackages/auto-instrumentations-node/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as opentelemetry from '@opentelemetry/sdk-node';
import { diag, DiagConsoleLogger } from '@opentelemetry/api';
import {
getNodeAutoInstrumentations,
getPropagator,
getResourceDetectorsFromEnv,
} from './utils';

Expand All @@ -28,6 +29,7 @@ diag.setLogger(
const sdk = new opentelemetry.NodeSDK({
instrumentations: getNodeAutoInstrumentations(),
resourceDetectors: getResourceDetectorsFromEnv(),
textMapPropagator: getPropagator(),
});

try {
Expand Down
71 changes: 70 additions & 1 deletion metapackages/auto-instrumentations-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -253,3 +262,63 @@ export function getResourceDetectorsFromEnv(): Array<Detector | DetectorSync> {
return resourceDetector || [];
});
}

type PropagatorFactoryFunction = () => TextMapPropagator;

const propagatorMap = new Map<string, PropagatorFactoryFunction>([
['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 || process.env.OTEL_PROPAGATORS.trim() === '') {
return new CompositePropagator({
propagators: [
new W3CTraceContextPropagator(),
new W3CBaggagePropagator(),
],
});
}

const propagatorsFromEnv = Array.from(
new Set(
process.env.OTEL_PROPAGATORS?.split(',').map(value =>
value.toLowerCase().trim()
)
)
);

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(
`Invalid propagator "${propagatorName}" specified in the environment variable OTEL_PROPAGATORS`
);
return [];
}
return propagatorFactoryFunction();
});

return new CompositePropagator({ propagators });
}
57 changes: 56 additions & 1 deletion metapackages/auto-instrumentations-node/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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';

describe('utils', () => {
describe('getNodeAutoInstrumentations', () => {
Expand Down Expand Up @@ -161,4 +161,59 @@ 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 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(), [
'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']);
});

it('should return no-op propagator if all propagators are unknown', () => {
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(), []);
});
});
});
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading