Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(otel): fix async resource attribute access issue #667

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/tracing/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<IResource> {
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
Expand Down Expand Up @@ -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', () => {
Expand Down