2020-02-28 16:56:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-03 10:34:52 +00:00
|
|
|
"github.com/fasthttp/router"
|
2021-11-12 11:37:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gw/response"
|
2020-02-28 16:56:22 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/expfmt"
|
|
|
|
"github.com/valyala/fasthttp"
|
2022-03-25 13:06:33 +00:00
|
|
|
"go.uber.org/zap"
|
2020-02-28 16:56:22 +00:00
|
|
|
)
|
|
|
|
|
2022-03-25 13:06:33 +00:00
|
|
|
func attachMetrics(r *router.Router, l *zap.Logger) {
|
2022-03-25 13:12:26 +00:00
|
|
|
r.GET("/metrics/", metricsHandler(prometheus.DefaultGatherer, l))
|
2020-03-03 10:34:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 13:12:26 +00:00
|
|
|
func metricsHandler(reg prometheus.Gatherer, logger *zap.Logger) fasthttp.RequestHandler {
|
2020-02-28 16:56:22 +00:00
|
|
|
var (
|
|
|
|
inFlightSem chan struct{}
|
|
|
|
errCnt = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "promhttp_metric_handler_errors_total",
|
|
|
|
Help: "Total number of internal errors encountered by the promhttp metric handler.",
|
|
|
|
},
|
|
|
|
[]string{"cause"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
h := fasthttp.RequestHandler(func(c *fasthttp.RequestCtx) {
|
|
|
|
if inFlightSem != nil {
|
|
|
|
select {
|
|
|
|
case inFlightSem <- struct{}{}: // All good, carry on.
|
|
|
|
defer func() { <-inFlightSem }()
|
|
|
|
default:
|
2022-03-25 13:12:26 +00:00
|
|
|
response.Error(c, "Limit of concurrent requests reached, try again later.", fasthttp.StatusServiceUnavailable)
|
2020-02-28 16:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mfs, err := reg.Gather()
|
|
|
|
if err != nil {
|
2022-03-25 13:06:33 +00:00
|
|
|
if logger != nil {
|
2020-02-28 16:56:22 +00:00
|
|
|
panic("error gathering metrics:" + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
errCnt.WithLabelValues("gathering").Inc()
|
2022-03-25 13:12:26 +00:00
|
|
|
response.Error(c, err.Error(), fasthttp.StatusServiceUnavailable)
|
|
|
|
return
|
2020-02-28 16:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contentType := expfmt.FmtText
|
|
|
|
c.SetContentType(string(contentType))
|
|
|
|
enc := expfmt.NewEncoder(c, contentType)
|
|
|
|
|
|
|
|
var lastErr error
|
|
|
|
|
|
|
|
// handleError handles the error according to opts.ErrorHandling
|
|
|
|
// and returns true if we have to abort after the handling.
|
|
|
|
handleError := func(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
lastErr = err
|
2022-03-25 13:06:33 +00:00
|
|
|
if logger != nil {
|
|
|
|
logger.Error("encoding and sending metric family", zap.Error(err))
|
2020-02-28 16:56:22 +00:00
|
|
|
}
|
|
|
|
errCnt.WithLabelValues("encoding").Inc()
|
2022-03-25 13:12:26 +00:00
|
|
|
response.Error(c, err.Error(), fasthttp.StatusServiceUnavailable)
|
|
|
|
return true
|
2020-02-28 16:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, mf := range mfs {
|
|
|
|
if handleError(enc.Encode(mf)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if closer, ok := enc.(expfmt.Closer); ok {
|
|
|
|
// This in particular takes care of the final "# EOF\n" line for OpenMetrics.
|
|
|
|
if handleError(closer.Close()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(lastErr)
|
|
|
|
})
|
|
|
|
|
2022-03-25 13:12:26 +00:00
|
|
|
return h
|
2020-02-28 16:56:22 +00:00
|
|
|
}
|