diff --git a/plugins/inputs/ecs/ecs.go b/plugins/inputs/ecs/ecs.go index 212f408f9..216060de7 100644 --- a/plugins/inputs/ecs/ecs.go +++ b/plugins/inputs/ecs/ecs.go @@ -122,6 +122,15 @@ func resolveEndpoint(ecs *Ecs) { // Auto-detect metadata endpoint version. + // Use metadata v4 if available. + // https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html + v4Endpoint := os.Getenv("ECS_CONTAINER_METADATA_URI_V4") + if v4Endpoint != "" { + ecs.EndpointURL = v4Endpoint + ecs.metadataVersion = 4 + return + } + // Use metadata v3 if available. // https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v3.html v3Endpoint := os.Getenv("ECS_CONTAINER_METADATA_URI") diff --git a/plugins/inputs/ecs/ecs_test.go b/plugins/inputs/ecs/ecs_test.go index 078f807c6..356abcaa8 100644 --- a/plugins/inputs/ecs/ecs_test.go +++ b/plugins/inputs/ecs/ecs_test.go @@ -808,6 +808,19 @@ func TestResolveEndpoint(t *testing.T) { metadataVersion: 3, }, }, + { + name: "Endpoint is not set, ECS_CONTAINER_METADATA_URI_V4 is set => use v4 metadata", + setEnv: func(t *testing.T) { + t.Setenv("ECS_CONTAINER_METADATA_URI_V4", "v4-endpoint.local") + }, + given: Ecs{ + EndpointURL: "", + }, + exp: Ecs{ + EndpointURL: "v4-endpoint.local", + metadataVersion: 4, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {