Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions proxy/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type context struct {
logger filters.FilterContextLogger
proxyWatch stopWatch
proxyRequestLatency time.Duration
goCtx *stdlibcontext.Context
}

type filterMetrics struct {
Expand Down
17 changes: 17 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/url"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -1200,6 +1201,8 @@ func (p *Proxy) do(ctx *context, parentSpan ot.Span) (err error) {
err = perr
}
}()
traceIDCtx, _ := pprof.Label(*ctx.goCtx, "trace_id")
ctx.Logger().Debugf("trace id in goroutine context: %s", traceIDCtx)

if ctx.executionCounter > p.maxLoops {
// TODO(sszuecs): think about setting status code to 463 or 465 (check what AWS ALB sets for redirect loop) or similar
Expand Down Expand Up @@ -1640,6 +1643,20 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx.initialSpan = span
ctx.parentSpan = span

traceID := tracing.GetTraceID(span)

gCtx := stdlibcontext.Background()

defer pprof.SetGoroutineLabels(gCtx)
labels := pprof.Labels("trace_id", traceID)
gCtx = pprof.WithLabels(gCtx, labels)
pprof.SetGoroutineLabels(gCtx)

ctx.goCtx = &gCtx

traceIDCtx, _ := pprof.Label(gCtx, "trace_id")
ctx.Logger().Debugf("trace id in goroutine context: %s", traceIDCtx)
Copy link
Member

@AlexanderYastrebov AlexanderYastrebov Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to store ctx.goCtx, i.e. just do:

labels := pprof.Labels("trace_id", tracing.GetTraceID(span))
pprof.SetGoroutineLabels(pprof.WithLabels(stdlibcontext.Background(), labels))
defer pprof.SetGoroutineLabels(stdlibcontext.Background())

From https://pkg.go.dev/runtime/pprof#SetGoroutineLabels we read

SetGoroutineLabels sets the current goroutine's labels to match ctx. A new goroutine inherits the labels of the goroutine that created it.

So to test this we can create a testproxy with a mock tracer and a test filter that spawns a goroutine and verifies it can obtain trace_id via https://pkg.go.dev/runtime/pprof#Label. See TestFilterPanic for how to create and register a test filter.


defer func() {
if ctx.response != nil && ctx.response.Body != nil {
err := ctx.response.Body.Close()
Expand Down
Loading