From 44e584d0a17ad41018ff72e2af7ac013c7954421 Mon Sep 17 00:00:00 2001 From: Joao Paulo Vieira Date: Wed, 3 May 2023 17:50:54 -0300 Subject: [PATCH] Add support to IncrCounterWithAttrs metrics --- metrics.go | 2 ++ request.go | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/metrics.go b/metrics.go index d65f1b4..0e605db 100644 --- a/metrics.go +++ b/metrics.go @@ -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) } diff --git a/request.go b/request.go index d79196b..5a81c5c 100644 --- a/request.go +++ b/request.go @@ -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 { @@ -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) }