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 logic in WriteJSONToResponse to log errors in ECS agent log #2641

Merged
merged 1 commit into from
Sep 18, 2020

Conversation

chienhanlin
Copy link
Contributor

@chienhanlin chienhanlin commented Sep 18, 2020

Summary

This PR adds logic to propagate responses of requests to the Amazon ECS Container Agent Log when an error occurs.

Implementation details

This PR includes the following change in agent/handlers/utils/helpers.go:

  • Added logic in WriteJSONToResponse function to propagate HTTP response status codes, request types and responses in JSON to the Amazon ECS Container Agent Log when the response status code is 4xx client error or 5xx server error.

Testing

  • Manually tested the changes with following Task Metadata Endpoint (TMDE) version 3 and 4 paths, and get returned JSON responses.
  1. ${ECS_CONTAINER_METADATA_URI}
  2. ${ECS_CONTAINER_METADATA_URI}/task
  3. ${ECS_CONTAINER_METADATA_URI}/stats
  4. ${ECS_CONTAINER_METADATA_URI}/task/stats
  • Manually tested the changes when HTTP response status code 500 occurs, and verified the HTTP response status code, the request type and responses are successfully propagated to Amazon ECS Container Agent Log.

  • This change is not covered by new tests

Description for the changelog

Improvement - Propagate responses to the Amazon ECS Container Agent Log when an error occurs

Licensing

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

@ubhattacharjya ubhattacharjya requested a review from a team September 18, 2020 18:28
@@ -129,3 +131,10 @@ func LimitReachedHandler(auditLogger audit.AuditLogger) func(http.ResponseWriter
auditLogger.Log(logRequest, http.StatusTooManyRequests, "")
}
}

// WriteErrorToDefaultLogger write the requestType, httpStatusCode and responseJSON of error to the default logger
func WriteErrorToDefaultLogger(httpStatusCode int, responseJSON []byte, requestType string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think the function name is a bit misleading, there's no error passed as parameter, also the function is not reused anywhere. I think I'd prefer to get rid of the function and just put the code directly in WriteJSONToResponse

Copy link
Contributor Author

@chienhanlin chienhanlin Sep 18, 2020

Choose a reason for hiding this comment

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

Thanks for the suggestion. function WriteErrorToDefaultLogger is now integrated into function WriteJSONToResponse from line 84 to 86.


// WriteErrorToDefaultLogger write the requestType, httpStatusCode and responseJSON of error to the default logger
func WriteErrorToDefaultLogger(httpStatusCode int, responseJSON []byte, requestType string) {
if httpStatusCode >= 400 && httpStatusCode <= 599 {
Copy link
Contributor

Choose a reason for hiding this comment

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

would it be better to have if httpStatusCode<200 || httpStatusCode>299 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As httpStatusCode 1xx and 3xx would not be considered as an error code; therefore the if statement httpStatusCode >= 400 && httpStatusCode <= 599 is used here to only propagate httpStatusCode 4xx and 5xx to the agent log.

@chienhanlin chienhanlin changed the title Add WriteErrorToDefaultLogger function to log errors in ECS agent def… Add logic in WriteJSONToResponse to log errors in ECS agent log Sep 18, 2020
@chienhanlin chienhanlin merged commit 6da230c into aws:dev Sep 18, 2020
@sparrc sparrc added this to the 1.45.0 milestone Sep 28, 2020
@chienhanlin chienhanlin deleted the ECSAgentTMDELogger branch January 12, 2021 07:10
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.

5 participants