mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-21 21:12:34 +02:00

* cleanup code for URL type * fix makefile for trace mode * refactor, merge Entry, RawEntry and Route into one. * Implement fileserver. * refactor: rename HTTPRoute to ReverseProxyRoute to avoid confusion * refactor: move metrics logger to middleware package - fix prometheus metrics for load balanced routes - route will now fail when health monitor fail to start * fix extra output of ls-* commands by defer initializaing stuff, speed up start time * add test for path traversal attack, small fix on FileServer.Start method * rename rule.on.bypass to pass * refactor and fixed map-to-map deserialization * updated route loading logic * schemas: add "add_prefix" option to modify_request middleware * updated route JSONMarshalling --------- Co-authored-by: yusing <yusing@6uo.me>
47 lines
1 KiB
Go
47 lines
1 KiB
Go
package metricslogger
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/yusing/go-proxy/internal/metrics"
|
|
)
|
|
|
|
type httpMetricLogger struct {
|
|
http.ResponseWriter
|
|
timestamp time.Time
|
|
labels *metrics.HTTPRouteMetricLabels
|
|
}
|
|
|
|
// WriteHeader implements http.ResponseWriter.
|
|
func (l *httpMetricLogger) WriteHeader(status int) {
|
|
l.ResponseWriter.WriteHeader(status)
|
|
duration := time.Since(l.timestamp)
|
|
go func() {
|
|
m := metrics.GetRouteMetrics()
|
|
m.HTTPReqTotal.Inc()
|
|
m.HTTPReqElapsed.With(l.labels).Set(float64(duration.Milliseconds()))
|
|
|
|
// ignore 1xx
|
|
switch {
|
|
case status >= 500:
|
|
m.HTTP5xx.With(l.labels).Inc()
|
|
case status >= 400:
|
|
m.HTTP4xx.With(l.labels).Inc()
|
|
case status >= 200:
|
|
m.HTTP2xx3xx.With(l.labels).Inc()
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (l *httpMetricLogger) Unwrap() http.ResponseWriter {
|
|
return l.ResponseWriter
|
|
}
|
|
|
|
func newHTTPMetricLogger(w http.ResponseWriter, labels *metrics.HTTPRouteMetricLabels) *httpMetricLogger {
|
|
return &httpMetricLogger{
|
|
ResponseWriter: w,
|
|
timestamp: time.Now(),
|
|
labels: labels,
|
|
}
|
|
}
|