Skip to content

Commit

Permalink
Add support to IncrCounterWithAttrs metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopaulovieira committed May 3, 2023
1 parent d3ba48a commit 44e584d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ type Metrics interface {
IncrCounter(name string)
// PushToSeries adds a new value to a histogram identified by the given name.
PushToSeries(name string, value float64)
// IncrCounterWithAttrs increments the counter value identified by the given name while adding attributes.
IncrCounterWithAttrs(name string, attributes map[string]string)
}
5 changes: 3 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ func registerMetrics(key string, metrics Metrics, f func() (*Response, error)) (
resp, err := f()

if metrics != nil {
metrics.IncrCounter(fmt.Sprintf("%s.%s", key, "total"))

go func(resp *Response, err error) {
attrs := map[string]string{}
if resp != nil {
metrics.PushToSeries(fmt.Sprintf("%s.%s", key, "response_time"), resp.ResponseTime().Seconds())
if resp.statusCode != 0 {
metrics.IncrCounter(fmt.Sprintf("%s.status.%d", key, resp.StatusCode()))
attrs["status"] = fmt.Sprintf("%d", resp.StatusCode())
}
}
if err != nil {
Expand All @@ -159,6 +159,7 @@ func registerMetrics(key string, metrics Metrics, f func() (*Response, error)) (
metrics.IncrCounter(fmt.Sprintf("%s.%s", key, "errors"))
}
}
metrics.IncrCounterWithAttrs(fmt.Sprintf("%s.%s", key, "total"), attrs)
}(resp, err)
}

Expand Down

0 comments on commit 44e584d

Please sign in to comment.