Skip to content

Commit

Permalink
feat: detect host id for non-cloud environments
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Jan 27, 2023
1 parent e0abcc0 commit 89544cb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

## Unreleased

* feat: collect host id for non-cloud environments [#3575](https://github.com/open-telemetry/opentelemetry-js/pull/3575) @mwear

### :boom: Breaking Change

### :rocket: (Enhancement)
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
},
"dependencies": {
"@opentelemetry/core": "1.9.0",
"@opentelemetry/semantic-conventions": "1.9.0"
"@opentelemetry/semantic-conventions": "1.9.0",
"node-machine-id": "^1.1.12"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-resources",
"sideEffects": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
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 { machineId } from 'node-machine-id';

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

try {
attributes[SemanticResourceAttributes.HOST_ID] = await machineId(true);
} catch (err) {
diag.warn(`error reading machine id: ${err}`);
}

return new Resource(attributes);
}

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

it('should return resource information about the host', async () => {
const os = require('os');
const mach = require('node-machine-id');

sinon.stub(os, 'arch').returns('x64');
sinon.stub(os, 'hostname').returns('opentelemetry-test');
sinon.stub(mach, 'machineId').returns(Promise.resolve('123-abc'));

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

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

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

it('should handle errors reading machine id', async () => {
const os = require('os');
const mach = require('node-machine-id');

sinon.stub(os, 'arch').returns('x64');
sinon.stub(os, 'hostname').returns('opentelemetry-test');
sinon.stub(mach, 'machineId').rejects(new Error('id not found'));

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 89544cb

Please sign in to comment.