@@ -13,12 +13,13 @@ import (
1313)
1414
1515type 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.
4046func (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
0 commit comments