|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | | -// Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License. |
4 | 3 |
|
5 | | -import { diag, Attributes, HrTime, ROOT_CONTEXT } from '@opentelemetry/api'; |
| 4 | +import { diag, Attributes, HrTime, ROOT_CONTEXT, createContextKey } from '@opentelemetry/api'; |
6 | 5 | import { LoggerProvider } from '@opentelemetry/sdk-logs'; |
7 | 6 | import { EventLoggerProvider } from '@opentelemetry/sdk-events'; |
8 | 7 | import { Event } from '@opentelemetry/api-events'; |
@@ -381,17 +380,26 @@ export class LLOHandler { |
381 | 380 | const timestamp = eventTimestamp || span.endTime; |
382 | 381 | const eventLogger = this.eventLoggerProvider.getEventLogger(span.instrumentationLibrary.name); |
383 | 382 |
|
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); |
391 | 400 | const event: Event = { |
392 | 401 | name: span.instrumentationLibrary.name, |
393 | 402 | timestamp: timestamp, |
394 | | - attributes: {}, |
395 | 403 | data: eventBody, |
396 | 404 | context: customContext, |
397 | 405 | }; |
@@ -545,15 +553,6 @@ export class LLOHandler { |
545 | 553 | } |
546 | 554 | } |
547 | 555 |
|
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'); |
0 commit comments