Skip to content

Commit

Permalink
feat(resource-detector-alibaba): add service.instance.id to resourc…
Browse files Browse the repository at this point in the history
…e 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](open-telemetry/semantic-conventions#312)

Part Of open-telemetry#2065

Signed-off-by: maryliag <marylia.gutierrez@grafana.com>
  • Loading branch information
maryliag committed Apr 3, 2024
1 parent 421f1b4 commit 89f85c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -59,10 +60,11 @@ class AlibabaCloudEcsDetector implements Detector {
*/
async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
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();
Expand All @@ -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,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { alibabaCloudEcsDetector } from '../../src';
import {
assertCloudResource,
assertHostResource,
assertServiceResource,
} from '@opentelemetry/contrib-test-utils';

const ALIYUN_HOST =
Expand Down Expand Up @@ -80,6 +81,9 @@ describe('alibabaCloudEcsDetector', () => {
hostType: 'my-instance-type',
name: 'my-hostname',
});
assertServiceResource(resource, {
instanceId: 'my-instance-serial-number',
})
});
});

Expand Down
24 changes: 14 additions & 10 deletions packages/opentelemetry-test-utils/src/resource-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 89f85c8

Please sign in to comment.