From 2963386f3230b3cd19fce6a5e73f26461aef5cf3 Mon Sep 17 00:00:00 2001 From: Ge Song Date: Fri, 5 Jan 2024 16:45:33 +0800 Subject: [PATCH] bunrouterotel/now can get HTTP path from req.Request.URL.Path; --- bunrouterotel/middleware.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bunrouterotel/middleware.go b/bunrouterotel/middleware.go index a416a05..f9c3e84 100644 --- a/bunrouterotel/middleware.go +++ b/bunrouterotel/middleware.go @@ -13,7 +13,7 @@ import ( const ( tracerName = "git.blauwelle.com/go/crate/bunrouterotel" - version = "0.1.0" + version = "0.2.0" ) type config struct { @@ -53,18 +53,21 @@ func Middleware(serverName string, opts ...Option) bunrouter.MiddlewareFunc { var spanName string params := req.Params() route := params.Route() - if route != "" { - spanName = req.Method + " " + route - } else { - spanName = "HTTP " + req.Method + " route not found" + if route == "" { + if req.Request != nil && req.Request.URL != nil && req.Request.URL.Path != "" { + route = req.Request.URL.Path + } else { + route = "route not found" + } } - tarcerStartOpts := []trace.SpanStartOption{ + spanName = req.Method + " " + route + tracerStartOpts := []trace.SpanStartOption{ trace.WithAttributes(semconv.NetAttributesFromHTTPRequest("tcp", req.Request)...), trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(req.Request)...), trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(serverName, route, req.Request)...), trace.WithSpanKind(trace.SpanKindServer), } - ctx, span := tracer.Start(ctx, spanName, tarcerStartOpts...) + ctx, span := tracer.Start(ctx, spanName, tracerStartOpts...) req.Request = req.Request.WithContext(ctx) paramSlice := params.Slice() attrs := make([]attribute.KeyValue, 0, len(paramSlice))