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

Add ECS metadata allowing cloudwatch-logs to be linked with traces #93

Merged

Conversation

etiennechabert
Copy link
Contributor

@etiennechabert etiennechabert commented Sep 11, 2023

In this PR

  • For ECS/Fargate: allow traces and logs to be linked together by adding missing log-group information

Warning

It will only work for ECS Task running on Fargate 1.4 and above.

Related issue

#90

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@etiennechabert etiennechabert requested a review from a team as a code owner September 11, 2023 08:12
@etiennechabert etiennechabert marked this pull request as draft September 11, 2023 08:12
@@ -10,13 +10,80 @@ module ECS

ORIGIN = 'AWS::ECS::Container'.freeze

# Only compatible with v4!
# The v3 metadata url does not contain cloudwatch informations
METADATA_ENV_KEY = 'ECS_CONTAINER_METADATA_URI_V4'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we have a strong dependency with Fargate 1.4, otherwise this environment variable will not be defined:
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4-fargate.html

Sadly the necessary information are not exposed with the meta-information of the v3:
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v3-fargate.html

Comment on lines +51 to +59
ecs: {
container_arn: data['ContainerARN'],
},
cloudwatch_logs: {
log_group: data["LogOptions"]['awslogs-group'],
log_region: data["LogOptions"]['awslogs-region'],
arn: data['ContainerARN']
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Potentially, much more could be added to the x-ray segment.

This particular information for example would be valuable to hunt regressions:

com.amazonaws.ecs.task-definition-version
Screenshot 2023-09-11 at 12 52 41

@@ -43,6 +44,7 @@ def test_ec2_metadata_v2_successful
ami_id: 'ami-03cca83dd001d4666'
}
}
# We should probably use `assert_equal` here ? Always true otherwise...
assert expected, XRay::Plugins::EC2.aws
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not related to my PR, but assert is always returning true here... regardless what is passed

We probably want to use assert_equal if we want to compare something expected with XRay::Plugins::EC2.aws

"LogOptions"=>{"awslogs-group"=>"/ecs/a_service_name", "awslogs-region"=>"eu-central-1", "awslogs-stream"=>"ecs/a_service_name/a_task_id"},
}

ENV[XRay::Plugins::ECS::METADATA_ENV_KEY] = dummy_metadata_uri
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried a few things to mutate the ENV in a better way...

Could not do it without adding extra dependencies to the project

@etiennechabert etiennechabert marked this pull request as ready for review September 11, 2023 10:59
container: Socket.gethostname,
container_arn: 'arn:aws:ecs:eu-central-1:an_id:container/a_cluster/a_cluster_id/a_task_id',
},
cloudwatch_logs: {:log_group=>"/ecs/a_service_name", :log_region=>"eu-central-1", :arn=>"arn:aws:ecs:eu-central-1:an_id:container/a_cluster/a_cluster_id/a_task_id"}
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 the key change here, with this information present in the trace, the logs should be linked

Comment on lines -6 to -7
# Due to lack of ECS container metadata service, the only host information
# available is the host name.
Copy link
Contributor Author

@etiennechabert etiennechabert Sep 11, 2023

Choose a reason for hiding this comment

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

This comment is not anymore relevant

@etiennechabert etiennechabert changed the title Add cloudwatch logs to aws context in traces Add ecs metadata regarding logs to traces Sep 11, 2023
@etiennechabert etiennechabert changed the title Add ecs metadata regarding logs to traces Add ECS metadata allowing cloudwatch-logs to be linked with traces Sep 11, 2023
Copy link
Contributor

@mxiamxia mxiamxia left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the contribution!

@srprash srprash merged commit 75d350d into aws:master Sep 25, 2023
8 checks passed
@etiennechabert etiennechabert deleted the add-cloudwatch_logs-to-aws-context-in-traces branch September 25, 2023 19:20
tetsu040e added a commit to enechange/aws-xray-sdk-ruby that referenced this pull request Oct 15, 2024
* Add ECS metadata allowing cloudwatch-logs to be linked with traces (aws#93)

* First step

* Add comment

* Typo

* Copy logic of EC2 plugin for ECS

* Typo

* Back to symbols

* Copy paste all EC2 tests as starting point for ECS

* Comments

* Somewhat working ?

* Backword compatible with prevous feature when not 1.4

* Add hint for fargate 1.4 in case of errors

---------

Co-authored-by: Etienne Chabert <[email protected]>

* release 0.15.0 (aws#94)

* Update version for 0.15.0 release (aws#95)

* Update continuous-build.yml (aws#97)

the latest macos-14 supports arm only. 
This PR using the previous version of macos image to unblock the release.

* Fix issue: Wrong duration for DB transaction event  on ROR 7.1 aws#92 (aws#96)

* Fix issue: Wrong duration for DB transaction event  on ROR 7.1 aws#92

* Added unit tests and restructured the code

* Fix unit tests

* Update continuous-build.yml (aws#97)

the latest macos-14 supports arm only. 
This PR using the previous version of macos image to unblock the release.

* Worked on the jj22ee's comment and added logic that convert in seconds only for rails 7.1 versions.

* Conditionally handle for ruby 7.1

---------

Co-authored-by: Lei Wang <[email protected]>

* Release 0.16.0 (aws#98)

---------

Co-authored-by: Chabert Etienne <[email protected]>
Co-authored-by: Etienne Chabert <[email protected]>
Co-authored-by: Prashant Srivastava <[email protected]>
Co-authored-by: Lei Wang <[email protected]>
Co-authored-by: Osama Bin Junaid <[email protected]>
Co-authored-by: Min Xia <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants