bunrouterotel/now can get HTTP path from req.Request.URL.Path;

develop bunrouterotel/v0.2.0
Ge Song 11 months ago
parent a278d10e4c
commit 2963386f32

@ -13,7 +13,7 @@ import (
const ( const (
tracerName = "git.blauwelle.com/go/crate/bunrouterotel" tracerName = "git.blauwelle.com/go/crate/bunrouterotel"
version = "0.1.0" version = "0.2.0"
) )
type config struct { type config struct {
@ -53,18 +53,21 @@ func Middleware(serverName string, opts ...Option) bunrouter.MiddlewareFunc {
var spanName string var spanName string
params := req.Params() params := req.Params()
route := params.Route() route := params.Route()
if route != "" { if route == "" {
spanName = req.Method + " " + route if req.Request != nil && req.Request.URL != nil && req.Request.URL.Path != "" {
} else { route = req.Request.URL.Path
spanName = "HTTP " + req.Method + " route not found" } 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.NetAttributesFromHTTPRequest("tcp", req.Request)...),
trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(req.Request)...), trace.WithAttributes(semconv.EndUserAttributesFromHTTPRequest(req.Request)...),
trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(serverName, route, req.Request)...), trace.WithAttributes(semconv.HTTPServerAttributesFromHTTPRequest(serverName, route, req.Request)...),
trace.WithSpanKind(trace.SpanKindServer), trace.WithSpanKind(trace.SpanKindServer),
} }
ctx, span := tracer.Start(ctx, spanName, tarcerStartOpts...) ctx, span := tracer.Start(ctx, spanName, tracerStartOpts...)
req.Request = req.Request.WithContext(ctx) req.Request = req.Request.WithContext(ctx)
paramSlice := params.Slice() paramSlice := params.Slice()
attrs := make([]attribute.KeyValue, 0, len(paramSlice)) attrs := make([]attribute.KeyValue, 0, len(paramSlice))

Loading…
Cancel
Save