MSP-6493 add http metrics middleware
This commit is contained in:
1 parent
e9c5c9b805
commit
16cdb18f43
2 files changed
+37
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package assembly
|
||||
|
||||
import (
|
||||
"github.com/txix-open/isp-kit/metrics"
|
||||
"github.com/txix-open/isp-kit/metrics/http_metrics"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -94,6 +96,8 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Ha
|
||||
forwardReqIdByAppId[setting.ApplicationId] = setting.ForwardRequestId
|
||||
}
|
||||
|
||||
metricsStorage := http_metrics.NewServerStorage(metrics.DefaultRegistry)
|
||||
|
||||
handler := middleware.Chain(
|
||||
proxyFunc,
|
||||
middleware.Logger(
|
||||
@@ -110,6 +114,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Ha
|
||||
middleware.Throttling(throttlingService),
|
||||
middleware.DailyLimit(dailyLimitService),
|
||||
middleware.RequestId(config.EnableClientRequestIdForwarding, forwardReqIdByAppId),
|
||||
middleware.Metrics(metricsStorage),
|
||||
)
|
||||
if location.SkipAuth {
|
||||
handler = middleware.Chain(
|
||||
@@ -121,6 +126,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Ha
|
||||
),
|
||||
middleware.ErrorHandler(l.logger),
|
||||
middleware.RequestId(config.EnableClientRequestIdForwarding, forwardReqIdByAppId),
|
||||
middleware.Metrics(metricsStorage),
|
||||
)
|
||||
}
|
||||
entrypoint := middleware.Entrypoint(
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"isp-gate-service/request"
|
||||
"time"
|
||||
|
||||
"github.com/txix-open/isp-kit/metrics/http_metrics"
|
||||
)
|
||||
|
||||
func Metrics(storage *http_metrics.ServerStorage) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *request.Context) error {
|
||||
endpoint := ctx.Endpoint()
|
||||
if endpoint == "" {
|
||||
return next.Handle(ctx)
|
||||
}
|
||||
|
||||
r := ctx.Request()
|
||||
|
||||
start := time.Now()
|
||||
err := next.Handle(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
storage.ObserveDuration(r.Method, endpoint, time.Since(start))
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user