Skip to content

Commit b819116

Browse files
committed
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 5fe764b commit b819116

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-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
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 map[string]string{}
18+
}
19+
return *l
20+
}

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)