Skip to content

Commit 00df13d

Browse files
committed
reorder to baggage,xray,tracecontext
1 parent 61cf2d4 commit 00df13d

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export OTEL_TRACES_SAMPLER_ARG=endpoint=http://localhost:2000,polling_interval=3
3030
| Environment Variable | Description | Example |
3131
| -------------------- | ----------- | ------- |
3232
| `OTEL_EXPORTER_OTLP_PROTOCOL` | Leave unset so that ADOT JS will use a recommended OTLP protocol | `http/protobuf` |
33-
| `OTEL_PROPAGATORS` | Leave unset so that ADOT JS will use a recommended list of propagators | `tracecontext,baggage,xray` |
33+
| `OTEL_PROPAGATORS` | Leave unset so that ADOT JS will use a recommended list of propagators | `baggage,xray,tracecontext` |
3434
| `OTEL_NODE_DISABLED_INSTRUMENTATIONS` | Leave unset so that ADOT JS will disable a recommended list of instrumentations | `fs,dns` |
3535
| `OTEL_NODE_RESOURCE_DETECTORS` | Leave unset so that ADOT JS will use a recommended list of Resource Detectors. If set, `env` should be at the end of the list | `aws,env` |

aws-distro-opentelemetry-node-autoinstrumentation/src/register.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function setAwsDefaultEnvironmentVariables() {
4444
if (!process.env.OTEL_PROPAGATORS) {
4545
// Propagators are run in the order they are configured.
4646
// xray is set after baggage in case xray propagator depends on the result of the baggage header extraction.
47-
process.env.OTEL_PROPAGATORS = 'tracecontext,baggage,xray';
47+
process.env.OTEL_PROPAGATORS = 'baggage,xray,tracecontext';
4848
}
4949
if (!process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS) {
5050
if (isAgentObservabilityEnabled()) {

aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
import { OTLPAwsLogExporter } from '../src/exporter/otlp/aws/logs/otlp-aws-log-exporter';
6464
import { OTLPAwsSpanExporter } from '../src/exporter/otlp/aws/traces/otlp-aws-span-exporter';
6565
import { AwsCloudWatchOtlpBatchLogRecordProcessor } from '../src/exporter/otlp/aws/logs/aws-cw-otlp-batch-log-record-processor';
66+
import { TRACE_PARENT_HEADER } from '@opentelemetry/core';
6667

6768
// Tests AwsOpenTelemetryConfigurator after running Environment Variable setup in register.ts
6869
describe('AwsOpenTelemetryConfiguratorTest', () => {
@@ -178,6 +179,51 @@ describe('AwsOpenTelemetryConfiguratorTest', () => {
178179
expect(spanContext.traceFlags).toEqual(1);
179180
});
180181

182+
it('Default Propagator extraction of Trace Context prioritizes W3C Trace Header over X-Ray Trace Header for span context if they mismatch', () => {
183+
const carrier: Record<string, string> = {
184+
'X-Amzn-Trace-Id':
185+
'Root=1-5759e988-bd862e3fe1bf46a994270000;Parent=53945c3f42cd0000;Sampled=0;Lineage=a87bd80c:1|68fd508a:5|c512fbe3:2',
186+
[TRACE_PARENT_HEADER]: '00-11111111222222223333333344444444-5555555566666666-01',
187+
};
188+
const textMapGetter: TextMapGetter = {
189+
keys: (carrier: Record<string, string>): string[] => {
190+
return Object.keys(carrier);
191+
},
192+
get: (carrier: Record<string, string>, key: string) => {
193+
return carrier?.[key];
194+
},
195+
};
196+
197+
// Create configurator with default settings and AgentObservability enabled for this test case
198+
setAwsDefaultEnvironmentVariables();
199+
const customAwsOtelConfigurator = new AwsOpentelemetryConfigurator([]);
200+
const customAwsOtelConfiguration = customAwsOtelConfigurator.configure();
201+
202+
const tracerProvider: NodeTracerProvider = new NodeTracerProvider(customAwsOtelConfiguration);
203+
const tracer: Tracer = tracerProvider.getTracer('test');
204+
205+
const contextAfterExtraction = customAwsOtelConfiguration.textMapPropagator?.extract(
206+
ROOT_CONTEXT,
207+
carrier,
208+
textMapGetter
209+
);
210+
211+
// Test Trace Context extraction directly
212+
const spanContextAfterExtraction = trace.getSpanContext(contextAfterExtraction!);
213+
expect(spanContextAfterExtraction?.traceId).toEqual('11111111222222223333333344444444');
214+
expect(spanContextAfterExtraction?.spanId).toEqual('5555555566666666');
215+
expect(spanContextAfterExtraction?.traceFlags).toEqual(1);
216+
217+
// Test Trace Context extraction indirectly through span creation
218+
const span: Span = tracer.startSpan('test', undefined, contextAfterExtraction);
219+
span.end();
220+
const spanContext = span.spanContext();
221+
222+
expect(spanContext.traceId).toEqual('11111111222222223333333344444444');
223+
expect((span as any).parentSpanId).toEqual('5555555566666666');
224+
expect(spanContext.traceFlags).toEqual(1);
225+
});
226+
181227
it('Propagator extracts session.id baggage header attribute into to span attributes when Agent Observability is enabled', () => {
182228
// Create configurator with default settings and AgentObservability enabled for this test case
183229
setAwsDefaultEnvironmentVariables();
@@ -712,7 +758,7 @@ describe('AwsOpenTelemetryConfiguratorTest', () => {
712758
function validateConfiguratorEnviron() {
713759
// Set by register.ts
714760
expect(process.env).toHaveProperty('OTEL_EXPORTER_OTLP_PROTOCOL', 'http/protobuf');
715-
expect(process.env).toHaveProperty('OTEL_PROPAGATORS', 'tracecontext,baggage,xray');
761+
expect(process.env).toHaveProperty('OTEL_PROPAGATORS', 'baggage,xray,tracecontext');
716762

717763
// Not set
718764
expect(process.env).not.toHaveProperty('OTEL_TRACES_SAMPLER');

aws-distro-opentelemetry-node-autoinstrumentation/test/register.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('Register', function () {
4646
it('sets AWS Default Environment Variables', () => {
4747
setAwsDefaultEnvironmentVariables();
4848
expect(process.env.OTEL_EXPORTER_OTLP_PROTOCOL).toEqual('http/protobuf');
49-
expect(process.env.OTEL_PROPAGATORS).toEqual('tracecontext,baggage,xray');
49+
expect(process.env.OTEL_PROPAGATORS).toEqual('baggage,xray,tracecontext');
5050
expect(process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS).toEqual('fs,dns');
5151
});
5252

@@ -167,7 +167,7 @@ describe('Register', function () {
167167

168168
assert.ok(proc.stdout.includes('AWS Distro of OpenTelemetry automatic instrumentation started successfully'));
169169
assert.ok(proc.stdout.includes("Environment variable OTEL_EXPORTER_OTLP_PROTOCOL is set to 'http/protobuf'"));
170-
assert.ok(proc.stdout.includes("Environment variable OTEL_PROPAGATORS is set to 'tracecontext,baggage,xray'"));
170+
assert.ok(proc.stdout.includes("Environment variable OTEL_PROPAGATORS is set to 'baggage,xray,tracecontext'"));
171171

172172
// Check a span has been generated for the GET request done in app.js
173173
assert.ok(proc.stdout.includes("name: 'GET'"), 'console span output in stdout - validate Span Name');

0 commit comments

Comments
 (0)