Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add trace.Region in existing tracing middlewares #764

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-764.v2.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion witchcraft/internal/middleware/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package middleware

import (
"net/http"
"runtime/trace"
"strconv"
"time"

Expand Down Expand Up @@ -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()),
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions witchcraft/internal/middleware/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package middleware
import (
"context"
"net/http"
"runtime/trace"
"time"

"github.com/palantir/conjure-go-runtime/v2/conjure-go-contract/errors"
Expand Down Expand Up @@ -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)
}
}
Expand Down