chore: coding style, add code for prometheus (#4002)

This commit is contained in:
Kevin Wan
2024-03-13 20:00:35 +08:00
committed by GitHub
parent 09d7af76af
commit cc2a7e97f9
2 changed files with 28 additions and 27 deletions

View File

@@ -17,7 +17,7 @@ var (
Subsystem: "requests",
Name: "duration_ms",
Help: "http server requests duration(ms).",
Labels: []string{"path", "method"},
Labels: []string{"path", "method", "code"},
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 750, 1000},
})
@@ -26,7 +26,7 @@ var (
Subsystem: "requests",
Name: "code_total",
Help: "http server requests error count.",
Labels: []string{"path", "code", "method"},
Labels: []string{"path", "method", "code"},
})
)
@@ -37,8 +37,9 @@ func PrometheusHandler(path, method string) func(http.Handler) http.Handler {
startTime := timex.Now()
cw := response.NewWithCodeResponseWriter(w)
defer func() {
metricServerReqDur.Observe(timex.Since(startTime).Milliseconds(), path, method)
metricServerReqCodeTotal.Inc(path, strconv.Itoa(cw.Code), method)
code := strconv.Itoa(cw.Code)
metricServerReqDur.Observe(timex.Since(startTime).Milliseconds(), path, method, code)
metricServerReqCodeTotal.Inc(path, method, code)
}()
next.ServeHTTP(cw, r)