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

feat(sdk-node): add HostDetector as default resource detector #4566

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(node-sdk): add `HostDetector` as default resource detector
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved

### :bug: (Bug Fix)

* fix(exporter-*-otlp-*): use parseHeaders() to ensure header-values are not 'undefined' #4540
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was solely to resolve a deprecation warning, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right!

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