mirror of
https://github.com/1f349/violet.git
synced 2024-11-21 19:01:39 +00:00
Add host to metric middlewares
This commit is contained in:
parent
e901a73129
commit
a13db89c44
@ -27,9 +27,12 @@ func NewHttpsServer(conf *conf.Conf, registry *prometheus.Registry) *http.Server
|
|||||||
favMiddleware := setupFaviconMiddleware(conf.Favicons, r)
|
favMiddleware := setupFaviconMiddleware(conf.Favicons, r)
|
||||||
rateLimiter := setupRateLimiter(conf.RateLimit, favMiddleware)
|
rateLimiter := setupRateLimiter(conf.RateLimit, favMiddleware)
|
||||||
metricsMiddleware := metrics.New(registry, nil).WrapHandler("violet-https", rateLimiter)
|
metricsMiddleware := metrics.New(registry, nil).WrapHandler("violet-https", rateLimiter)
|
||||||
|
metricsMeta := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
metricsMiddleware.ServeHTTP(rw, metrics.AddHostCtx(req))
|
||||||
|
})
|
||||||
hsts := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
hsts := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||||
rw.Header().Set("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
|
rw.Header().Set("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
|
||||||
metricsMiddleware.ServeHTTP(rw, req)
|
metricsMeta.ServeHTTP(rw, req)
|
||||||
})
|
})
|
||||||
|
|
||||||
return &http.Server{
|
return &http.Server{
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
@ -46,7 +47,7 @@ func (m *middleware) WrapHandler(handlerName string, handler http.Handler) http.
|
|||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
Name: "http_requests_total",
|
Name: "http_requests_total",
|
||||||
Help: "Tracks the number of HTTP requests.",
|
Help: "Tracks the number of HTTP requests.",
|
||||||
}, []string{"method", "code"},
|
}, []string{"method", "code", "host"},
|
||||||
)
|
)
|
||||||
requestDuration := promauto.With(reg).NewHistogramVec(
|
requestDuration := promauto.With(reg).NewHistogramVec(
|
||||||
prometheus.HistogramOpts{
|
prometheus.HistogramOpts{
|
||||||
@ -54,23 +55,28 @@ func (m *middleware) WrapHandler(handlerName string, handler http.Handler) http.
|
|||||||
Help: "Tracks the latencies for HTTP requests.",
|
Help: "Tracks the latencies for HTTP requests.",
|
||||||
Buckets: m.buckets,
|
Buckets: m.buckets,
|
||||||
},
|
},
|
||||||
[]string{"method", "code"},
|
[]string{"method", "code", "host"},
|
||||||
)
|
)
|
||||||
requestSize := promauto.With(reg).NewSummaryVec(
|
requestSize := promauto.With(reg).NewSummaryVec(
|
||||||
prometheus.SummaryOpts{
|
prometheus.SummaryOpts{
|
||||||
Name: "http_request_size_bytes",
|
Name: "http_request_size_bytes",
|
||||||
Help: "Tracks the size of HTTP requests.",
|
Help: "Tracks the size of HTTP requests.",
|
||||||
},
|
},
|
||||||
[]string{"method", "code"},
|
[]string{"method", "code", "host"},
|
||||||
)
|
)
|
||||||
responseSize := promauto.With(reg).NewSummaryVec(
|
responseSize := promauto.With(reg).NewSummaryVec(
|
||||||
prometheus.SummaryOpts{
|
prometheus.SummaryOpts{
|
||||||
Name: "http_response_size_bytes",
|
Name: "http_response_size_bytes",
|
||||||
Help: "Tracks the size of HTTP responses.",
|
Help: "Tracks the size of HTTP responses.",
|
||||||
},
|
},
|
||||||
[]string{"method", "code"},
|
[]string{"method", "code", "host"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
hostCtxGetter := promhttp.WithLabelFromCtx("host", func(ctx context.Context) string {
|
||||||
|
s, _ := ctx.Value(hostCtxKey(0)).(string)
|
||||||
|
return s
|
||||||
|
})
|
||||||
|
|
||||||
// Wraps the provided http.Handler to observe the request result with the provided metrics.
|
// Wraps the provided http.Handler to observe the request result with the provided metrics.
|
||||||
base := promhttp.InstrumentHandlerCounter(
|
base := promhttp.InstrumentHandlerCounter(
|
||||||
requestsTotal,
|
requestsTotal,
|
||||||
@ -81,9 +87,13 @@ func (m *middleware) WrapHandler(handlerName string, handler http.Handler) http.
|
|||||||
promhttp.InstrumentHandlerResponseSize(
|
promhttp.InstrumentHandlerResponseSize(
|
||||||
responseSize,
|
responseSize,
|
||||||
handler,
|
handler,
|
||||||
|
hostCtxGetter,
|
||||||
),
|
),
|
||||||
|
hostCtxGetter,
|
||||||
),
|
),
|
||||||
|
hostCtxGetter,
|
||||||
),
|
),
|
||||||
|
hostCtxGetter,
|
||||||
)
|
)
|
||||||
|
|
||||||
return base.ServeHTTP
|
return base.ServeHTTP
|
||||||
@ -100,3 +110,9 @@ func New(registry prometheus.Registerer, buckets []float64) Middleware {
|
|||||||
registry: registry,
|
registry: registry,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type hostCtxKey uint8
|
||||||
|
|
||||||
|
func AddHostCtx(req *http.Request) *http.Request {
|
||||||
|
return req.WithContext(context.WithValue(req.Context(), hostCtxKey(0), req.Host))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user