diff --git a/changelog/@unreleased/pr-764.v2.yml b/changelog/@unreleased/pr-764.v2.yml new file mode 100644 index 000000000..69de20df2 --- /dev/null +++ b/changelog/@unreleased/pr-764.v2.yml @@ -0,0 +1,5 @@ +type: feature +feature: + description: Add trace.Region in existing tracing middlewares + links: + - https://github.com/palantir/witchcraft-go-server/pull/764 diff --git a/witchcraft/internal/middleware/request.go b/witchcraft/internal/middleware/request.go index 3ed6412a5..791a8e3d0 100644 --- a/witchcraft/internal/middleware/request.go +++ b/witchcraft/internal/middleware/request.go @@ -16,6 +16,7 @@ package middleware import ( "net/http" + "runtime/trace" "strconv" "time" @@ -137,7 +138,8 @@ func NewRequestExtractIDs( // retrieve existing trace info from request and create a span reqSpanContext := b3.SpanExtractor(req)() - span := tracer.StartSpan("witchcraft-go-server request middleware", + spanName := "witchcraft-go-server request middleware" + span := tracer.StartSpan(spanName, wtracing.WithParentSpanContext(reqSpanContext), wtracing.WithSpanTag("http.method", req.Method), wtracing.WithSpanTag("http.useragent", req.UserAgent()), @@ -150,6 +152,9 @@ func NewRequestExtractIDs( // update request with new context req = req.WithContext(ctx) + // Create a go execution trace region for the request middleware + defer trace.StartRegion(ctx, spanName).End() + // delegate to the next handler lrw := toLoggingResponseWriter(rw) next.ServeHTTP(lrw, req) diff --git a/witchcraft/internal/middleware/route.go b/witchcraft/internal/middleware/route.go index 2421a965d..116d432ff 100644 --- a/witchcraft/internal/middleware/route.go +++ b/witchcraft/internal/middleware/route.go @@ -17,6 +17,7 @@ package middleware import ( "context" "net/http" + "runtime/trace" "time" "github.com/palantir/conjure-go-runtime/v2/conjure-go-contract/errors" @@ -109,6 +110,9 @@ func NewRouteLogTraceSpan() wrouter.RouteHandlerMiddleware { req = req.WithContext(ctx) b3.SpanInjector(req)(span.Context()) + // Create a go execution trace region for the route + defer trace.StartRegion(ctx, spanName).End() + next(rw, req, reqVals) } }