node-sdk-resource.ts 980 B

1234567891011121314151617181920212223242526272829303132
  1. import type { Resource } from '@opentelemetry/resources';
  2. import type { NodeSDK } from '@opentelemetry/sdk-node';
  3. /**
  4. * Get resource from SDK instance
  5. * Note: This uses internal API of NodeSDK
  6. */
  7. export const getResource = (sdk: NodeSDK): Resource => {
  8. // This cast is necessary as _resource is a private property
  9. const resource = (sdk as any)._resource;
  10. if (!resource || typeof resource !== 'object' || !resource.attributes) {
  11. throw new Error('Failed to access SDK resource');
  12. }
  13. return resource;
  14. };
  15. /**
  16. * Set resource to SDK instance
  17. * Note: This uses internal API of NodeSDK
  18. * @throws Error if resource cannot be set
  19. */
  20. export const setResource = (sdk: NodeSDK, resource: Resource): void => {
  21. // Verify that we can access the _resource property
  22. try {
  23. getResource(sdk);
  24. } catch (e) {
  25. throw new Error('Failed to access SDK resource');
  26. }
  27. // This cast is necessary as _resource is a private property
  28. (sdk as any)._resource = resource;
  29. };