33// Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
44
55import { TextMapPropagator , diag } from '@opentelemetry/api' ;
6- import { getPropagator } from '@opentelemetry/auto-configuration-propagators' ;
76import { getResourceDetectors as getResourceDetectorsFromEnv } from '@opentelemetry/auto-instrumentations-node' ;
8- import { ENVIRONMENT , TracesSamplerValues , getEnv , getEnvWithoutDefaults } from '@opentelemetry/core' ;
7+ import { CompositePropagator , ENVIRONMENT , TracesSamplerValues , getEnv , getEnvWithoutDefaults , W3CBaggagePropagator , W3CTraceContextPropagator } from '@opentelemetry/core' ;
98import { OTLPMetricExporter as OTLPGrpcOTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc' ;
109import {
1110 AggregationTemporalityPreference ,
@@ -17,6 +16,8 @@ import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/expo
1716import { ZipkinExporter } from '@opentelemetry/exporter-zipkin' ;
1817import { AWSXRayIdGenerator } from '@opentelemetry/id-generator-aws-xray' ;
1918import { Instrumentation } from '@opentelemetry/instrumentation' ;
19+ import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray' ;
20+ import { AWSXRayLambdaPropagator } from '@opentelemetry/propagator-aws-xray-lambda' ;
2021import { awsEc2DetectorSync , awsEcsDetectorSync , awsEksDetectorSync } from '@opentelemetry/resource-detector-aws' ;
2122import {
2223 Detector ,
@@ -75,6 +76,13 @@ const FORMAT_OTEL_UNSAMPLED_TRACES_BINARY_PREFIX = 'T1U';
7576// Follow Python SDK Impl to set the max span batch size
7677// which will reduce the chance of UDP package size is larger than 64KB
7778const LAMBDA_SPAN_EXPORT_BATCH_SIZE = 10 ;
79+
80+ const propagatorMap = new Map < string , ( ) => TextMapPropagator > ( [
81+ [ 'tracecontext' , ( ) => new W3CTraceContextPropagator ( ) ] ,
82+ [ 'baggage' , ( ) => new W3CBaggagePropagator ( ) ] ,
83+ [ 'xray' , ( ) => new AWSXRayPropagator ( ) ] ,
84+ [ 'xray-lambda' , ( ) => new AWSXRayLambdaPropagator ( ) ] ,
85+ ] ) ;
7886/**
7987 * Aws Application Signals Config Provider creates a configuration object that can be provided to
8088 * the OTel NodeJS SDK for Auto Instrumentation with Application Signals Functionality.
@@ -163,7 +171,7 @@ export class AwsOpentelemetryConfigurator {
163171 this . resource = autoResource ;
164172
165173 this . instrumentations = instrumentations ;
166- this . propagator = getPropagator ( ) ;
174+ this . propagator = this . getPropagator ( ) ;
167175
168176 // TODO: Consider removing AWSXRayIdGenerator as it is not needed
169177 // Similarly to Java, always use AWS X-Ray Id Generator
@@ -190,6 +198,44 @@ export class AwsOpentelemetryConfigurator {
190198 return autoResource ;
191199 }
192200
201+ private getPropagator ( ) : TextMapPropagator {
202+ if (
203+ process . env . OTEL_PROPAGATORS == null ||
204+ process . env . OTEL_PROPAGATORS . trim ( ) === ''
205+ ) {
206+ return new CompositePropagator ( {
207+ propagators : [
208+ new W3CTraceContextPropagator ( ) ,
209+ new W3CBaggagePropagator ( ) ,
210+ ] ,
211+ } ) ;
212+ }
213+ const propagatorsFromEnv = Array . from (
214+ new Set (
215+ process . env . OTEL_PROPAGATORS ?. split ( ',' ) . map ( value =>
216+ value . toLowerCase ( ) . trim ( ) ,
217+ ) ,
218+ ) ,
219+ ) ;
220+ const propagators = propagatorsFromEnv . flatMap ( propagatorName => {
221+ if ( propagatorName === 'none' ) {
222+ diag . info (
223+ 'Not selecting any propagator for value "none" specified in the environment variable OTEL_PROPAGATORS' ,
224+ ) ;
225+ return [ ] ;
226+ }
227+ const propagatorFactoryFunction = propagatorMap . get ( propagatorName ) ;
228+ if ( propagatorFactoryFunction == null ) {
229+ diag . warn (
230+ `Invalid propagator "${ propagatorName } " specified in the environment variable OTEL_PROPAGATORS` ,
231+ ) ;
232+ return [ ] ;
233+ }
234+ return propagatorFactoryFunction ( ) ;
235+ } ) ;
236+ return new CompositePropagator ( { propagators } ) ;
237+ }
238+
193239 public configure ( ) : Partial < NodeSDKConfiguration > {
194240 // config.autoDetectResources is set to False, as the resources are detected and added to the
195241 // resource ahead of time. The resource is needed to be populated ahead of time instead of letting
0 commit comments