diff --git a/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/AlibabaCloudEcsDetector.ts b/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/AlibabaCloudEcsDetector.ts index 776e5dbccd..bfcf29e3a2 100644 --- a/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/AlibabaCloudEcsDetector.ts +++ b/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/AlibabaCloudEcsDetector.ts @@ -15,8 +15,10 @@ */ import { - Detector, + DetectorSync, + IResource, Resource, + ResourceAttributes, ResourceDetectionConfig, } from '@opentelemetry/resources'; import { @@ -31,6 +33,7 @@ import { SEMRESATTRS_HOST_NAME, SEMRESATTRS_HOST_TYPE, } from '@opentelemetry/semantic-conventions'; + import * as http from 'http'; /** @@ -38,7 +41,7 @@ import * as http from 'http'; * AlibabaCloud ECS and return a {@link Resource} populated with metadata about * the ECS instance. Returns an empty Resource if detection fails. */ -class AlibabaCloudEcsDetector implements Detector { +class AlibabaCloudEcsDetector implements DetectorSync { /** * See https://www.alibabacloud.com/help/doc-detail/67254.htm for * documentation about the AlibabaCloud instance identity document. @@ -57,7 +60,14 @@ class AlibabaCloudEcsDetector implements Detector { * * @param config (unused) The resource detection config */ - async detect(_config?: ResourceDetectionConfig): Promise { + detect(_config?: ResourceDetectionConfig): IResource { + return new Resource({}, this._getAttributes()); + } + + /** Gets identity and host info and returns them as attribs. Empty object if fails */ + async _getAttributes( + _config?: ResourceDetectionConfig + ): Promise { const { 'owner-account-id': accountId, 'instance-id': instanceId, @@ -67,7 +77,7 @@ class AlibabaCloudEcsDetector implements Detector { } = await this._fetchIdentity(); const hostname = await this._fetchHost(); - return new Resource({ + return { [SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_ALIBABA_CLOUD, [SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_ALIBABA_CLOUD_ECS, [SEMRESATTRS_CLOUD_ACCOUNT_ID]: accountId, @@ -76,7 +86,7 @@ class AlibabaCloudEcsDetector implements Detector { [SEMRESATTRS_HOST_ID]: instanceId, [SEMRESATTRS_HOST_TYPE]: instanceType, [SEMRESATTRS_HOST_NAME]: hostname, - }); + }; } /** diff --git a/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/index.ts b/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/index.ts index 362ed6ecb7..1f43069640 100644 --- a/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/index.ts +++ b/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/index.ts @@ -14,4 +14,4 @@ * limitations under the License. */ -export * from './AlibabaCloudEcsDetector'; +export { alibabaCloudEcsDetector } from './AlibabaCloudEcsDetector'; diff --git a/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/index.ts b/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/index.ts index 0acba8788c..b14741ba21 100644 --- a/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/index.ts +++ b/detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/index.ts @@ -14,4 +14,4 @@ * limitations under the License. */ -export * from './detectors'; +export { alibabaCloudEcsDetector } from './detectors'; diff --git a/detectors/node/opentelemetry-resource-detector-alibaba-cloud/test/detectors/AlibabaCloudEcsDetector.test.ts b/detectors/node/opentelemetry-resource-detector-alibaba-cloud/test/detectors/AlibabaCloudEcsDetector.test.ts index c4a2c41d36..15cd13dd99 100644 --- a/detectors/node/opentelemetry-resource-detector-alibaba-cloud/test/detectors/AlibabaCloudEcsDetector.test.ts +++ b/detectors/node/opentelemetry-resource-detector-alibaba-cloud/test/detectors/AlibabaCloudEcsDetector.test.ts @@ -18,11 +18,11 @@ import * as nock from 'nock'; import * as assert from 'assert'; import { Resource } from '@opentelemetry/resources'; import { CLOUDPROVIDERVALUES_ALIBABA_CLOUD } from '@opentelemetry/semantic-conventions'; -import { alibabaCloudEcsDetector } from '../../src'; import { assertCloudResource, assertHostResource, } from '@opentelemetry/contrib-test-utils'; +import { alibabaCloudEcsDetector } from '../../src'; const ALIYUN_HOST = 'http://' + alibabaCloudEcsDetector.ALIBABA_CLOUD_IDMS_ENDPOINT; @@ -64,6 +64,7 @@ describe('alibabaCloudEcsDetector', () => { .reply(200, () => mockedHostResponse); const resource: Resource = await alibabaCloudEcsDetector.detect(); + await resource.waitForAsyncAttributes?.(); scope.done(); @@ -84,8 +85,7 @@ describe('alibabaCloudEcsDetector', () => { }); describe('with unsuccessful request', () => { - it('should throw when receiving error response code', async () => { - const expectedError = new Error('Failed to load page, status code: 404'); + it('should return empty resource when receiving error response code', async () => { const scope = nock(ALIYUN_HOST) .persist() .get(ALIYUN_IDENTITY_PATH) @@ -93,18 +93,15 @@ describe('alibabaCloudEcsDetector', () => { .get(ALIYUN_HOST_PATH) .reply(404, () => new Error()); - try { - await alibabaCloudEcsDetector.detect(); - assert.ok(false, 'Expected to throw'); - } catch (err) { - assert.deepStrictEqual(err, expectedError); - } + const resource = await alibabaCloudEcsDetector.detect(); + await resource.waitForAsyncAttributes?.(); + + assert.deepStrictEqual(resource.attributes, {}); scope.done(); }); - it('should throw when timed out', async () => { - const expectedError = new Error('ECS metadata api request timed out.'); + it('should return empty resource when timed out', async () => { const scope = nock(ALIYUN_HOST) .get(ALIYUN_IDENTITY_PATH) .reply(200, () => mockedIdentityResponse) @@ -112,28 +109,23 @@ describe('alibabaCloudEcsDetector', () => { .delayConnection(2000) .reply(200, () => mockedHostResponse); - try { - await alibabaCloudEcsDetector.detect(); - assert.ok(false, 'Expected to throw'); - } catch (err) { - assert.deepStrictEqual(err, expectedError); - } + const resource = await alibabaCloudEcsDetector.detect(); + await resource.waitForAsyncAttributes?.(); + + assert.deepStrictEqual(resource.attributes, {}); scope.done(); }); - it('should throw when replied with an Error', async () => { - const expectedError = new Error('NOT FOUND'); + it('should return empty resource when replied with an Error', async () => { const scope = nock(ALIYUN_HOST) .get(ALIYUN_IDENTITY_PATH) - .replyWithError(expectedError.message); - - try { - await alibabaCloudEcsDetector.detect(); - assert.ok(false, 'Expected to throw'); - } catch (err) { - assert.deepStrictEqual(err, expectedError); - } + .replyWithError('NOT FOUND'); + + const resource = await alibabaCloudEcsDetector.detect(); + await resource.waitForAsyncAttributes?.(); + + assert.deepStrictEqual(resource.attributes, {}); scope.done(); }); diff --git a/detectors/node/opentelemetry-resource-detector-instana/package.json b/detectors/node/opentelemetry-resource-detector-instana/package.json index 93d915730e..5ed0ce5dd5 100644 --- a/detectors/node/opentelemetry-resource-detector-instana/package.json +++ b/detectors/node/opentelemetry-resource-detector-instana/package.json @@ -51,7 +51,7 @@ "typescript": "4.4.4" }, "dependencies": { - "@opentelemetry/resources": "^1.0.0", + "@opentelemetry/resources": "^1.10.0", "@opentelemetry/semantic-conventions": "^1.22.0" }, "peerDependencies": { diff --git a/package-lock.json b/package-lock.json index 63775108ae..ffc20fc4fa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -251,7 +251,7 @@ "version": "0.10.0", "license": "Apache-2.0", "dependencies": { - "@opentelemetry/resources": "^1.0.0", + "@opentelemetry/resources": "^1.10.0", "@opentelemetry/semantic-conventions": "^1.22.0" }, "devDependencies": { @@ -55622,7 +55622,7 @@ "requires": { "@opentelemetry/api": "^1.3.0", "@opentelemetry/contrib-test-utils": "^0.40.0", - "@opentelemetry/resources": "^1.0.0", + "@opentelemetry/resources": "^1.10.0", "@opentelemetry/sdk-node": "^0.52.0", "@opentelemetry/semantic-conventions": "^1.22.0", "@types/mocha": "8.2.3",