open-telemetryotelopentelemetry-js

How to retrieve resource attributes whilst using OpenTelemetry Node.js SDK?


The following code is inside a instrumentation.js file:

const { NodeSDK } = require('@opentelemetry/sdk-node');
const sdk = new NodeSDK({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: 'xyz',
    [SemanticResourceAttributes.SERVICE_VERSION]: '1.2.3',
  }),
  ...
}
sdk.start();

When running applications in production, a flag to --require the instrumentation.js is added to the node command, as outlined in the Getting Started > Node.js documentation:

$ node --require ./instrumentation.js app.js

Application developers currently have no access to this instrumentation.js file.

Is there a way for application developers to retrieve the resource attributes from within their applications (app.js) for logging, without access to instrumentation.js?


  1. If I have a reference to the resource, I can get the attributes using resource.attributes. But how do I get a reference to that resource within the application?
  2. @opentelemetry/api provides a getTracerProvider() method to get the current globally-registered TracerProvider. Is there an equivalent getResource()?

Solution

  • Got a reply from one of the maintainers of opentelemetry-js:

    As of today, the OpenTelemetry API specification does not include a way to retrieve the resource, therefore this is also not exposed in the JavaScript OTel API.