open-telemetry / opentelemetry-js

OpenTelemetry JavaScript Client
https://opentelemetry.io
Apache License 2.0
2.56k stars 743 forks source link

How to handle "Accessing resource attributes before async attributes settled" errors #4638

Open sgohlke opened 2 months ago

sgohlke commented 2 months ago

In the logs of one of our applications I see the following error from time to time: Accessing resource attributes before async attributes settled I took a look into https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-resources/src/Resource.ts#L98 which I assume is the code line that writes this error.

I'm not sure what to do with this error. Is is necessary to take an action or can this error be ignored? There isn't really much information about the origin of this error so I'm not sure how to find the root problem/how to debug this.

We are using the following packages:

In our instrumentation create a new SDK using the following resource:

resource: Resource.default().merge(
                new Resource({
                    [SEMRESATTRS_SERVICE_NAME]: 'our-service',
                })
            )

Maybe someone has an idea how to handle this.

tmokmss commented 2 months ago

I'm also seeing this message. My otel config is here: https://gist.github.com/tmokmss/b6b0d639658161cd28519a65992866f9

packages versions are same as the OP.

timosaikkonen commented 1 month ago

Encountered the same issue and wrote a simple workaround while awaiting the actual fix to land:

import {
  Detector,
  DetectorSync,
  IResource,
  ResourceDetectionConfig,
  envDetectorSync,
  hostDetectorSync,
  processDetectorSync,
} from "@opentelemetry/resources"

function awaitAttributes(detector: DetectorSync): Detector {
  return {
    async detect(config?: ResourceDetectionConfig): Promise<IResource> {
      const resource = detector.detect(config)
      await resource.waitForAsyncAttributes?.()

      return resource
    },
  }
}

const sdk = new opentelemetry.NodeSDK({
  // ...
  resourceDetectors: [
    awaitAttributes(envDetectorSync),
    awaitAttributes(processDetectorSync),
    awaitAttributes(hostDetectorSync),
  ],
})
Billmike commented 1 month ago

@timosaikkonen Your solution was really helpful!