aws / amazon-ecs-agent

Amazon Elastic Container Service Agent
http://aws.amazon.com/ecs/
Apache License 2.0
2.07k stars 608 forks source link

Add a middleware for metrics for fault injection requests #4365

Closed xxx0624 closed 4 days ago

xxx0624 commented 4 days ago

Summary

This PR is to create a telemetry middleware that will have a log for the duration of the request, one duration metric and one error metric.

Implementation details

Testing

New tests cover the changes: Yes

Manual testing by consuming this change locally on Fargate

// We can see the log from the telemetry middleware Sep 25 22:54:09 ip-10-194-20-138 two: {"level":"info","time":"2024-09-25T22:54:09.377","msg":"Complete the request","DurationInMs":0,"Request":"/api/ed46e7244d974cc491d3c3850fd8df5a-3147973833/fault/v1/network-blackhole-port","StatusCode":400}

// We can see the metric in logs {"level":"info","timestamp":"2024-09-25T22:54:09.377Z","msg":"","Operation":"MetadataServer.CheckNetworkBlackholePortDuration","Count":1,"Gauge":"0","Success":1,"Duration":0.000001254,"StatusCode":400,"DurationInMs":0,"Request":"/api/ed46e7244d974cc491d3c3850fd8df5a-3147973833/fault/v1/network-blackhole-port"} {"level":"info","timestamp":"2024-09-25T22:54:09.377Z","msg":"","Operation":"MetadataServer.CheckNetworkBlackholePortClientError","error":"fail to process the request due to a client error","Count":1,"Gauge":"","Success":0,"Duration":0.000000647,"StatusCode":400,"DurationInMs":0,"Request":"/api/ed46e7244d974cc491d3c3850fd8df5a-3147973833/fault/v1/network-blackhole-port"}


- Case 2

sh-5.2# curl -X PUT $ECS_AGENT_URI/fault/v1/network-blackhole-port -d '{"Port":80,"Protocol":"tcp","TrafficType":"ingress"}' -v

// We can see the log about the duration Sep 25 22:54:50 ip-10-194-20-138 two: {"level":"info","time":"2024-09-25T22:54:50.042","msg":"Complete the request","DurationInMs":7,"Request":"/api/ed46e7244d974cc491d3c3850fd8df5a-3147973833/fault/v1/network-blackhole-port","StatusCode":200}

// We can see the metric in logs {"level":"info","timestamp":"2024-09-25T22:54:50.042Z","msg":"","Operation":"MetadataServer.StartNetworkBlackholePortDuration","Count":1,"Gauge":"7","Success":1,"Duration":0.000000539,"Request":"/api/ed46e7244d974cc491d3c3850fd8df5a-3147973833/fault/v1/network-blackhole-port","StatusCode":200,"DurationInMs":7}



### Description for the changelog
<!--
Write a short (one line) summary that describes the changes in this
pull request for inclusion in the changelog.
You can see our changelog entry style here:
https://github.com/aws/amazon-ecs-agent/commit/c9aefebc2b3007f09468f651f6308136bd7b384f
-->

Feature - Add a telemetry middleware for fault injection handlers.

### Additional Information

**Does this PR include breaking model changes? If so, Have you added transformation functions?**
<!-- If yes, next release should have a upgraded minor version -->  

No

**Does this PR include the addition of new environment variables in the README?**
<!-- 
If it is a sensitive variable, add it to this blocklist in ecs-logs-collector here: https://github.com/aws/amazon-ecs-logs-collector/blob/b0958c2aa424c6dc578d5a8def4422c51791a076/ecs-logs-collector.sh#L63
If it is not a sensitive variable, add it to the allowlist in ecs-logs-collector here: https://github.com/aws/amazon-ecs-logs-collector/blob/b0958c2aa424c6dc578d5a8def4422c51791a076/ecs-logs-collector.sh#L66
-->

No

### Licensing

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