Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(detector-alibaba)!: change implementation to DetectorSync interface #2328

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

import {
Detector,
DetectorSync,
IResource,
Resource,
ResourceAttributes,
ResourceDetectionConfig,
} from '@opentelemetry/resources';
import {
Expand All @@ -31,14 +33,15 @@ import {
SEMRESATTRS_HOST_NAME,
SEMRESATTRS_HOST_TYPE,
} from '@opentelemetry/semantic-conventions';

import * as http from 'http';

/**
* The AlibabaCloudEcsDetector can be used to detect if a process is running in
* 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.
Expand All @@ -57,7 +60,14 @@ class AlibabaCloudEcsDetector implements Detector {
*
* @param config (unused) The resource detection config
*/
async detect(_config?: ResourceDetectionConfig): Promise<Resource> {
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<ResourceAttributes> {
const {
'owner-account-id': accountId,
'instance-id': instanceId,
Expand All @@ -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,
Expand All @@ -76,7 +86,7 @@ class AlibabaCloudEcsDetector implements Detector {
[SEMRESATTRS_HOST_ID]: instanceId,
[SEMRESATTRS_HOST_TYPE]: instanceType,
[SEMRESATTRS_HOST_NAME]: hostname,
});
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './AlibabaCloudEcsDetector';
export { alibabaCloudEcsDetector } from './AlibabaCloudEcsDetector';
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './detectors';
export { alibabaCloudEcsDetector } from './detectors';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to reviewer: little step forward to resolve open-telemetry/opentelemetry-js#4186

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +64,7 @@ describe('alibabaCloudEcsDetector', () => {
.reply(200, () => mockedHostResponse);

const resource: Resource = await alibabaCloudEcsDetector.detect();
await resource.waitForAsyncAttributes?.();

scope.done();

Expand All @@ -84,56 +85,47 @@ 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 () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to reviewer: the detector does not throw but rather returns a resource with no attribs.

const scope = nock(ALIYUN_HOST)
.persist()
.get(ALIYUN_IDENTITY_PATH)
.reply(200, () => mockedIdentityResponse)
.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)
.get(ALIYUN_HOST_PATH)
.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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.