Skip to content

Commit

Permalink
feat(pkg/server/ginserver): more metric
Browse files Browse the repository at this point in the history
  • Loading branch information
amazing-gao committed Aug 18, 2021
1 parent fbb1f6a commit 63d6f38
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 25 deletions.
71 changes: 46 additions & 25 deletions pkg/server/ginserver/mid/ginprom/ginprom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ginprom

import (
"strconv"
"strings"
"time"

"github.com/boxgo/box/pkg/metric"
Expand All @@ -12,25 +11,54 @@ import (
type (
GinProm struct {
cfg *Config
reqCounter *metric.CounterVec
reqSizeSummary *metric.SummaryVec
reqBeginCounter *metric.CounterVec
reqFinishCounter *metric.CounterVec
reqDurationSummary *metric.SummaryVec
resSizeSummary *metric.SummaryVec
}
)

func newGinProm(c *Config) *GinProm {
return &GinProm{
cfg: c,
reqCounter: metric.NewCounterVec(
"http_request_total",
"How many HTTP requests processed, partitioned by status code and HTTP method.",
[]string{"status", "retcode", "method", "url", "handler"},
reqSizeSummary: metric.NewSummaryVec(
"http_server_request_size_bytes",
"The HTTP request sizes in bytes.",
[]string{"method", "url"},
map[float64]float64{
0.5: 0.05,
0.75: 0.05,
0.9: 0.01,
0.99: 0.001,
},
),
reqBeginCounter: metric.NewCounterVec(
"http_server_request_begin_total",
"How many HTTP requests ready to process.",
[]string{"method", "url"},
),
reqFinishCounter: metric.NewCounterVec(
"http_server_request_finish_total",
"How many HTTP requests processed.",
[]string{"method", "url", "status", "errcode"},
),
reqDurationSummary: metric.NewSummaryVec(
"http_request_duration_seconds",
"http_server_request_duration_seconds",
"The HTTP request latencies in seconds.",
[]string{"status", "retcode", "method", "url", "handler"},
[]string{"method", "url", "status", "errcode"},
map[float64]float64{
0.5: 0.05,
0.75: 0.05,
0.9: 0.01,
0.99: 0.001,
},
),
resSizeSummary: metric.NewSummaryVec(
"http_server_response_size_bytes",
"The HTTP response sizes in bytes.",
[]string{"method", "url", "status", "errcode"},
map[float64]float64{
0.25: 0.05,
0.5: 0.05,
0.75: 0.05,
0.9: 0.01,
Expand All @@ -43,28 +71,21 @@ func newGinProm(c *Config) *GinProm {
func (prom *GinProm) Handler() gin.HandlerFunc {
return func(ctx *gin.Context) {
start := time.Now()

ctx.Next()

labels := []string{
strconv.Itoa(ctx.Writer.Status()),
strconv.Itoa(ctx.GetInt("retcode")),
ctx.Request.Method,
prom.cfg.requestURLMappingFn(ctx),
getHandlerName(ctx),
}

prom.reqCounter.WithLabelValues(labels...).Inc()
prom.reqDurationSummary.WithLabelValues(labels...).Observe(time.Since(start).Seconds())
}
}
reqSz := computeApproximateRequestSize(ctx.Request)

func getHandlerName(c *gin.Context) string {
longName := c.HandlerName()
shortName := longName
prom.reqSizeSummary.WithLabelValues(labels...).Observe(reqSz)
prom.reqBeginCounter.WithLabelValues(labels...).Inc()

if idx := strings.LastIndex(longName, "."); idx != -1 {
shortName = longName[idx+1:]
ctx.Next()

labels = append(labels, strconv.Itoa(ctx.Writer.Status()), strconv.Itoa(ctx.GetInt("errcode")))
prom.resSizeSummary.WithLabelValues(labels...).Observe(float64(ctx.Writer.Size()))
prom.reqFinishCounter.WithLabelValues(labels...).Inc()
prom.reqDurationSummary.WithLabelValues(labels...).Observe(time.Since(start).Seconds())
}
return shortName
}
28 changes: 28 additions & 0 deletions pkg/server/ginserver/mid/ginprom/reqsize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ginprom

import "net/http"

// From https://github.com/DanielHeckrath/gin-prometheus/blob/master/gin_prometheus.go#L112-L129
func computeApproximateRequestSize(r *http.Request) float64 {
s := 0
if r.URL != nil {
s = len(r.URL.Path)
}

s += len(r.Method)
s += len(r.Proto)
for name, values := range r.Header {
s += len(name)
for _, value := range values {
s += len(value)
}
}
s += len(r.Host)

// N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.

if r.ContentLength != -1 {
s += int(r.ContentLength)
}
return float64(s)
}

0 comments on commit 63d6f38

Please sign in to comment.