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

Update error prefix of v4 container stats endpoint for task lookup failure case #3794

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agent/handlers/task_server_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ func TestV4TaskNotFoundError404(t *testing.T) {
},
{
testPath: "/v4/bad/stats",
expectedBody: "\"V4 container handler: unable to get task arn from request: unable to get task Arn from v3 endpoint ID: bad\"",
expectedBody: "\"V4 container stats handler: unable to get task arn from request: unable to get task Arn from v3 endpoint ID: bad\"",
},
{
testPath: "/v4/bad/stats",
Expand Down Expand Up @@ -2818,7 +2818,7 @@ func TestV4ContainerStats(t *testing.T) {
},
expectedStatusCode: http.StatusNotFound,
expectedResponseBody: fmt.Sprintf(
"V4 container handler: unable to get task arn from request: unable to get task Arn from v3 endpoint ID: %s",
"V4 container stats handler: unable to get task arn from request: unable to get task Arn from v3 endpoint ID: %s",
v3EndpointID),
})
})
Expand Down
6 changes: 3 additions & 3 deletions agent/handlers/v4/tmdsstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ func (s *TMDSAgentState) GetContainerStats(v3EndpointID string) (tmdsv4.StatsRes
taskARN, ok := s.state.TaskARNByV3EndpointID(v3EndpointID)
if !ok {
return tmdsv4.StatsResponse{}, tmdsv4.NewErrorStatsLookupFailure(fmt.Sprintf(
"V4 container handler: unable to get task arn from request: unable to get task Arn from v3 endpoint ID: %s",
"unable to get task arn from request: unable to get task Arn from v3 endpoint ID: %s",
v3EndpointID))
}

containerID, ok := s.state.DockerIDByV3EndpointID(v3EndpointID)
if !ok {
return tmdsv4.StatsResponse{}, tmdsv4.NewErrorStatsLookupFailure(fmt.Sprintf(
"V4 container stats handler: unable to get container ID from request: unable to get docker ID from v3 endpoint ID: %s",
"unable to get container ID from request: unable to get docker ID from v3 endpoint ID: %s",
v3EndpointID))
}

Expand All @@ -186,7 +186,7 @@ func (s *TMDSAgentState) GetTaskStats(v3EndpointID string) (map[string]*tmdsv4.S
taskARN, ok := s.state.TaskARNByV3EndpointID(v3EndpointID)
if !ok {
return nil, tmdsv4.NewErrorStatsLookupFailure(fmt.Sprintf(
"V4 task stats handler: unable to get task arn from request: unable to get task Arn from v3 endpoint ID: %s",
"unable to get task arn from request: unable to get task Arn from v3 endpoint ID: %s",
v3EndpointID))
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

1 change: 0 additions & 1 deletion agent/vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime
github.com/aws/amazon-ecs-agent/ecs-agent/utils/ttime/mocks
github.com/aws/amazon-ecs-agent/ecs-agent/wsclient
github.com/aws/amazon-ecs-agent/ecs-agent/wsclient/mock
github.com/aws/amazon-ecs-agent/ecs-agent/wsclient/mock/utils
github.com/aws/amazon-ecs-agent/ecs-agent/wsclient/wsconn
# github.com/aws/aws-sdk-go v1.36.0
## explicit; go 1.11
Expand Down
16 changes: 11 additions & 5 deletions ecs-agent/tmds/handlers/v4/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
const (
EndpointContainerIDMuxName = "endpointContainerIDMuxName"
version = "v4"
containerStatsErrorPrefix = "V4 container stats handler"
taskStatsErrorPrefix = "V4 task stats handler"
)

// ContainerMetadataPath specifies the relative URI path for serving container metadata.
Expand Down Expand Up @@ -191,15 +193,17 @@ func ContainerStatsHandler(
agentState state.AgentState,
metricsFactory metrics.EntryFactory,
) func(http.ResponseWriter, *http.Request) {
return statsHandler(agentState.GetContainerStats, metricsFactory, utils.RequestTypeContainerStats)
return statsHandler(agentState.GetContainerStats, metricsFactory,
utils.RequestTypeContainerStats, containerStatsErrorPrefix)
}

// Returns an HTTP handler for v4 task stats endpoint
func TaskStatsHandler(
agentState state.AgentState,
metricsFactory metrics.EntryFactory,
) func(http.ResponseWriter, *http.Request) {
return statsHandler(agentState.GetTaskStats, metricsFactory, utils.RequestTypeTaskStats)
return statsHandler(agentState.GetTaskStats, metricsFactory,
utils.RequestTypeTaskStats, taskStatsErrorPrefix)
}

// Generic function that returns an HTTP handler for container or task stats endpoint
Expand All @@ -208,6 +212,7 @@ func statsHandler[R state.StatsResponse | map[string]*state.StatsResponse](
getStats func(string) (R, error), // container stats or task stats getter function
metricsFactory metrics.EntryFactory,
requestType string, // container stats or task stats request type
errorPrefix string,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Extract endpoint container ID
Expand All @@ -222,7 +227,7 @@ func statsHandler[R state.StatsResponse | map[string]*state.StatsResponse](
field.RequestType: requestType,
})

responseCode, responseBody := getStatsErrorResponse(endpointContainerID, err)
responseCode, responseBody := getStatsErrorResponse(endpointContainerID, err, errorPrefix)
utils.WriteJSONResponse(w, responseCode, responseBody, requestType)

if utils.Is5XXStatus(responseCode) {
Expand All @@ -242,11 +247,12 @@ func statsHandler[R state.StatsResponse | map[string]*state.StatsResponse](
}

// Returns appropriate HTTP status code and response body for stats endpoint error cases.
func getStatsErrorResponse(endpointContainerID string, err error) (int, string) {
func getStatsErrorResponse(endpointContainerID string, err error, errorPrefix string) (int, string) {
// 404 if lookup failure
var errLookupFailure *state.ErrorStatsLookupFailure
if errors.As(err, &errLookupFailure) {
return http.StatusNotFound, errLookupFailure.ExternalReason()
return http.StatusNotFound, fmt.Sprintf(
"%s: %s", errorPrefix, errLookupFailure.ExternalReason())
}

// 500 if any other known failure
Expand Down
4 changes: 2 additions & 2 deletions ecs-agent/tmds/handlers/v4/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func TestContainerStats(t *testing.T) {
testTMDSRequest(t, handler, TMDSTestCase[string]{
path: path,
expectedStatusCode: http.StatusNotFound,
expectedResponseBody: externalReason,
expectedResponseBody: "V4 container stats handler: " + externalReason,
})
})

Expand Down Expand Up @@ -451,7 +451,7 @@ func TestTaskStats(t *testing.T) {
testTMDSRequest(t, handler, TMDSTestCase[string]{
path: path,
expectedStatusCode: http.StatusNotFound,
expectedResponseBody: externalReason,
expectedResponseBody: "V4 task stats handler: " + externalReason,
})
})

Expand Down