Skip to content

Commit

Permalink
Fix dupe logs
Browse files Browse the repository at this point in the history
  • Loading branch information
zhihonl committed Sep 5, 2024
1 parent 9a3f11f commit 481d6e4
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 48 deletions.
4 changes: 0 additions & 4 deletions extension/agenthealth/handler/stats/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type Stats struct {
RunningInContainer *int `json:"ric,omitempty"`
RegionType *string `json:"rt,omitempty"`
Mode *string `json:"m,omitempty"`
EntityRejected *int `json:"ent,omitempty"`
}

// Merge the other Stats into the current. If the field is not nil,
Expand Down Expand Up @@ -77,9 +76,6 @@ func (s *Stats) Merge(other Stats) {
if other.Mode != nil {
s.Mode = other.Mode
}
if other.EntityRejected != nil {
s.EntityRejected = other.EntityRejected
}
}

func (s *Stats) Marshal() (string, error) {
Expand Down
24 changes: 0 additions & 24 deletions extension/agenthealth/handler/stats/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package client

import (
"bytes"
"context"
"io"
"net/http"
Expand All @@ -24,10 +23,6 @@ const (
cacheSize = 1000
)

var (
rejectedEntityInfo = []byte("\"rejectedEntityInfo\"")
)

type Stats interface {
awsmiddleware.RequestHandler
awsmiddleware.ResponseHandler
Expand Down Expand Up @@ -113,9 +108,6 @@ func (csh *clientStatsHandler) HandleResponse(ctx context.Context, r *http.Respo
}
latency := time.Since(recorder.start)
stats.LatencyMillis = aws.Int64(latency.Milliseconds())
if rejectedEntityInfoExists(r) {
stats.EntityRejected = aws.Int(1)
}
csh.statsByOperation.Store(operation, stats)
}

Expand All @@ -130,19 +122,3 @@ func (csh *clientStatsHandler) Stats(operation string) agent.Stats {
}
return stats
}

// rejectedEntityInfoExists checks if the response body
// contains element rejectedEntityInfo
func rejectedEntityInfoExists(r *http.Response) bool {
// Example body for rejectedEntityInfo would be:
// {"rejectedEntityInfo":{"errorType":"InvalidAttributes"}}
if r == nil || r.Body == nil {
return false
}
defer r.Body.Close()
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
return false
}
return bytes.Contains(bodyBytes, rejectedEntityInfo)
}
21 changes: 1 addition & 20 deletions extension/agenthealth/handler/stats/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package client
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -38,16 +37,11 @@ func TestHandle(t *testing.T) {
assert.Nil(t, got.PayloadBytes)
assert.Nil(t, got.StatusCode)
time.Sleep(time.Millisecond)
handler.HandleResponse(ctx, &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"rejectedEntityInfo":{"errorType":"InvalidAttributes"}}`)),
})
handler.HandleResponse(ctx, &http.Response{StatusCode: http.StatusOK})
got = handler.Stats(operation)
assert.NotNil(t, got.LatencyMillis)
assert.NotNil(t, got.PayloadBytes)
assert.NotNil(t, got.StatusCode)
assert.NotNil(t, got.EntityRejected)
assert.Equal(t, 1, *got.EntityRejected)
assert.Equal(t, http.StatusOK, *got.StatusCode)
assert.Equal(t, 20, *got.PayloadBytes)
assert.GreaterOrEqual(t, *got.LatencyMillis, int64(1))
Expand All @@ -71,16 +65,3 @@ func TestHandle(t *testing.T) {
assert.NotNil(t, got.PayloadBytes)
assert.Equal(t, 29, *got.PayloadBytes)
}

func BenchmarkRejectedEntityInfoExists(b *testing.B) {
body := `{"rejectedEntityInfo":{"errorType":"InvalidAttributes"}}`
resp := &http.Response{
Body: io.NopCloser(bytes.NewBufferString(body)),
}

for n := 0; n < b.N; n++ {
rejectedEntityInfoExists(resp)
// Reset the body for the next iteration
resp.Body = io.NopCloser(bytes.NewBufferString(body))
}
}

0 comments on commit 481d6e4

Please sign in to comment.