Skip to content

Commit e512bb7

Browse files
Add support to send additional attributes on any counter metric
1 parent 93aaabb commit e512bb7

2 files changed

Lines changed: 53 additions & 15 deletions

File tree

request.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import (
1313
)
1414

1515
type Request struct {
16-
alias string
17-
chainCallback Callback
18-
hostURL *url.URL
19-
metrics Metrics
20-
restyRequest *resty.Request
21-
startTime time.Time
16+
alias string
17+
chainCallback Callback
18+
hostURL *url.URL
19+
metrics Metrics
20+
additionalAttrs map[string]string
21+
restyRequest *resty.Request
22+
startTime time.Time
2223
}
2324

2425
// NewRequest creates a request for the specified HTTP method.
@@ -36,6 +37,11 @@ func (r *Request) HostURL() *url.URL {
3637
return r.hostURL
3738
}
3839

40+
func (r *Request) SetMetricsAttrs(attrs map[string]string) *Request {
41+
r.additionalAttrs = attrs
42+
return r
43+
}
44+
3945
// SetHostURL sets the host url for the request.
4046
func (r *Request) SetHostURL(url *url.URL) *Request {
4147
r.hostURL = url
@@ -131,6 +137,11 @@ func (r *Request) Execute(method string, url string) (*Response, error) {
131137

132138
metricsAlias = strings.Replace(metricsAlias, ".", "-", -1)
133139

140+
attrs := r.additionalAttrs
141+
if len(r.additionalAttrs) == 0 {
142+
attrs = map[string]string{}
143+
}
144+
134145
return registerMetrics(metricsAlias, r.metrics, func() (*Response, error) {
135146
execute := func() (*Response, error) {
136147
r.startTime = time.Now()
@@ -142,24 +153,21 @@ func (r *Request) Execute(method string, url string) (*Response, error) {
142153
}
143154

144155
return r.chainCallback(execute)
145-
})
156+
}, attrs)
146157
}
147158

148-
func registerMetrics(key string, metrics Metrics, f func() (*Response, error)) (*Response, error) {
159+
func registerMetrics(key string, metrics Metrics, f func() (*Response, error), additionalAttrs map[string]string) (*Response, error) {
149160
resp, err := f()
150161

151162
if metrics != nil {
152163
go func(resp *Response, err error) {
153-
var attrs map[string]string
154164
if resp != nil {
155-
attrs = map[string]string{
156-
"host": resp.Request().HostURL().Host,
157-
"path": resp.Request().HostURL().Path,
158-
}
165+
additionalAttrs["host"] = resp.Request().HostURL().Host
166+
additionalAttrs["path"] = resp.Request().HostURL().Path
159167
metrics.PushToSeries(fmt.Sprintf("%s.%s", key, "response_time"), resp.ResponseTime().Seconds())
160168
if resp.statusCode != 0 {
161169
metrics.IncrCounter(fmt.Sprintf("%s.status.%d", key, resp.StatusCode()))
162-
attrs["status"] = fmt.Sprintf("%d", resp.StatusCode())
170+
additionalAttrs["status"] = fmt.Sprintf("%d", resp.StatusCode())
163171
}
164172
}
165173
if err != nil {
@@ -169,7 +177,7 @@ func registerMetrics(key string, metrics Metrics, f func() (*Response, error)) (
169177
metrics.IncrCounter(fmt.Sprintf("%s.%s", key, "errors"))
170178
}
171179
}
172-
metrics.IncrCounterWithAttrs(fmt.Sprintf("%s.%s", key, "total"), attrs)
180+
metrics.IncrCounterWithAttrs(fmt.Sprintf("%s.%s", key, "total"), additionalAttrs)
173181
}(resp, err)
174182
}
175183

request_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http/httptest"
77
"net/url"
88
"testing"
9+
"time"
910

1011
"github.com/globocom/httpclient"
1112

@@ -160,3 +161,32 @@ func testSetHostURL(target *httpclient.Request) func(*testing.T) {
160161
assert.Nil(t, target.HostURL())
161162
}
162163
}
164+
165+
func TestSetMetricsAttrs_PropagatesToMetrics(t *testing.T) {
166+
server := httptest.NewServer(http.HandlerFunc(handleFunc))
167+
defer server.Close()
168+
169+
metrics := &mockMetrics{}
170+
client := httpclient.NewHTTPClient(
171+
&httpclient.LoggerAdapter{Writer: io.Discard},
172+
httpclient.WithHostURL(server.URL),
173+
httpclient.WithMetrics(metrics),
174+
)
175+
request := client.NewRequest()
176+
177+
attrs := map[string]string{"foo": "bar", "baz": "qux"}
178+
request.SetMetricsAttrs(attrs)
179+
_, _ = request.Get("/")
180+
181+
// Aguarda goroutine
182+
time.Sleep(100 * time.Millisecond)
183+
184+
found := false
185+
for _, calledAttrs := range metrics.incrCounterWithAttrsCalls {
186+
if calledAttrs.attrs["foo"] == "bar" && calledAttrs.attrs["baz"] == "qux" {
187+
found = true
188+
break
189+
}
190+
}
191+
assert.True(t, found, "Attributes passed to SetMetricsAttrs must be propagated to IncrCounterWithAttrs")
192+
}

0 commit comments

Comments
 (0)