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 // A new goroutine inherits the labels of the goroutine that created it. 20 // This is a lower-level API than Do, which should be used instead when possible. 21 func SetGoroutineLabels(ctx context.Context) { 22 ctxLabels, _ := ctx.Value(labelContextKey{}).(*labelMap) 23 runtime_setProfLabel(unsafe.Pointer(ctxLabels)) 24 } 25 26 // Do calls f with a copy of the parent context with the 27 // given labels added to the parent's label map. 28 // Goroutines spawned while executing f will inherit the augmented label-set. 29 // Each key/value pair in labels is inserted into the label map in the 30 // order provided, overriding any previous value for the same key. 31 // The augmented label map will be set for the duration of the call to f 32 // and restored once f returns. 33 func Do(ctx context.Context, labels LabelSet, f func(context.Context)) { 34 defer SetGoroutineLabels(ctx) 35 ctx = WithLabels(ctx, labels) 36 SetGoroutineLabels(ctx) 37 f(ctx) 38 } 39