Fix ECS resource detector populating container.name with the hostname - #4961
Fix ECS resource detector populating container.name with the hostname#4961rdbisme wants to merge 4 commits into
Conversation
Pull request dashboard statusWaiting on the author · refreshed 2026-08-25 16:06 UTC Two things need attention:
Status above doesn't look right?
|
|
Nice fix, and the v4 path looks right to me: One gap: the fix only covers the v4 path, but the title is about the hostname fallback in general. Two options: also read the v3 metadata endpoint ( Separate and softer, so treat this as a question: the v4 response carries both |
emdneto
left a comment
There was a problem hiding this comment.
I'm ok to keep the current behavior for the v3 metadata endpoint branch, because it's been deprecated for a while, although I agree one test proving a missing v4 Name falls back correctly would be nice. Also, the changelog needs to be updated explicitly about the change for the v4 metadata endpoint branch only.
I don't think DockerName is appropriate here; it won't be meaningful for troubleshooting purposes, so I'm ok with using the Name defined in the Task.
| container_name = metadata_container.get("Name", socket.gethostname()) | ||
| base_resource = base_resource.merge( | ||
| Resource({ResourceAttributes.CONTAINER_NAME: container_name}) | ||
| ) | ||
|
|
There was a problem hiding this comment.
| container_name = metadata_container.get("Name", socket.gethostname()) | |
| base_resource = base_resource.merge( | |
| Resource({ResourceAttributes.CONTAINER_NAME: container_name}) | |
| ) | |
| container_name = metadata_container.get("Name") | |
| if container_name: | |
| base_resource = base_resource.merge( | |
| Resource({ResourceAttributes.CONTAINER_NAME: container_name}) | |
| ) |
Description
The
AwsEcsResourceDetectorpopulates thecontainer.nameresource attribute withsocket.gethostname(). On ECS tasks using theawsvpcnetwork mode the container hostname is the IP-like private DNS name of the ENI (e.g.ip-10-0-2-100.us-west-2.compute.internal), socontainer.nameends up being an "IP name" instead of the actual container name.When the metadata endpoint v4 is available (which the detector already queries), the container-level response contains the container name as defined in the task definition (field
Name, e.g.curl). This PR uses that value forcontainer.namewhen the v4 endpoint is present, and keeps the hostname as a fallback (v3-only path, or a response without aNamefield).No existing issue was filed for this; the fix follows the metadata response documented at https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html#task-metadata-endpoint-v4-response
Type of change
How Has This Been Tested?
python -m pytest sdk-extension/opentelemetry-sdk-extension-aws/tests/resource/test_ecs.py— 4 passed.The v4 EC2/Fargate tests now mock
socket.gethostname()to return the IP-like name while expectingcontainer.name: "curl"from the metadata fixtures, proving the metadata value overrides the hostname. The v3 test keeps covering the hostname fallback.ruff checkon the changed source and test files — clean.Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.