Skip to content

Commit 17acfc7

Browse files
Merge branch 'main' into additional-attrs-counter-metrics
2 parents 4babe4b + bc9e1de commit 17acfc7

4 files changed

Lines changed: 99 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.swp
22
.vscode/
3-
.idea/
3+
.idea/
4+
coverage.*

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
PACKAGES ?= "$(shell go list ./... | grep -v tests)"
22

33
test:
4-
@go test -race $(shell echo $(PACKAGES))
4+
@go test ./... -coverprofile=coverage.out.tmp
5+
cat coverage.out.tmp | grep -v "/mocks/" > coverage.out
6+
@go tool cover -func=coverage.out
7+
8+
local-coverage: test
9+
@go tool cover -html=coverage.out -o coverage.html
10+
@echo "\n\n open the coverage.html on your browser !!!"
511

612
lint:
713
@golangci-lint run

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func registerMetrics(key string, metrics Metrics, f func() (*Response, error), a
171171
metrics.IncrCounter(fmt.Sprintf("%s.%s", key, "circuit_open"))
172172
} else {
173173
metrics.IncrCounter(fmt.Sprintf("%s.%s", key, "errors"))
174+
attrs["error"] = err.Error()
174175
}
175176
}
176177
metrics.IncrCounterWithAttrs(fmt.Sprintf("%s.%s", key, "total"), additionalAttrs)

0 commit comments

Comments
 (0)