Add ssh metrics
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
// LoggingMiddleware is used for logging info about requests to the servers configured accesslogger.
|
||||
@@ -35,3 +36,46 @@ func (s *Server) LoggingMiddleware(next http.Handler) http.Handler {
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
|
||||
type MetricsMiddleware struct {
|
||||
requests *prometheus.CounterVec
|
||||
latency *prometheus.HistogramVec
|
||||
}
|
||||
|
||||
func (mm *MetricsMiddleware) handler(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
mm.requests.WithLabelValues(http.StatusText(ww.Status()), r.Method, r.URL.Path).Inc()
|
||||
mm.latency.WithLabelValues(http.StatusText(ww.Status()), r.Method, r.URL.Path).Observe(float64(time.Since(start).Milliseconds()))
|
||||
}()
|
||||
next.ServeHTTP(ww, r)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
|
||||
func NewMetricsMiddleware() func(next http.Handler) http.Handler {
|
||||
mm := &MetricsMiddleware{}
|
||||
mm.requests = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "filmblogg_http_requests_total",
|
||||
Help: "Total requests processed.",
|
||||
ConstLabels: prometheus.Labels{"service": "http"},
|
||||
},
|
||||
[]string{"code", "method", "path"},
|
||||
)
|
||||
|
||||
mm.latency = prometheus.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Name: "filmblogg_http_request_duration_milliseconds",
|
||||
Help: "Request processing time.",
|
||||
ConstLabels: prometheus.Labels{"service": "http"},
|
||||
Buckets: []float64{100, 500, 1500},
|
||||
},
|
||||
[]string{"code", "method", "path"},
|
||||
)
|
||||
prometheus.MustRegister(mm.requests)
|
||||
prometheus.MustRegister(mm.latency)
|
||||
return mm.handler
|
||||
}
|
||||
|
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/google/uuid"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.uio.no/torjus/apiary"
|
||||
"github.uio.no/torjus/apiary/config"
|
||||
"github.uio.no/torjus/apiary/honeypot/ssh"
|
||||
@@ -84,8 +85,10 @@ func NewServer(cfg config.FrontendConfig, hs *ssh.HoneypotServer, store store.Lo
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(s.LoggingMiddleware)
|
||||
r.Use(NewMetricsMiddleware())
|
||||
r.Use(middleware.SetHeader("Server", fmt.Sprintf("apiary/%s", apiary.FullVersion())))
|
||||
|
||||
r.Handle("/metrics", promhttp.Handler())
|
||||
r.Route("/", func(r chi.Router) {
|
||||
r.Get("/*", s.IndexHandler("web/vue-frontend/dist"))
|
||||
r.Get("/stream", s.HandlerAttemptStream)
|
||||
|
Reference in New Issue
Block a user