mirror of
https://github.com/prymitive/karma
synced 2026-05-11 03:46:48 +00:00
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:
64
cmd/karma/exporter.go
Normal file
64
cmd/karma/exporter.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user