-
Notifications
You must be signed in to change notification settings - Fork 530
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
david-luna
merged 14 commits into
open-telemetry:main
from
david-luna:dluna/aibaba-ecs-detector-sync
Aug 2, 2024
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3389dcc
refactor(detector-alibaba): change implementation to DetectorSync int…
david-luna 0fd9ba3
refactor(detector-alibaba): fix return type
david-luna 3ded77a
Update detectors/node/opentelemetry-resource-detector-alibaba-cloud/t…
david-luna 2b50ea1
chore(detector-alibaba): remove unused fixture file
david-luna 72663b9
chore(detector-alibaba): sync changes
david-luna 873b9a5
chore(detector-alibaba): update dependencies
david-luna e672c53
Merge branch 'main' into dluna/aibaba-ecs-detector-sync
david-luna 42862a8
refactor(detector-alibaba): avoid breaking change in API
david-luna baaa6ee
chore(detector-alibaba): fix lint issue
david-luna 25bcb8d
chore(detector-alibaba): move constants to a separate file
david-luna fdd6524
chore(detector-alibaba): remove async detector
david-luna 57bf61a
chore(detector-alibaba): reset test name
david-luna ac92a5c
Merge branch 'main' into dluna/aibaba-ecs-detector-sync
david-luna fe26750
Merge branch 'main' into dluna/aibaba-ecs-detector-sync
david-luna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ describe('alibabaCloudEcsDetector', () => { | |
.reply(200, () => mockedHostResponse); | ||
|
||
const resource: Resource = await alibabaCloudEcsDetector.detect(); | ||
await resource.waitForAsyncAttributes?.(); | ||
|
||
scope.done(); | ||
|
||
|
@@ -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 () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
}); | ||
|
86 changes: 86 additions & 0 deletions
86
...telemetry-resource-detector-alibaba-cloud/test/fixtures/use-alibaba-cloud-ecs-detector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
david-luna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Usage: | ||
// node use-gcp-detector.js | ||
david-luna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const { createTestNodeSdk } = require('@opentelemetry/contrib-test-utils'); | ||
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); | ||
const { alibabaCloudEcsDetector } = require('../../build/src/index.js'); | ||
|
||
|
||
const sdk = createTestNodeSdk({ | ||
serviceName: 'use-detector-alibaba-cloud-ecs', | ||
instrumentations: [ | ||
new HttpInstrumentation(), | ||
], | ||
resourceDetectors: [alibabaCloudEcsDetector], | ||
}); | ||
|
||
sdk.start(); | ||
|
||
const http = require('http'); | ||
|
||
const server = http.createServer((req, res) => { | ||
console.log('incoming request: %s %s %s', req.method, req.url, req.headers); | ||
|
||
req.resume(); | ||
req.on('end', function () { | ||
const body = 'pong'; | ||
res.writeHead(200, { | ||
'content-type': 'text/plain', | ||
'content-length': body.length, | ||
}); | ||
res.end(body); | ||
}); | ||
}); | ||
|
||
server.listen(0, '127.0.0.1', async function () { | ||
const port = server.address().port; | ||
|
||
// First request to show a client error. | ||
const startTime = Date.now(); | ||
await new Promise((resolve) => { | ||
const clientReq = http.request( | ||
`http://127.0.0.1:${port}/ping`, | ||
function (cres) { | ||
console.log( | ||
'client response: %s %s', | ||
cres.statusCode, | ||
cres.headers | ||
); | ||
const chunks = []; | ||
cres.on('data', function (chunk) { | ||
chunks.push(chunk); | ||
}); | ||
cres.on('end', function () { | ||
const body = chunks.join(''); | ||
console.log('client response body: %j', body); | ||
resolve(); | ||
}); | ||
} | ||
); | ||
clientReq.write('ping'); | ||
clientReq.end(); | ||
}); | ||
|
||
// flush any left spans | ||
// NOTE: this adds extra requests but its necessary to make sure | ||
// spans have the resouce and are queued in the exporter | ||
await alibabaCloudEcsDetector.detect().waitForAsyncAttributes(); | ||
await sdk.shutdown(); | ||
await new Promise(resolve => server.close(resolve)); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could a diag log be added for the error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that the rejection is handled at https://github.com/open-telemetry/opentelemetry-js/blob/54b14fbbe33e0cf38115ee8c5c934a4e27786186/packages/opentelemetry-resources/src/Resource.ts#L87-L91. I think we should propagate the error instead of discarding it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree.