Skip to content

Commit e85a47c

Browse files
Add error content on metric counter with attrs
1 parent 93aaabb commit e85a47c

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

register_metrics_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55
"net/http"
66
"net/http/httptest"
7+
"strings"
78
"sync"
89
"testing"
910
"time"
@@ -67,4 +68,92 @@ func TestHTTPClient_MetricsIntegration(t *testing.T) {
6768
defer metrics.lock.Unlock()
6869
return len(metrics.pushToSeriesCalls) > 0 && len(metrics.incrCounterCalls) > 0 && len(metrics.incrCounterWithAttrsCalls) > 0
6970
}, time.Second, 100*time.Millisecond)
71+
72+
metrics.lock.Lock()
73+
defer metrics.lock.Unlock()
74+
75+
foundPush := false
76+
for _, key := range metrics.pushToSeriesCalls {
77+
if strings.HasSuffix(key, "response_time") {
78+
foundPush = true
79+
break
80+
}
81+
}
82+
assert.True(t, foundPush, "PushToSeries should register key with suffix response_time")
83+
84+
foundStatus := false
85+
for _, key := range metrics.incrCounterCalls {
86+
if strings.HasSuffix(key, "status.200") {
87+
foundStatus = true
88+
break
89+
}
90+
}
91+
assert.True(t, foundStatus, "IncrCounter should register key with suffix status.200")
92+
93+
foundAttrs := false
94+
for _, call := range metrics.incrCounterWithAttrsCalls {
95+
if strings.HasSuffix(call.key, "total") &&
96+
call.attrs["status"] == "200" &&
97+
call.attrs["host"] != "" {
98+
foundAttrs = true
99+
break
100+
}
101+
}
102+
assert.True(t, foundAttrs, "IncrCounterWithAttrs should register correct attributes and key with suffix total")
103+
}
104+
105+
func TestHTTPClient_Metrics_Errors(t *testing.T) {
106+
t.Run("circuit open error", func(t *testing.T) {
107+
metrics := &mockMetrics{}
108+
client := httpclient.NewHTTPClient(
109+
&httpclient.LoggerAdapter{Writer: io.Discard},
110+
httpclient.WithMetrics(metrics),
111+
httpclient.WithChainCallback(func(fn func() (*httpclient.Response, error)) (*httpclient.Response, error) {
112+
return nil, httpclient.ErrCircuitOpen
113+
}),
114+
)
115+
_, _ = client.NewRequest().Get("/test-circuit-open")
116+
time.Sleep(50 * time.Millisecond)
117+
metrics.lock.Lock()
118+
defer metrics.lock.Unlock()
119+
found := false
120+
for _, call := range metrics.incrCounterCalls {
121+
if strings.HasSuffix(call, "circuit_open") {
122+
found = true
123+
break
124+
}
125+
}
126+
assert.True(t, found, "Should increment circuit_open counter on ErrCircuitOpen")
127+
})
128+
129+
t.Run("generic error", func(t *testing.T) {
130+
metrics := &mockMetrics{}
131+
client := httpclient.NewHTTPClient(
132+
&httpclient.LoggerAdapter{Writer: io.Discard},
133+
httpclient.WithMetrics(metrics),
134+
httpclient.WithChainCallback(func(fn func() (*httpclient.Response, error)) (*httpclient.Response, error) {
135+
return nil, assert.AnError
136+
}),
137+
)
138+
_, _ = client.NewRequest().Get("/test-generic-error")
139+
time.Sleep(50 * time.Millisecond)
140+
metrics.lock.Lock()
141+
defer metrics.lock.Unlock()
142+
foundError := false
143+
foundAttr := false
144+
for _, call := range metrics.incrCounterCalls {
145+
if strings.HasSuffix(call, "errors") {
146+
foundError = true
147+
break
148+
}
149+
}
150+
for _, call := range metrics.incrCounterWithAttrsCalls {
151+
if val, ok := call.attrs["error"]; ok && val == assert.AnError.Error() {
152+
foundAttr = true
153+
break
154+
}
155+
}
156+
assert.True(t, foundError, "Should increment errors counter on generic error")
157+
assert.True(t, foundAttr, "Should add error attribute on generic error")
158+
})
70159
}

request.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func registerMetrics(key string, metrics Metrics, f func() (*Response, error)) (
150150

151151
if metrics != nil {
152152
go func(resp *Response, err error) {
153-
var attrs map[string]string
153+
attrs := map[string]string{}
154154
if resp != nil {
155155
attrs = map[string]string{
156156
"host": resp.Request().HostURL().Host,
@@ -167,6 +167,7 @@ func registerMetrics(key string, metrics Metrics, f func() (*Response, error)) (
167167
metrics.IncrCounter(fmt.Sprintf("%s.%s", key, "circuit_open"))
168168
} else {
169169
metrics.IncrCounter(fmt.Sprintf("%s.%s", key, "errors"))
170+
attrs["error"] = err.Error()
170171
}
171172
}
172173
metrics.IncrCounterWithAttrs(fmt.Sprintf("%s.%s", key, "total"), attrs)

0 commit comments

Comments
 (0)