Add metrics collector for HTTP status codes

This commit is contained in:
Ingo Oppermann
2023-09-29 17:18:59 +02:00
parent 40495e5ef0
commit 5b81e6e23f
5 changed files with 98 additions and 6 deletions

View File

@@ -32,6 +32,7 @@ import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/datarhei/core/v16/cluster"
cfgstore "github.com/datarhei/core/v16/config/store"
@@ -43,6 +44,7 @@ import (
api "github.com/datarhei/core/v16/http/handler/api"
httplog "github.com/datarhei/core/v16/http/log"
"github.com/datarhei/core/v16/http/router"
serverhandler "github.com/datarhei/core/v16/http/server"
"github.com/datarhei/core/v16/http/validator"
"github.com/datarhei/core/v16/iam"
"github.com/datarhei/core/v16/log"
@@ -104,10 +106,6 @@ type CorsConfig struct {
Origins []string
}
type Server interface {
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
type server struct {
logger log.Logger
@@ -154,6 +152,11 @@ type server struct {
profiling bool
readOnly bool
metrics struct {
lock sync.Mutex
status map[int]uint64
}
}
type filesystem struct {
@@ -164,7 +167,7 @@ type filesystem struct {
middleware echo.MiddlewareFunc
}
func NewServer(config Config) (Server, error) {
func NewServer(config Config) (serverhandler.Server, error) {
s := &server{
logger: config.Logger,
mimeTypesFile: config.MimeTypesFile,
@@ -172,6 +175,8 @@ func NewServer(config Config) (Server, error) {
readOnly: config.ReadOnly,
}
s.metrics.status = map[int]uint64{}
s.filesystems = map[string]*filesystem{}
corsPrefixes := map[string][]string{
@@ -327,6 +332,12 @@ func NewServer(config Config) (Server, error) {
s.middleware.log = mwlog.NewWithConfig(mwlog.Config{
Logger: s.logger,
Status: func(code int) {
s.metrics.lock.Lock()
defer s.metrics.lock.Unlock()
s.metrics.status[code]++
},
})
s.v3handler.widget = api.NewWidget(api.WidgetConfig{
@@ -458,6 +469,19 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}
func (s *server) HTTPStatus() map[int]uint64 {
status := map[int]uint64{}
s.metrics.lock.Lock()
defer s.metrics.lock.Unlock()
for code, value := range s.metrics.status {
status[code] = value
}
return status
}
func (s *server) setRoutes() {
gzipMiddleware := mwgzip.NewWithConfig(mwgzip.Config{
Level: mwgzip.BestSpeed,