From 33f46be98b6364ebc0b8036d3ef830aa9ede80a8 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 23 Feb 2023 21:18:19 -0600 Subject: [PATCH] trying in vain to figure out which goroutine I'm in Signed-off-by: Jeff Carr --- example-pprof-simple/Makefile | 11 ++++------ example-pprof-simple/foo.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 example-pprof-simple/foo.go diff --git a/example-pprof-simple/Makefile b/example-pprof-simple/Makefile index fa67bcf..b0c8dbc 100644 --- a/example-pprof-simple/Makefile +++ b/example-pprof-simple/Makefile @@ -1,9 +1,6 @@ run: GO111MODULE="off" go run -v -x main.go - -prep: - GO111MODULE="off" go get -v . - -build: - GO111MODULE="off" go build -v -x - # go build + @echo + @echo "Run foo.go:" + @echo + GO111MODULE="off" go run foo.go diff --git a/example-pprof-simple/foo.go b/example-pprof-simple/foo.go new file mode 100644 index 0000000..4fd371e --- /dev/null +++ b/example-pprof-simple/foo.go @@ -0,0 +1,38 @@ +package main + +import ( + "runtime/pprof" + "context" + "fmt" + "time" +) + +func main() { + type favContextKey string + + f := func(ctx context.Context, k string) { + ctx2 := context.WithValue(context.Background(), "language", "Go2") + if v := ctx2.Value(k); v != nil { + fmt.Println("found value:", v) + } else { + fmt.Println("key not found:", k) + } + pprof.SetGoroutineLabels(ctx) + + if v := ctx.Value(k); v != nil { + fmt.Println("found value:", v) + } else { + fmt.Println("key not found:", k) + } + } + + ctx := context.WithValue(context.Background(), "language", "Go") + + f(ctx, "language") + f(ctx, "color") + + go f(ctx, "language") + go f(ctx, "color") + + time.Sleep(time.Duration(1) * time.Second) +}