Skip to content

Commit 981c0f1

Browse files
authored
Merge 91921be into dc44b86
2 parents dc44b86 + 91921be commit 981c0f1

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
1010

1111
* feat(ConsoleSpanExporter): export span links [#2917](https://github.com/open-telemetry/opentelemetry-js/pull/2917) @trentm
1212
* feat: warn when hooked module is already loaded [#2926](https://github.com/open-telemetry/opentelemetry-js/pull/2926) @nozik
13+
* feat: implement HostDetector [#2921](https://github.com/open-telemetry/opentelemetry-js/pull/2921) @rauno56
1314

1415
### :bug: (Bug Fix)
1516

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
18+
import { Resource } from '../Resource';
19+
import { Detector, ResourceAttributes } from '../types';
20+
import { ResourceDetectionConfig } from '../config';
21+
import { arch, hostname } from 'os';
22+
23+
/**
24+
* HostDetector detects the resources related to the host current process is
25+
* running on. Currently only non-cloud-based attributes are included.
26+
*/
27+
class HostDetector implements Detector {
28+
async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
29+
const attributes: ResourceAttributes = {
30+
[SemanticResourceAttributes.HOST_NAME]: hostname(),
31+
[SemanticResourceAttributes.HOST_ARCH]: this._normalizeArch(arch()),
32+
};
33+
return new Resource(attributes);
34+
}
35+
36+
private _normalizeArch(nodeArchString: string): string {
37+
// Maps from https://nodejs.org/api/os.html#osarch to arch values in spec:
38+
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/host.md
39+
switch (nodeArchString) {
40+
case 'arm':
41+
return 'arm32';
42+
case 'ppc':
43+
return 'ppc32';
44+
case 'x64':
45+
return 'amd64';
46+
default:
47+
return nodeArchString;
48+
}
49+
}
50+
}
51+
52+
export const hostDetector = new HostDetector();

packages/opentelemetry-resources/src/detectors/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
export * from './BrowserDetector';
1718
export * from './EnvDetector';
19+
export * from './HostDetector';
1820
export * from './ProcessDetector';
19-
export * from './BrowserDetector';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as sinon from 'sinon';
18+
import * as assert from 'assert';
19+
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
20+
import { describeNode } from '../../util';
21+
import { hostDetector, Resource } from '../../../src';
22+
23+
describeNode('hostDetector() on Node.js', () => {
24+
afterEach(() => {
25+
sinon.restore();
26+
});
27+
28+
it('should return resource information about the host', async () => {
29+
const os = require('os');
30+
31+
sinon.stub(os, 'arch').returns('x64');
32+
sinon.stub(os, 'hostname').returns('opentelemetry-test');
33+
34+
const resource: Resource = await hostDetector.detect();
35+
36+
assert.strictEqual(
37+
resource.attributes[SemanticResourceAttributes.HOST_NAME],
38+
'opentelemetry-test'
39+
);
40+
assert.strictEqual(
41+
resource.attributes[SemanticResourceAttributes.HOST_ARCH],
42+
'amd64'
43+
);
44+
});
45+
46+
it('should pass through arch string if unknown', async () => {
47+
const os = require('os');
48+
49+
sinon.stub(os, 'arch').returns('some-unknown-arch');
50+
51+
const resource: Resource = await hostDetector.detect();
52+
53+
assert.strictEqual(
54+
resource.attributes[SemanticResourceAttributes.HOST_ARCH],
55+
'some-unknown-arch'
56+
);
57+
});
58+
});

0 commit comments

Comments
 (0)