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: Migrate EC2 Plugin Resource Detector from IMDSv1 to IMDSv2 #1408

Merged
merged 24 commits into from
Aug 18, 2020

Conversation

EdZou
Copy link
Contributor

@EdZou EdZou commented Aug 7, 2020

Which problem is this PR solving?

Since currently, the most updated version of Instance Metadata Service is already v2 and the previous implementation follows the IMDSv1 standard. We migrate it to follow IMDSv2 standard in this PR.
For more details, refer to AWS EC2 Instance Metadata Service

Short description of the changes

  1. migrate IMDSv1 EC2 resource detector to IMDSv2
  2. updated corresponding unit test
  3. also updated test part for detect-resources

If you would like to review @dyladan @anuraaga
Really appreciate it!

Additional Question:

When implementing this part, I notice that detect-resources take use of all the detectors components, should I also add Beanstalk and ECS detector to this part of code and make new PR?

@codecov
Copy link

codecov bot commented Aug 7, 2020

Codecov Report

Merging #1408 into master will increase coverage by 0.13%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1408      +/-   ##
==========================================
+ Coverage   94.11%   94.24%   +0.13%     
==========================================
  Files         142      144       +2     
  Lines        4280     4346      +66     
  Branches      873      879       +6     
==========================================
+ Hits         4028     4096      +68     
+ Misses        252      250       -2     
Impacted Files Coverage Δ
...rces/src/platform/node/detectors/AwsEc2Detector.ts 98.03% <100.00%> (+10.53%) ⬆️
packages/opentelemetry-plugin-https/src/utils.ts
packages/opentelemetry-plugin-https/src/https.ts
...ackages/opentelemetry-exporter-zipkin/src/types.ts 100.00% <0.00%> (ø)
...ges/opentelemetry-exporter-zipkin/src/transform.ts 100.00% <0.00%> (ø)
...ckages/opentelemetry-exporter-zipkin/src/zipkin.ts 98.46% <0.00%> (ø)
...ackages/opentelemetry-exporter-zipkin/src/utils.ts 100.00% <0.00%> (ø)

return new Resource({
[CLOUD_RESOURCE.PROVIDER]: 'aws',
[CLOUD_RESOURCE.ACCOUNT_ID]: accountId,
[CLOUD_RESOURCE.REGION]: region,
[CLOUD_RESOURCE.ZONE]: availabilityZone,
[HOST_RESOURCE.ID]: instanceId,
[HOST_RESOURCE.TYPE]: instanceType,
[HOST_RESOURCE.NAME]: hostname,
Copy link
Member

Choose a reason for hiding this comment

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

Name and hostname are the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

The main reason for this is not knowing how to populate this :) I filed an issue to ask about why there are two similar fields

open-telemetry/opentelemetry-specification#787

* If the application is running on an EC2 instance, we should be able
* to get back a valid JSON document. Parses that document and stores
* the identity properties in a local map.
*/
private async _awsMetadataAccessor<T>(): Promise<T> {
private async _fetchString(options: Record<string, any>): Promise<string> {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a more descriptive type we can use than Record<string, any>? Looks like HttpRequestOptions might be a good fit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds great! Let me do it!

readonly AWS_INSTANCE_IDENTITY_DOCUMENT_URI =
'http://169.254.169.254/latest/dynamic/instance-identity/document';
readonly AWS_INSTANCE_TOKEN_DOCUMENT_URI =
Copy link
Member

Choose a reason for hiding this comment

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

Looks like unused.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will delete this part, thank you for pointing out!

readonly AWS_INSTANCE_IDENTITY_DOCUMENT_URI =
'http://169.254.169.254/latest/dynamic/instance-identity/document';
readonly AWS_INSTANCE_TOKEN_DOCUMENT_URI =
'http://169.254.169.254/latest/api/token';
readonly AWS_INSTANCE_HOST_DOCUMENT_URI =
Copy link
Member

Choose a reason for hiding this comment

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

ditto

@@ -30,8 +30,18 @@ class AwsEc2Detector implements Detector {
* See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
* for documentation about the AWS instance identity document endpoint.
*/
readonly HTTP_HEADER = 'http://';
readonly AWS_IDMS_ENDPOINT = '169.254.169.254';
Copy link
Member

Choose a reason for hiding this comment

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

Maybe AWS_IDMS_HOST_ADDRESS?

Copy link
Contributor Author

@EdZou EdZou Aug 11, 2020

Choose a reason for hiding this comment

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

Nice suggestion!
But because we have another path called HOST_DOCUMENT. To avoid confusion, I suppose this kind of naming is also acceptable

Copy link

Choose a reason for hiding this comment

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

It's IMDS (Instance Metadata Service) ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for pointing out! Will modify this

const options = {
host: this.AWS_IDMS_ENDPOINT,
path: this.AWS_INSTANCE_TOKEN_DOCUMENT_PATH,
method: 'PUT',
Copy link
Member

Choose a reason for hiding this comment

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

Why is this PUT instead of GET?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is part of AWS IDMSv2 standard, we need to do PUT first to obtain TOKEN. You can refer to https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html

Copy link
Member

@mwear mwear left a comment

Choose a reason for hiding this comment

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

LGTM

@dyladan dyladan added the enhancement New feature or request label Aug 12, 2020
*/
readonly AWS_INSTANCE_IDENTITY_DOCUMENT_URI =
'http://169.254.169.254/latest/dynamic/instance-identity/document';
readonly HTTP_HEADER = 'http://';
Copy link
Member

Choose a reason for hiding this comment

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

This is probably not a great name since HTTP Header already has its own meaning. It looks like you're looking for the URL Scheme.

It looks like this is also only used in tests, so why is it defined here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh here is a historical problem. Initially we use Url in order to connect with AWS identity document. And current version we use host and path separately.
I will move this constant to test code, thank you for pointing out!

Copy link
Member

@obecny obecny left a comment

Choose a reason for hiding this comment

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

lgtm, I hope you can address just last Nit comment prior to merging.

@EdZou EdZou changed the title Feat: Migrate EC2 Plugin Resource Detector from IDMSv1 to IDMSv2 Feat: Migrate EC2 Plugin Resource Detector from IMDSv1 to IMDSv2 Aug 17, 2020
@obecny obecny merged commit 1f8c365 into open-telemetry:master Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants