fix(backend): don't export endpoint paths in metrics

/metrics currently exposes all paths, which causes high cardinality. Switch to exporting only handler names
This commit is contained in:
Łukasz Mierzwa
2019-10-15 12:25:15 +01:00
parent b76e90d289
commit 6d892b7f43
4 changed files with 77 additions and 13 deletions
+64
View File
@@ -0,0 +1,64 @@
package main
// stripped down version of https://github.com/chenjiandongx/ginprom
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
)
var (
labels = []string{"status", "handler", "method"}
reqCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_request_count_total",
Help: "Total number of HTTP requests made.",
}, labels,
)
reqDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "HTTP request latencies in seconds.",
}, labels,
)
respSizeBytes = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "http_response_size_bytes",
Help: "HTTP response sizes in bytes.",
}, labels,
)
)
func init() {
prometheus.MustRegister(reqCount, reqDuration, respSizeBytes)
}
func promMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
status := fmt.Sprintf("%d", c.Writer.Status())
handler := c.HandlerName()
method := c.Request.Method
lvs := []string{status, handler, method}
reqCount.WithLabelValues(lvs...).Inc()
reqDuration.WithLabelValues(lvs...).Observe(time.Since(start).Seconds())
respSizeBytes.WithLabelValues(lvs...).Observe(float64(c.Writer.Size()))
}
}
func promHandler(handler http.Handler) gin.HandlerFunc {
return func(c *gin.Context) {
handler.ServeHTTP(c.Writer, c.Request)
}
}
+13 -10
View File
@@ -23,10 +23,10 @@ import (
"github.com/gin-contrib/static"
"github.com/gin-gonic/contrib/sentry"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/pflag"
raven "github.com/getsentry/raven-go"
ginprometheus "github.com/mcuadros/go-gin-prometheus"
cache "github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
@@ -57,6 +57,14 @@ func getViewURL(sub string) string {
return u
}
func customCSS(c *gin.Context) {
serveFileOr404(config.Config.Custom.CSS, "text/css", c)
}
func customJS(c *gin.Context) {
serveFileOr404(config.Config.Custom.JS, "application/javascript", c)
}
func setupRouter(router *gin.Engine) {
router.Use(gzip.Gzip(gzip.DefaultCompression))
@@ -94,20 +102,15 @@ func setupRouter(router *gin.Engine) {
router.GET(getViewURL("/labelNames.json"), knownLabelNames)
router.GET(getViewURL("/labelValues.json"), knownLabelValues)
router.GET(getViewURL("/custom.css"), func(c *gin.Context) {
serveFileOr404(config.Config.Custom.CSS, "text/css", c)
})
router.GET(getViewURL("/custom.js"), func(c *gin.Context) {
serveFileOr404(config.Config.Custom.JS, "application/javascript", c)
})
router.GET(getViewURL("/custom.css"), customCSS)
router.GET(getViewURL("/custom.js"), customJS)
router.NoRoute(notFound)
}
func setupMetrics(router *gin.Engine) {
prom := ginprometheus.NewPrometheus("gin")
prom.MetricsPath = getViewURL("/metrics")
prom.Use(router)
router.Use(promMiddleware())
router.GET(getViewURL("/metrics"), promHandler(promhttp.Handler()))
}
func setupUpstreams() {
-1
View File
@@ -24,7 +24,6 @@ require (
github.com/google/go-cmp v0.3.1
github.com/hansrodtang/randomcolor v0.0.0-20160512071917-d27108b3d7a5
github.com/jarcoal/httpmock v1.0.4
github.com/mcuadros/go-gin-prometheus v0.1.1-0.20190723203314-c7374e9082f8
github.com/patrickmn/go-cache v2.1.1-0.20180815053127-5633e0862627+incompatible
github.com/pmezard/go-difflib v1.0.0
github.com/prometheus/client_golang v1.1.1-0.20191002102308-9a37535b1ba5
-2
View File
@@ -280,8 +280,6 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mcuadros/go-gin-prometheus v0.1.1-0.20190723203314-c7374e9082f8 h1:8/I9igpury40XEhc6fqut/jnjE3KEMGNyfbjxwbOv/o=
github.com/mcuadros/go-gin-prometheus v0.1.1-0.20190723203314-c7374e9082f8/go.mod h1:ezECAsiHtCRIa+6Ii8THg7G7RJvpO4S19d499UkEE3s=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=