mirror of
https://github.com/datarhei/core.git
synced 2025-10-05 16:07:07 +08:00
Add metrics collector for HTTP status codes
This commit is contained in:
@@ -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,
|
||||
|
Reference in New Issue
Block a user