Skip to content

Commit ebbced5

Browse files
committed
optimize code
1 parent 5ce1283 commit ebbced5

2 files changed

Lines changed: 16 additions & 25 deletions

File tree

pkg/metrics/http.go

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,26 @@ func (rt *InstrumentedRoundTripper) RoundTrip(request *http.Request) (*http.Resp
3131
response, err := rt.base.RoundTrip(request)
3232
duration := time.Since(startTime)
3333

34-
statusCode := ""
34+
statusCode := "network_error"
3535
if response != nil {
3636
statusCode = strconv.Itoa(response.StatusCode)
3737
}
3838

39-
HTTPRequestDurationHistogram.
40-
With(prometheus.Labels{
41-
apiLabel: rt.api,
42-
methodLabel: request.Method,
43-
operationLabel: operation,
44-
codeLabel: statusCode,
45-
}).
46-
Observe(float64(duration.Seconds()))
47-
HTTPRequestCount.
48-
With(prometheus.Labels{
49-
apiLabel: rt.api,
50-
methodLabel: request.Method,
51-
operationLabel: operation,
52-
codeLabel: statusCode,
53-
}).
54-
Inc()
55-
56-
if response != nil && response.StatusCode >= 400 {
57-
HTTPErrorCount.With(prometheus.Labels{
58-
apiLabel: rt.api,
59-
methodLabel: request.Method,
60-
operationLabel: operation,
61-
codeLabel: statusCode,
62-
}).Inc()
39+
labels := prometheus.Labels{
40+
apiLabel: rt.api,
41+
methodLabel: request.Method,
42+
operationLabel: operation,
43+
codeLabel: statusCode,
44+
}
45+
46+
HTTPRequestDurationHistogram.With(labels).Observe(duration.Seconds())
47+
HTTPRequestCount.With(labels).Inc()
48+
49+
isHTTPError := response != nil && response.StatusCode >= 400
50+
isNetworkError := err != nil
51+
52+
if isHTTPError || isNetworkError {
53+
HTTPErrorCount.With(labels).Inc()
6354
}
6455

6556
return response, err

pkg/stackit/client.go

Whitespace-only changes.

0 commit comments

Comments
 (0)