package main import ( "log" "net/http" "strings" "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) // TODO: more metrics var ( _promRegistry *prometheus.Registry _promHttpHandler http.Handler opsProcessed = prometheus.NewCounter(prometheus.CounterOpts{ Name: "processed_ops_total", Help: "The total number of processed requests", }) labelStringReplacer *strings.Replacer = strings.NewReplacer( "\"", "", "'", "", ) ) func setupPrometheus(r *gin.Engine) { _promRegistry = prometheus.NewRegistry() _promRegistry.MustRegister(opsProcessed) _promHttpHandler = promhttp.HandlerFor(_promRegistry, promhttp.HandlerOpts{ Registry: _promRegistry, EnableOpenMetrics: true, ErrorHandling: promhttp.ContinueOnError, ErrorLog: log.Default(), }) r.GET("/metrics", promHttpHandler) r.GET("/metrics/", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, "/metrics") }) } func promSanitizeLabel(str string, fallback string) string { out := strings.ToValidUTF8(str, "") out = labelStringReplacer.Replace(out) out = strings.TrimSpace(out) if out == "" { return fallback } return out } func promHttpHandler(c *gin.Context) { opsProcessed.Inc() _promHttpHandler.ServeHTTP(c.Writer, c.Request) }