Skip to content

Commit 70628bd

Browse files
authored
Add auto logging of goroutine pid label (#19212)
* Add auto logging of goroutine pid label This PR uses unsafe to export the hidden runtime_getProfLabel function from the runtime package and then casts the result to a map[string]string. We can then interrogate this map to get the pid label from the goroutine allowing us to log it with any logging request. Reference #19202 Signed-off-by: Andrew Thornton <[email protected]>
1 parent c119828 commit 70628bd

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

modules/log/groutinelabel.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package log
6+
7+
import "unsafe"
8+
9+
//go:linkname runtime_getProfLabel runtime/pprof.runtime_getProfLabel
10+
func runtime_getProfLabel() unsafe.Pointer // nolint
11+
12+
type labelMap map[string]string
13+
14+
func getGoroutineLabels() map[string]string {
15+
l := (*labelMap)(runtime_getProfLabel())
16+
if l == nil {
17+
return nil
18+
}
19+
return *l
20+
}

modules/log/groutinelabel_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package log
6+
7+
import (
8+
"context"
9+
"runtime/pprof"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func Test_getGoroutineLabels(t *testing.T) {
16+
pprof.Do(context.Background(), pprof.Labels(), func(ctx context.Context) {
17+
currentLabels := getGoroutineLabels()
18+
pprof.ForLabels(ctx, func(key, value string) bool {
19+
assert.EqualValues(t, value, currentLabels[key])
20+
return true
21+
})
22+
23+
pprof.Do(ctx, pprof.Labels("Test_getGoroutineLabels", "Test_getGoroutineLabels_child1"), func(ctx context.Context) {
24+
currentLabels := getGoroutineLabels()
25+
pprof.ForLabels(ctx, func(key, value string) bool {
26+
assert.EqualValues(t, value, currentLabels[key])
27+
return true
28+
})
29+
if assert.NotNil(t, currentLabels) {
30+
assert.EqualValues(t, "Test_getGoroutineLabels_child1", currentLabels["Test_getGoroutineLabels"])
31+
}
32+
})
33+
})
34+
}

modules/log/multichannel.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ func (l *MultiChannelledLogger) Log(skip int, level Level, format string, v ...i
7272
if len(v) > 0 {
7373
msg = ColorSprintf(format, v...)
7474
}
75+
labels := getGoroutineLabels()
76+
if labels != nil {
77+
pid, ok := labels["pid"]
78+
if ok {
79+
msg = "[" + ColorString(FgHiYellow) + pid + ColorString(Reset) + "] " + msg
80+
}
81+
}
7582
stack := ""
7683
if l.GetStacktraceLevel() <= level {
7784
stack = Stack(skip + 1)

0 commit comments

Comments
 (0)