Skip to content

Commit b193269

Browse files
committed
fix linter and inefficencies
Signed-off-by: hfuss <[email protected]>
1 parent 5e772d4 commit b193269

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

pkg/ffresty/ffresty.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const (
4848
)
4949

5050
type retryCtxKey struct{}
51+
type hostCtxKey struct{}
5152

5253
type retryCtx struct {
5354
id string
@@ -112,21 +113,14 @@ func EnableClientMetrics(ctx context.Context, metricsRegistry metric.MetricsRegi
112113

113114
// create hooks
114115
onErrorMetricsHook := func(req *resty.Request, _ error) {
115-
method := req.Method
116-
u, _ := url.Parse(req.URL)
117-
host := u.Host
118116
// whilst there it is a possibility to get an response returned in the error here (and resty doc for OnError shows this) it seems to be a special case and the statuscode in such cases was not set.
119117
// therefore we log all cases as network_error we may in future find reason to extract more detail from the error
120-
metricsManager.IncCounterMetricWithLabels(ctx, metricsNetworkErrorsTotal, map[string]string{"host": host, "method": method}, nil)
118+
metricsManager.IncCounterMetricWithLabels(ctx, metricsNetworkErrorsTotal, map[string]string{"host": req.Context().Value(hostCtxKey{}).(string), "method": req.Method}, nil)
121119
}
122120
RegisterGlobalOnError(onErrorMetricsHook)
123121

124122
onSuccessMetricsHook := func(_ *resty.Client, resp *resty.Response) {
125-
method := resp.Request.Method
126-
u, _ := url.Parse(resp.Request.URL)
127-
host := u.Host
128-
code := resp.RawResponse.StatusCode
129-
metricsManager.IncCounterMetricWithLabels(ctx, metricsHTTPResponsesTotal, map[string]string{"status": fmt.Sprintf("%d", code), "error": "false", "host": host, "method": method}, nil)
123+
metricsManager.IncCounterMetricWithLabels(ctx, metricsHTTPResponsesTotal, map[string]string{"status": fmt.Sprintf("%d", resp.RawResponse.StatusCode), "error": "false", "host": resp.Request.Context().Value(hostCtxKey{}).(string), "method": resp.Request.Method}, nil)
130124
}
131125
RegisterGlobalOnSuccess(onSuccessMetricsHook)
132126
return nil
@@ -158,7 +152,7 @@ func OnAfterResponse(c *resty.Client, resp *resty.Response) {
158152
}
159153
log.L(rCtx).Logf(level, "<== %s %s [%d] (%.2fms)", resp.Request.Method, resp.Request.URL, status, elapsed/float64(time.Millisecond))
160154
if metricsManager != nil {
161-
metricsManager.ObserveSummaryMetricWithLabels(rCtx, metricsHTTPResponseTime, elapsed/float64(time.Second), map[string]string{"status": fmt.Sprintf("%d", status), "host": rCtx.Value("host").(string), "method": resp.Request.Method}, nil)
155+
metricsManager.ObserveSummaryMetricWithLabels(rCtx, metricsHTTPResponseTime, elapsed/float64(time.Second), map[string]string{"status": fmt.Sprintf("%d", status), "host": rCtx.Value(hostCtxKey{}).(string), "method": resp.Request.Method}, nil)
162156
}
163157
// TODO use req.TraceInfo() for richer metrics at the DNS and transport layer
164158
}
@@ -275,7 +269,7 @@ func NewWithConfig(ctx context.Context, ffrestyConfig Config) (client *resty.Cli
275269
// Record host in context to avoid redundant parses in hooks
276270
u, _ := url.Parse(req.URL)
277271
host := u.Host
278-
rCtx = context.WithValue(rCtx, "host", host)
272+
rCtx = context.WithValue(rCtx, hostCtxKey{}, host)
279273
req.SetContext(rCtx)
280274
}
281275

pkg/metric/prometheusMetricsManager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2023 Kaleido, Inc.
1+
// Copyright © 2024 Kaleido, Inc.
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//

0 commit comments

Comments
 (0)