-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(httpclient): Added metrics transport (#108)
- Loading branch information
Showing
7 changed files
with
354 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package status | ||
|
||
// NormalizeHTTPStatus normalizes an HTTP status code. | ||
func NormalizeHTTPStatus(status int) string { | ||
switch { | ||
case status < 200: | ||
return "1xx" | ||
case status >= 200 && status < 300: | ||
return "2xx" | ||
case status >= 300 && status < 400: | ||
return "3xx" | ||
case status >= 400 && status < 500: | ||
return "4xx" | ||
default: | ||
return "5xx" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package status_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ankorstore/yokai/httpclient/status" | ||
) | ||
|
||
func TestNormalizeHTTPStatus(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
code int | ||
want string | ||
}{ | ||
{"1xx normalization", 101, "1xx"}, | ||
{"2xx normalization", 202, "2xx"}, | ||
{"3xx normalization", 303, "3xx"}, | ||
{"4xx normalization", 404, "4xx"}, | ||
{"5xx normalization", 505, "5xx"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
got := status.NormalizeHTTPStatus(tt.code) | ||
|
||
if got != tt.want { | ||
t.Errorf("expected %s, got %s", tt.want, got) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package transport | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/ankorstore/yokai/httpclient/status" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const ( | ||
HttpClientMetricsRequestsCount = "client_requests_total" | ||
HttpClientMetricsRequestsDuration = "client_request_duration_seconds" | ||
) | ||
|
||
// MetricsTransport is a wrapper around [http.RoundTripper] with some [MetricsTransportConfig] configuration. | ||
type MetricsTransport struct { | ||
transport http.RoundTripper | ||
config *MetricsTransportConfig | ||
requestsCounter *prometheus.CounterVec | ||
requestsDuration *prometheus.HistogramVec | ||
} | ||
|
||
// MetricsTransportConfig is the configuration of the [MetricsTransport]. | ||
type MetricsTransportConfig struct { | ||
Registry prometheus.Registerer | ||
Namespace string | ||
Subsystem string | ||
Buckets []float64 | ||
NormalizeHTTPStatus bool | ||
} | ||
|
||
// NewMetricsTransport returns a [MetricsTransport] instance with default [MetricsTransportConfig] configuration. | ||
func NewMetricsTransport(base http.RoundTripper) *MetricsTransport { | ||
return NewMetricsTransportWithConfig( | ||
base, | ||
&MetricsTransportConfig{ | ||
Registry: prometheus.DefaultRegisterer, | ||
Namespace: "", | ||
Subsystem: "", | ||
Buckets: prometheus.DefBuckets, | ||
NormalizeHTTPStatus: true, | ||
}, | ||
) | ||
} | ||
|
||
// NewMetricsTransportWithConfig returns a [MetricsTransport] instance for a provided [MetricsTransportConfig] configuration. | ||
func NewMetricsTransportWithConfig(base http.RoundTripper, config *MetricsTransportConfig) *MetricsTransport { | ||
if base == nil { | ||
base = NewBaseTransport() | ||
} | ||
|
||
if config.Registry == nil { | ||
config.Registry = prometheus.DefaultRegisterer | ||
} | ||
|
||
requestsCounter := prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: config.Namespace, | ||
Subsystem: config.Subsystem, | ||
Name: HttpClientMetricsRequestsCount, | ||
Help: "Number of performed HTTP requests", | ||
}, | ||
[]string{ | ||
"url", | ||
"method", | ||
"status", | ||
}, | ||
) | ||
|
||
requestsDuration := prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Namespace: config.Namespace, | ||
Subsystem: config.Subsystem, | ||
Name: HttpClientMetricsRequestsDuration, | ||
Help: "Time spent performing HTTP requests", | ||
Buckets: config.Buckets, | ||
}, | ||
[]string{ | ||
"url", | ||
"method", | ||
}, | ||
) | ||
|
||
config.Registry.MustRegister(requestsCounter, requestsDuration) | ||
|
||
return &MetricsTransport{ | ||
transport: base, | ||
config: config, | ||
requestsCounter: requestsCounter, | ||
requestsDuration: requestsDuration, | ||
} | ||
} | ||
|
||
// Base returns the wrapped [http.RoundTripper]. | ||
func (t *MetricsTransport) Base() http.RoundTripper { | ||
return t.transport | ||
} | ||
|
||
// RoundTrip performs a request / response round trip, based on the wrapped [http.RoundTripper]. | ||
func (t *MetricsTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
timer := prometheus.NewTimer(t.requestsDuration.WithLabelValues(req.URL.String(), req.Method)) | ||
resp, err := t.transport.RoundTrip(req) | ||
timer.ObserveDuration() | ||
|
||
respStatus := "" | ||
if t.config.NormalizeHTTPStatus { | ||
respStatus = status.NormalizeHTTPStatus(resp.StatusCode) | ||
} else { | ||
respStatus = strconv.Itoa(resp.StatusCode) | ||
} | ||
|
||
t.requestsCounter.WithLabelValues(req.URL.String(), req.Method, respStatus).Inc() | ||
|
||
return resp, err | ||
} |
Oops, something went wrong.