Skip to content

Commit

Permalink
feat: detect host.id in HostDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Feb 10, 2023
1 parent 10a6534 commit 6dcb998
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { Resource } from '../../Resource';
import { Detector, ResourceAttributes } from '../../types';
import { ResourceDetectionConfig } from '../../config';
import { arch, hostname } from 'os';
import { getMachineId } from './machine-id/getMachineId';

/**
* HostDetector detects the resources related to the host current process is
Expand All @@ -30,6 +30,12 @@ class HostDetector implements Detector {
[SemanticResourceAttributes.HOST_NAME]: hostname(),
[SemanticResourceAttributes.HOST_ARCH]: this._normalizeArch(arch()),
};

const machineId = await getMachineId();
if (machineId) {
attributes[SemanticResourceAttributes.HOST_ID] = machineId;
}

return new Resource(attributes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ describeNode('hostDetector() on Node.js', () => {

it('should return resource information about the host', async () => {
const os = require('os');
const mid = require('../../../src/platform/node/machine-id/getMachineId');

const expectedHostId = 'f2c668b579780554f70f72a063dc0864';

sinon.stub(os, 'arch').returns('x64');
sinon.stub(os, 'hostname').returns('opentelemetry-test');
sinon.stub(mid, 'getMachineId').returns(Promise.resolve(expectedHostId));

const resource: Resource = await hostDetector.detect();

Expand All @@ -41,6 +45,10 @@ describeNode('hostDetector() on Node.js', () => {
resource.attributes[SemanticResourceAttributes.HOST_ARCH],
'amd64'
);
assert.strictEqual(
resource.attributes[SemanticResourceAttributes.HOST_ID],
expectedHostId
);
});

it('should pass through arch string if unknown', async () => {
Expand All @@ -55,4 +63,28 @@ describeNode('hostDetector() on Node.js', () => {
'some-unknown-arch'
);
});

it('should handle missing machine id', async () => {
const os = require('os');
const mid = require('../../../src/platform/node/machine-id/getMachineId');

sinon.stub(os, 'arch').returns('x64');
sinon.stub(os, 'hostname').returns('opentelemetry-test');
sinon.stub(mid, 'getMachineId').returns(Promise.resolve(''));

const resource: Resource = await hostDetector.detect();

assert.strictEqual(
resource.attributes[SemanticResourceAttributes.HOST_NAME],
'opentelemetry-test'
);
assert.strictEqual(
resource.attributes[SemanticResourceAttributes.HOST_ARCH],
'amd64'
);
assert.strictEqual(
false,
SemanticResourceAttributes.HOST_ID in resource.attributes
);
});
});

0 comments on commit 6dcb998

Please sign in to comment.