Skip to content

Commit

Permalink
feat(sdk-node): add HostDetector as default resource detector (#4566)
Browse files Browse the repository at this point in the history
* feat(node-sdk): add `HostDetector` as default resource detector

Fixes #4282

- Add `HostDetector` to the list of default resource detectors
- Add test to make sure host and process values are being set by default
- Update from deprecated `sdk.detectResources()` to `sdk.start()` on tests
- Update from deprecated `SemanticResourceAttributes.SERVICE_NAME` to `SEMRESATTRS_SERVICE_NAME` on SDK file

* Update experimental/CHANGELOG.md

* fix lint

remove extra lint causing lint error

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
maryliag and pichlermarc authored Mar 25, 2024
1 parent fec6286 commit 97af8e6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ All notable changes to experimental packages in this project will be documented
### :rocket: (Enhancement)

* refactor(instr-http): use exported strings for semconv. [#4573](https://github.com/open-telemetry/opentelemetry-js/pull/4573/) @JamieDanielson
* feat(sdk-node): add `HostDetector` as default resource detector

### :bug: (Bug Fix)

Expand Down
6 changes: 4 additions & 2 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
DetectorSync,
detectResourcesSync,
envDetector,
hostDetector,
IResource,
processDetector,
Resource,
Expand All @@ -46,7 +47,7 @@ import {
NodeTracerConfig,
NodeTracerProvider,
} from '@opentelemetry/sdk-trace-node';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { NodeSDKConfiguration } from './types';
import { TracerProviderWithEnvExporters } from './TracerProviderWithEnvExporter';
import { getEnv, getEnvWithoutDefaults } from '@opentelemetry/core';
Expand Down Expand Up @@ -123,6 +124,7 @@ export class NodeSDK {
this._resourceDetectors = configuration.resourceDetectors ?? [
envDetector,
processDetector,
hostDetector,
];

this._serviceName = configuration.serviceName;
Expand Down Expand Up @@ -328,7 +330,7 @@ export class NodeSDK {
? this._resource
: this._resource.merge(
new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: this._serviceName,
[SEMRESATTRS_SERVICE_NAME]: this._serviceName,
})
);

Expand Down
40 changes: 35 additions & 5 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { TracerProviderWithEnvExporters } from '../src/TracerProviderWithEnvExpo
import {
envDetector,
processDetector,
hostDetector,
Resource,
} from '@opentelemetry/resources';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
Expand All @@ -66,6 +67,10 @@ import {
InMemoryLogRecordExporter,
LoggerProvider,
} from '@opentelemetry/sdk-logs';
import {
SEMRESATTRS_HOST_NAME,
SEMRESATTRS_PROCESS_PID,
} from '@opentelemetry/semantic-conventions';

const DefaultContextManager = semver.gte(process.version, '14.8.0')
? AsyncLocalStorageContextManager
Expand Down Expand Up @@ -527,9 +532,10 @@ describe('Node SDK', () => {
},
},
envDetector,
hostDetector,
],
});
sdk.detectResources();
sdk.start();
const resource = sdk['_resource'];
await resource.waitForAsyncAttributes?.();

Expand All @@ -544,6 +550,28 @@ describe('Node SDK', () => {
});
});

describe('default resource detectors', () => {
it('default detectors populate values properly', async () => {
const sdk = new NodeSDK();
sdk.start();
const resource = sdk['_resource'];
await resource.waitForAsyncAttributes?.();

assertServiceResource(resource, {
instanceId: '627cc493',
name: 'my-service',
namespace: 'default',
version: '0.0.1',
});

assert.notEqual(
resource.attributes[SEMRESATTRS_PROCESS_PID],
undefined
);
assert.notEqual(resource.attributes[SEMRESATTRS_HOST_NAME], undefined);
});
});

describe('with a buggy detector', () => {
it('returns a merged resource', async () => {
const sdk = new NodeSDK({
Expand All @@ -556,10 +584,11 @@ describe('Node SDK', () => {
},
},
envDetector,
hostDetector,
],
});

sdk.detectResources();
sdk.start();
const resource = sdk['_resource'];
await resource.waitForAsyncAttributes?.();

Expand Down Expand Up @@ -609,7 +638,7 @@ describe('Node SDK', () => {
DiagLogLevel.VERBOSE
);

sdk.detectResources();
sdk.start();
await sdk['_resource'].waitForAsyncAttributes?.();

// Test that the Env Detector successfully found its resource and populated it with the right values.
Expand Down Expand Up @@ -642,7 +671,7 @@ describe('Node SDK', () => {
DiagLogLevel.DEBUG
);

sdk.detectResources();
sdk.start();

assert.ok(
callArgsContains(
Expand Down Expand Up @@ -794,9 +823,10 @@ describe('Node SDK', () => {
},
},
envDetector,
hostDetector,
],
});
sdk.detectResources();
sdk.start();
const resource = sdk['_resource'];
await resource.waitForAsyncAttributes?.();

Expand Down

0 comments on commit 97af8e6

Please sign in to comment.