From 92dbf08053545b82cf674ac57c3e3528d9dae0f5 Mon Sep 17 00:00:00 2001 From: Kat Schelonka Date: Mon, 22 Jul 2024 15:05:11 -0700 Subject: [PATCH] fix(otel): fix async resource attribute access issue https://github.com/open-telemetry/opentelemetry-js/issues/4638 [POCKET-10371] --- packages/tracing/src/tracing.ts | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packages/tracing/src/tracing.ts b/packages/tracing/src/tracing.ts index bfcab80e7..050e21c95 100644 --- a/packages/tracing/src/tracing.ts +++ b/packages/tracing/src/tracing.ts @@ -47,6 +47,16 @@ import { } from '@sentry/opentelemetry'; import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'; +import { + Detector, + DetectorSync, + IResource, + ResourceDetectionConfig, + envDetectorSync, + hostDetectorSync, + processDetectorSync, +} from '@opentelemetry/resources'; + import * as Sentry from '@sentry/node'; // instrumentations available to be added by implementing services @@ -94,6 +104,25 @@ const SentryContextManager = wrapContextManagerClass( AsyncLocalStorageContextManager, ); +// TODO: Remove after issue is fixed +// https://github.com/open-telemetry/opentelemetry-js/issues/4638 +/** + * A detector that returns attributes from the environment. + */ +function awaitAttributes(detector: DetectorSync): Detector { + return { + /** + * A function that returns a promise that resolves with the attributes + */ + async detect(config: ResourceDetectionConfig): Promise { + const resource = detector.detect(config); + await resource.waitForAsyncAttributes?.(); + + return resource; + }, + }; +} + /** * function to setup open-telemetry tracing config * Note: this function has to run before initial @@ -173,10 +202,18 @@ export async function nodeSDKBuilder(config: TracingConfig) { spanProcessors: _spanProcessors, traceExporter: _traceExporter, idGenerator: _idGenerator, + // TODO: Remove after issue is fixed + // https://github.com/open-telemetry/opentelemetry-js/issues/4638 + resourceDetectors: [ + awaitAttributes(envDetectorSync), + awaitAttributes(hostDetectorSync), + awaitAttributes(processDetectorSync), + ], }); // this enables the API to record telemetry sdk.start(); + diag.info('Tracer successfully started'); // gracefully shut down the SDK on process exit process.on('SIGTERM', () => {