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 11:59:16 +01:00
parent b76e90d289
commit 6d892b7f43
4 changed files with 77 additions and 13 deletions

64
cmd/karma/exporter.go Normal file
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)
}
}

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() {