From 1c575a19c9186df19f4d65060c401bca3adbd19b Mon Sep 17 00:00:00 2001 From: joelei Date: Sat, 28 Dec 2024 22:56:54 +0800 Subject: [PATCH] feat: add tracingHandler --- http/rest/generic.go | 5 +++++ http/rest/metrics.go | 11 +++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/http/rest/generic.go b/http/rest/generic.go index 816af23..b1ae6f1 100644 --- a/http/rest/generic.go +++ b/http/rest/generic.go @@ -59,6 +59,9 @@ func Handle[In any, Out any](fn UnaryFunc[In, Out]) func(w http.ResponseWriter, } _ = render.Render(w, r, APIOK(out)) } + + f = tracingHandler(handleName, f) + return f } @@ -106,6 +109,8 @@ func Stream[In any](fn StreamFunc[In]) func(w http.ResponseWriter, r *http.Reque _ = render.Render(w, r, APIError(err)) } } + + f = tracingHandler(handleName, f) return f } diff --git a/http/rest/metrics.go b/http/rest/metrics.go index ab1d6be..bd46dfc 100644 --- a/http/rest/metrics.go +++ b/http/rest/metrics.go @@ -56,14 +56,9 @@ func collectHandleMetrics(funcName, method string, st time.Time, err error) { responseTimeDuration.WithLabelValues(funcName, method, codeStr).Observe(duration) } -// TracingHandler -func tracingHandler(operation string) func(next http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - next.ServeHTTP(w, r) - } - return otelhttp.NewHandler(http.HandlerFunc(fn), operation) - } +// tracingHandler +func tracingHandler(operation string, fn func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { + return otelhttp.NewHandler(http.HandlerFunc(fn), operation).ServeHTTP } func init() {