Add metrics endpoint with http metrics using Prometheus client lib

This commit is contained in:
Markus Muellner
2022-03-15 20:52:47 +01:00
committed by Benj Fassbind
parent 8046fb1eb9
commit 6539e1b856
12 changed files with 561 additions and 40 deletions
+18
View File
@@ -5,10 +5,17 @@ import (
ctx "github.com/aptly-dev/aptly/context"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var context *ctx.AptlyContext
func apiMetricsGet() gin.HandlerFunc {
return func(c *gin.Context) {
promhttp.Handler().ServeHTTP(c.Writer, c.Request)
}
}
// Router returns prebuilt with routes http.Handler
func Router(c *ctx.AptlyContext) http.Handler {
context = c
@@ -16,6 +23,14 @@ func Router(c *ctx.AptlyContext) http.Handler {
router := gin.Default()
router.Use(gin.ErrorLogger())
if c.Config().EnableMetricsEndpoint {
router.Use(instrumentHandlerInFlight(apiRequestsInFlightGauge, getBasePath))
router.Use(instrumentHandlerCounter(apiRequestsTotalCounter, getBasePath))
router.Use(instrumentHandlerRequestSize(apiRequestSizeSummary, getBasePath))
router.Use(instrumentHandlerResponseSize(apiResponseSizeSummary, getBasePath))
router.Use(instrumentHandlerDuration(apiRequestsDurationSummary, getBasePath))
}
if context.Flags().Lookup("no-lock").Value.Get().(bool) {
// We use a goroutine to count the number of
// concurrent requests. When no more requests are
@@ -51,6 +66,9 @@ func Router(c *ctx.AptlyContext) http.Handler {
root := router.Group("/api")
{
if c.Config().EnableMetricsEndpoint {
root.GET("/metrics", apiMetricsGet())
}
root.GET("/version", apiVersion)
}