Home | History | Annotate | Download | only in pprof
      1 // Copyright 2017 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package pprof
      6 
      7 import (
      8 	"context"
      9 	"unsafe"
     10 )
     11 
     12 // runtime_setProfLabel is defined in runtime/proflabel.go.
     13 func runtime_setProfLabel(labels unsafe.Pointer)
     14 
     15 // runtime_getProfLabel is defined in runtime/proflabel.go.
     16 func runtime_getProfLabel() unsafe.Pointer
     17 
     18 // SetGoroutineLabels sets the current goroutine's labels to match ctx.
     19 // This is a lower-level API than Do, which should be used instead when possible.
     20 func SetGoroutineLabels(ctx context.Context) {
     21 	ctxLabels, _ := ctx.Value(labelContextKey{}).(*labelMap)
     22 	runtime_setProfLabel(unsafe.Pointer(ctxLabels))
     23 }
     24 
     25 // Do calls f with a copy of the parent context with the
     26 // given labels added to the parent's label map.
     27 // Each key/value pair in labels is inserted into the label map in the
     28 // order provided, overriding any previous value for the same key.
     29 // The augmented label map will be set for the duration of the call to f
     30 // and restored once f returns.
     31 func Do(ctx context.Context, labels LabelSet, f func(context.Context)) {
     32 	defer SetGoroutineLabels(ctx)
     33 	ctx = WithLabels(ctx, labels)
     34 	SetGoroutineLabels(ctx)
     35 	f(ctx)
     36 }
     37