Skip to content

Commit 4777b71

Browse files
committed
resource-detector-alibaba-cloud
1 parent 3f91296 commit 4777b71

File tree

5 files changed

+99
-42
lines changed

5 files changed

+99
-42
lines changed

detectors/node/opentelemetry-resource-detector-alibaba-cloud/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"author": "OpenTelemetry Authors",
2929
"license": "Apache-2.0",
3030
"engines": {
31-
"node": ">=14"
31+
"node": "^18.19.0 || >=20.6.0"
3232
},
3333
"files": [
3434
"build/src/**/*.js",
@@ -57,8 +57,8 @@
5757
"@opentelemetry/api": "^1.0.0"
5858
},
5959
"dependencies": {
60-
"@opentelemetry/core": "^1.26.0",
61-
"@opentelemetry/resources": "^1.10.0",
60+
"@opentelemetry/core": "^2.0.0-dev.0",
61+
"@opentelemetry/resources": "^2.0.0-dev.0",
6262
"@opentelemetry/semantic-conventions": "^1.27.0"
6363
},
6464
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-alibaba-cloud#readme",

detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/AlibabaCloudEcsDetector.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
import { context } from '@opentelemetry/api';
1818
import { suppressTracing } from '@opentelemetry/core';
1919
import {
20-
DetectorSync,
21-
IResource,
22-
Resource,
23-
ResourceAttributes,
24-
ResourceDetectionConfig,
20+
ResourceDetector,
21+
DetectedResource,
22+
DetectedResourceAttributes,
2523
} from '@opentelemetry/resources';
2624
import {
2725
CLOUDPLATFORMVALUES_ALIBABA_CLOUD_ECS,
@@ -43,7 +41,7 @@ import * as http from 'http';
4341
* AlibabaCloud ECS and return a {@link Resource} populated with metadata about
4442
* the ECS instance. Returns an empty Resource if detection fails.
4543
*/
46-
class AlibabaCloudEcsDetector implements DetectorSync {
44+
class AlibabaCloudEcsDetector implements ResourceDetector {
4745
/**
4846
* See https://www.alibabacloud.com/help/doc-detail/67254.htm for
4947
* documentation about the AlibabaCloud instance identity document.
@@ -62,35 +60,38 @@ class AlibabaCloudEcsDetector implements DetectorSync {
6260
*
6361
* @param config (unused) The resource detection config
6462
*/
65-
detect(_config?: ResourceDetectionConfig): IResource {
63+
detect(): DetectedResource {
6664
const attributes = context.with(suppressTracing(context.active()), () =>
6765
this._getAttributes()
6866
);
69-
return new Resource({}, attributes);
67+
return { attributes };
7068
}
7169

7270
/** Gets identity and host info and returns them as attribs. Empty object if fails */
73-
async _getAttributes(
74-
_config?: ResourceDetectionConfig
75-
): Promise<ResourceAttributes> {
76-
const {
77-
'owner-account-id': accountId,
78-
'instance-id': instanceId,
79-
'instance-type': instanceType,
80-
'region-id': region,
81-
'zone-id': availabilityZone,
82-
} = await this._fetchIdentity();
83-
const hostname = await this._fetchHost();
71+
_getAttributes(): DetectedResourceAttributes {
72+
const dataP = Promise.all([this._fetchIdentity(), this._fetchHost()]);
8473

8574
return {
86-
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_ALIBABA_CLOUD,
87-
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_ALIBABA_CLOUD_ECS,
88-
[SEMRESATTRS_CLOUD_ACCOUNT_ID]: accountId,
89-
[SEMRESATTRS_CLOUD_REGION]: region,
90-
[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: availabilityZone,
91-
[SEMRESATTRS_HOST_ID]: instanceId,
92-
[SEMRESATTRS_HOST_TYPE]: instanceType,
93-
[SEMRESATTRS_HOST_NAME]: hostname,
75+
[SEMRESATTRS_CLOUD_PROVIDER]: dataP.then(
76+
() => CLOUDPROVIDERVALUES_ALIBABA_CLOUD
77+
),
78+
[SEMRESATTRS_CLOUD_PLATFORM]: dataP.then(
79+
() => CLOUDPLATFORMVALUES_ALIBABA_CLOUD_ECS
80+
),
81+
82+
// Data from _fetchIdentity()
83+
[SEMRESATTRS_CLOUD_ACCOUNT_ID]: dataP.then(
84+
data => data[0]['owner-account-id']
85+
),
86+
[SEMRESATTRS_CLOUD_REGION]: dataP.then(data => data[0]['region-id']),
87+
[SEMRESATTRS_CLOUD_AVAILABILITY_ZONE]: dataP.then(
88+
data => data[0]['zone-id']
89+
),
90+
[SEMRESATTRS_HOST_ID]: dataP.then(data => data[0]['instance-id']),
91+
[SEMRESATTRS_HOST_TYPE]: dataP.then(data => data[0]['instance-type']),
92+
93+
// Data from _fetchHost()
94+
[SEMRESATTRS_HOST_NAME]: dataP.then(data => data[1]),
9495
};
9596
}
9697

detectors/node/opentelemetry-resource-detector-alibaba-cloud/test/detectors/AlibabaCloudEcsDetector.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import * as nock from 'nock';
1818
import * as assert from 'assert';
19-
import { Resource } from '@opentelemetry/resources';
19+
import { detectResources } from '@opentelemetry/resources';
2020
import { CLOUDPROVIDERVALUES_ALIBABA_CLOUD } from '@opentelemetry/semantic-conventions';
2121
import {
2222
assertCloudResource,
@@ -63,7 +63,9 @@ describe('alibabaCloudEcsDetector', () => {
6363
.get(ALIYUN_HOST_PATH)
6464
.reply(200, () => mockedHostResponse);
6565

66-
const resource: Resource = await alibabaCloudEcsDetector.detect();
66+
const resource = detectResources({
67+
detectors: [alibabaCloudEcsDetector],
68+
});
6769
await resource.waitForAsyncAttributes?.();
6870

6971
scope.done();
@@ -93,7 +95,9 @@ describe('alibabaCloudEcsDetector', () => {
9395
.get(ALIYUN_HOST_PATH)
9496
.reply(404, () => new Error());
9597

96-
const resource = await alibabaCloudEcsDetector.detect();
98+
const resource = detectResources({
99+
detectors: [alibabaCloudEcsDetector],
100+
});
97101
await resource.waitForAsyncAttributes?.();
98102

99103
assert.deepStrictEqual(resource.attributes, {});
@@ -109,7 +113,9 @@ describe('alibabaCloudEcsDetector', () => {
109113
.delayConnection(2000)
110114
.reply(200, () => mockedHostResponse);
111115

112-
const resource = await alibabaCloudEcsDetector.detect();
116+
const resource = detectResources({
117+
detectors: [alibabaCloudEcsDetector],
118+
});
113119
await resource.waitForAsyncAttributes?.();
114120

115121
assert.deepStrictEqual(resource.attributes, {});
@@ -122,7 +128,9 @@ describe('alibabaCloudEcsDetector', () => {
122128
.get(ALIYUN_IDENTITY_PATH)
123129
.replyWithError('NOT FOUND');
124130

125-
const resource = await alibabaCloudEcsDetector.detect();
131+
const resource = detectResources({
132+
detectors: [alibabaCloudEcsDetector],
133+
});
126134
await resource.waitForAsyncAttributes?.();
127135

128136
assert.deepStrictEqual(resource.attributes, {});

detectors/node/opentelemetry-resource-detector-alibaba-cloud/test/detectors/AlibabaCloudEcsDetectorIntegration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
SimpleSpanProcessor,
2323
} from '@opentelemetry/sdk-trace-base';
2424
import { NodeSDK } from '@opentelemetry/sdk-node';
25-
import { IResource } from '@opentelemetry/resources';
25+
import { detectResources } from '@opentelemetry/resources';
2626

2727
describe('[Integration] AlibabaCloudEcsDetector', () => {
2828
it('should not start spans for detector requests', async () => {
@@ -47,7 +47,7 @@ describe('[Integration] AlibabaCloudEcsDetector', () => {
4747
const {
4848
alibabaCloudEcsDetector,
4949
} = require('../../build/src/detectors/AlibabaCloudEcsDetector');
50-
const resource = alibabaCloudEcsDetector.detect() as IResource;
50+
const resource = detectResources({ detectors: [alibabaCloudEcsDetector] });
5151
await resource.waitForAsyncAttributes?.();
5252

5353
// Wait for the next loop to let the span close properly

package-lock.json

Lines changed: 53 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)