From 89f85c867c77d31e10bc0fc14418ff3c4ed65edb Mon Sep 17 00:00:00 2001 From: maryliag Date: Wed, 3 Apr 2024 15:15:42 -0400 Subject: [PATCH] feat(resource-detector-alibaba): add `service.instance.id` to resource detection `service.instance.id` is a recommended value. This commit adds it to the alibaba resource detector. It uses the `serial-number` value from the instance metadata, which has the description: ``` The serial number of the instance. Example: 4acd2b47-b328-4762-852f-998**** ``` Which alignes with the definition for `service.instance.id` from [here](https://github.com/open-telemetry/semantic-conventions/pull/312) Part Of #2065 Signed-off-by: maryliag --- .../src/detectors/AlibabaCloudEcsDetector.ts | 5 +++- .../detectors/AlibabaCloudEcsDetector.test.ts | 4 ++++ .../src/resource-assertions.ts | 24 +++++++++++-------- 3 files changed, 22 insertions(+), 11 deletions(-) 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..92c783559f 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 @@ -30,6 +30,7 @@ import { SEMRESATTRS_HOST_ID, SEMRESATTRS_HOST_NAME, SEMRESATTRS_HOST_TYPE, + SEMRESATTRS_SERVICE_INSTANCE_ID, } from '@opentelemetry/semantic-conventions'; import * as http from 'http'; @@ -59,10 +60,11 @@ class AlibabaCloudEcsDetector implements Detector { */ async detect(_config?: ResourceDetectionConfig): Promise { const { - 'owner-account-id': accountId, 'instance-id': instanceId, 'instance-type': instanceType, + 'owner-account-id': accountId, 'region-id': region, + 'serial-number': serviceInstanceId, 'zone-id': availabilityZone, } = await this._fetchIdentity(); const hostname = await this._fetchHost(); @@ -76,6 +78,7 @@ class AlibabaCloudEcsDetector implements Detector { [SEMRESATTRS_HOST_ID]: instanceId, [SEMRESATTRS_HOST_TYPE]: instanceType, [SEMRESATTRS_HOST_NAME]: hostname, + [SEMRESATTRS_SERVICE_INSTANCE_ID]: serviceInstanceId, }); } 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..b9ad79b7b3 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 @@ -22,6 +22,7 @@ import { alibabaCloudEcsDetector } from '../../src'; import { assertCloudResource, assertHostResource, + assertServiceResource, } from '@opentelemetry/contrib-test-utils'; const ALIYUN_HOST = @@ -80,6 +81,9 @@ describe('alibabaCloudEcsDetector', () => { hostType: 'my-instance-type', name: 'my-hostname', }); + assertServiceResource(resource, { + instanceId: 'my-instance-serial-number', + }) }); }); diff --git a/packages/opentelemetry-test-utils/src/resource-assertions.ts b/packages/opentelemetry-test-utils/src/resource-assertions.ts index eb248d41d8..9c38a21116 100644 --- a/packages/opentelemetry-test-utils/src/resource-assertions.ts +++ b/packages/opentelemetry-test-utils/src/resource-assertions.ts @@ -261,20 +261,24 @@ export const assertTelemetrySDKResource = ( export const assertServiceResource = ( resource: Resource, validations: { - name: string; - instanceId: string; + name?: string; + instanceId?: string; namespace?: string; version?: string; } ) => { - assert.strictEqual( - resource.attributes[SEMRESATTRS_SERVICE_NAME], - validations.name - ); - assert.strictEqual( - resource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID], - validations.instanceId - ); + if (validations.name) { + assert.strictEqual( + resource.attributes[SEMRESATTRS_SERVICE_NAME], + validations.name + ); + } + if (validations.instanceId) { + assert.strictEqual( + resource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID], + validations.instanceId + ); + } if (validations.namespace) assert.strictEqual( resource.attributes[SEMRESATTRS_SERVICE_NAMESPACE],