Skip to content

Commit 0e0feae

Browse files
committed
small cleanup
1 parent 2acbfe1 commit 0e0feae

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/llo-handler.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
// Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
43

5-
import { diag, Attributes, HrTime, ROOT_CONTEXT } from '@opentelemetry/api';
4+
import { diag, Attributes, HrTime, ROOT_CONTEXT, createContextKey } from '@opentelemetry/api';
65
import { LoggerProvider } from '@opentelemetry/sdk-logs';
76
import { EventLoggerProvider } from '@opentelemetry/sdk-events';
87
import { Event } from '@opentelemetry/api-events';
@@ -381,17 +380,26 @@ export class LLOHandler {
381380
const timestamp = eventTimestamp || span.endTime;
382381
const eventLogger = this.eventLoggerProvider.getEventLogger(span.instrumentationLibrary.name);
383382

384-
// Workaround to add a Context to an Event.
385-
// This is needed because a ReadableSpan only provides its SpanContext,
386-
// but does not provide access to the associated Context. An Event can
387-
// have a Context, but not a SpanContext. Here we attempt to attach a
388-
// custom Context that is associated to the ReadableSpan to mimic the
389-
// ReadableSpan's actual Context.
390-
const customContext = ROOT_CONTEXT.setValue(SPAN_KEY, span);
383+
// Hack - Workaround to add a custom-made Context to an Event so that the emitted event log
384+
// has the correct associated traceId and spanId. This is needed because a ReadableSpan only
385+
// provides its SpanContext, but does not provide access to its associated Context. We can only
386+
// provide a Context to an Event, but not a SpanContext. Here we attach a custom Context that
387+
// is associated to the ReadableSpan to mimic the ReadableSpan's actual Context.
388+
//
389+
// When a log record instance is created from this event, it will use the "custom Context" to
390+
// extract the ReadableSpan from the "custom Context", then extract the SpanContext from the
391+
// RedableSpan. This way, the emitted log event has the correct SpanContext (traceId, spanId).
392+
// - https://github.com/open-telemetry/opentelemetry-js/blob/experimental/v0.57.1/experimental/packages/sdk-logs/src/LogRecord.ts#L101
393+
// - https://github.com/open-telemetry/opentelemetry-js/blob/experimental/v0.57.1/api/src/trace/context-utils.ts#L78-L85
394+
//
395+
// We could omit the context field which is optional, but then the OTel EventLogger will assign
396+
// the same `context.active()` and its associated SpanContext to each span from processSpans(),
397+
// which would be incorrect since each span should have their own Context with unique SpanContext.
398+
// - https://github.com/open-telemetry/opentelemetry-js/blob/experimental/v0.57.1/experimental/packages/sdk-events/src/EventLogger.ts#L34
399+
const customContext = ROOT_CONTEXT.setValue(OTEL_SPAN_KEY, span);
391400
const event: Event = {
392401
name: span.instrumentationLibrary.name,
393402
timestamp: timestamp,
394-
attributes: {},
395403
data: eventBody,
396404
context: customContext,
397405
};
@@ -545,15 +553,6 @@ export class LLOHandler {
545553
}
546554
}
547555

548-
// The OpenTelemetry Authors code
549-
export const SPAN_KEY = createContextKey('OpenTelemetry Context Key SPAN');
550-
export function createContextKey(description: string) {
551-
// The specification states that for the same input, multiple calls should
552-
// return different keys. Due to the nature of the JS dependency management
553-
// system, this creates problems where multiple versions of some package
554-
// could hold different keys for the same property.
555-
//
556-
// Therefore, we use Symbol.for which returns the same key for the same input.
557-
return Symbol.for(description);
558-
}
559-
// END The OpenTelemetry Authors code
556+
// Defined by OTel in:
557+
// - https://github.com/open-telemetry/opentelemetry-js/blob/v1.9.0/api/src/trace/context-utils.ts#L24-L27
558+
export const OTEL_SPAN_KEY = createContextKey('OpenTelemetry Context Key SPAN');

aws-distro-opentelemetry-node-autoinstrumentation/test/llo-handler.events.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as sinon from 'sinon';
77
import { Event } from '@opentelemetry/api-events';
88
import { TimedEvent } from '@opentelemetry/sdk-trace-base';
99
import { InstrumentationScope } from '@opentelemetry/core';
10-
import { SPAN_KEY } from '../src/llo-handler';
10+
import { OTEL_SPAN_KEY } from '../src/llo-handler';
1111
import { Attributes, HrTime } from '@opentelemetry/api';
1212

1313
/**
@@ -53,7 +53,7 @@ describe('TestLLOHandlerEvents', () => {
5353

5454
expect(emittedEvent.name).toBe('test.scope');
5555
expect(emittedEvent.timestamp).toEqual(span.endTime);
56-
expect(emittedEvent.context?.getValue(SPAN_KEY)).toBe(span);
56+
expect(emittedEvent.context?.getValue(OTEL_SPAN_KEY)).toBe(span);
5757
expect(emittedEvent.data).toBeDefined();
5858

5959
const eventBody = emittedEvent.data as any;

0 commit comments

Comments
 (0)