Skip to content

Commit 1f48c34

Browse files
authored
only log unexpected status code if it's the last attempt (#288)
We don't need to see the error log for every retry, let's only log it if it's the last attempt and it fails.
1 parent 7bac821 commit 1f48c34

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

internal/metrics/client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func (c *Client) RecordEvent(ctx context.Context, params EventParams) {
104104
logger = logger.With(zap.String("idempotency", idempotency))
105105

106106
for i := range maxRetries {
107+
lastAttempt := i == maxRetries-1
108+
107109
if i > 0 {
108110
time.Sleep(time.Duration(rand.Int63n(backoffRange)*int64(i)) * time.Millisecond)
109111
}
@@ -124,7 +126,7 @@ func (c *Client) RecordEvent(ctx context.Context, params EventParams) {
124126

125127
resp, err := c.client.Do(req)
126128
if err != nil {
127-
if i < maxRetries-1 {
129+
if !lastAttempt {
128130
logger.Warn("failed execute metrics request, retrying", zap.Int("attempt", i), zap.Error(err))
129131
} else {
130132
logger.Error("failed execute metrics request", zap.Error(err))
@@ -135,7 +137,9 @@ func (c *Client) RecordEvent(ctx context.Context, params EventParams) {
135137
resp.Body.Close() //nolint:errcheck
136138

137139
if resp.StatusCode != http.StatusNoContent {
138-
logger.Error("unexpected status code from metrics endpoint", zap.Int("status", resp.StatusCode))
140+
if lastAttempt {
141+
logger.Error("unexpected status code from metrics endpoint", zap.Int("status", resp.StatusCode))
142+
}
139143
if resp.StatusCode/100 == 5 {
140144
continue
141145
}

0 commit comments

Comments
 (0)