mirror of https://github.com/go-gitea/gitea
Refactor pprof labels and process desc (#32909)
* Deprecate "gopid" in log, it is not useful and requires very hacky approach * Remove "git.Command.SetDescription" because it is not useful and only makes the logs too flexiblepull/32880/head
parent
c66de245c4
commit
52b319bc00
@ -0,0 +1,25 @@ |
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package gtprof |
||||
|
||||
// This is a Gitea-specific profiling package,
|
||||
// the name is chosen to distinguish it from the standard pprof tool and "GNU gprof"
|
||||
|
||||
// LabelGracefulLifecycle is a label marking manager lifecycle phase
|
||||
// Making it compliant with prometheus key regex https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
|
||||
// would enable someone interested to be able to continuously gather profiles into pyroscope.
|
||||
// Other labels for pprof should also follow this rule.
|
||||
const LabelGracefulLifecycle = "graceful_lifecycle" |
||||
|
||||
// LabelPid is a label set on goroutines that have a process attached
|
||||
const LabelPid = "pid" |
||||
|
||||
// LabelPpid is a label set on goroutines that have a process attached
|
||||
const LabelPpid = "ppid" |
||||
|
||||
// LabelProcessType is a label set on goroutines that have a process attached
|
||||
const LabelProcessType = "process_type" |
||||
|
||||
// LabelProcessDescription is a label set on goroutines that have a process attached
|
||||
const LabelProcessDescription = "process_description" |
@ -1,19 +0,0 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package log |
||||
|
||||
import "unsafe" |
||||
|
||||
//go:linkname runtime_getProfLabel runtime/pprof.runtime_getProfLabel
|
||||
func runtime_getProfLabel() unsafe.Pointer //nolint
|
||||
|
||||
type labelMap map[string]string |
||||
|
||||
func getGoroutineLabels() map[string]string { |
||||
l := (*labelMap)(runtime_getProfLabel()) |
||||
if l == nil { |
||||
return nil |
||||
} |
||||
return *l |
||||
} |
@ -1,33 +0,0 @@ |
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package log |
||||
|
||||
import ( |
||||
"context" |
||||
"runtime/pprof" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func Test_getGoroutineLabels(t *testing.T) { |
||||
pprof.Do(context.Background(), pprof.Labels(), func(ctx context.Context) { |
||||
currentLabels := getGoroutineLabels() |
||||
pprof.ForLabels(ctx, func(key, value string) bool { |
||||
assert.EqualValues(t, value, currentLabels[key]) |
||||
return true |
||||
}) |
||||
|
||||
pprof.Do(ctx, pprof.Labels("Test_getGoroutineLabels", "Test_getGoroutineLabels_child1"), func(ctx context.Context) { |
||||
currentLabels := getGoroutineLabels() |
||||
pprof.ForLabels(ctx, func(key, value string) bool { |
||||
assert.EqualValues(t, value, currentLabels[key]) |
||||
return true |
||||
}) |
||||
if assert.NotNil(t, currentLabels) { |
||||
assert.EqualValues(t, "Test_getGoroutineLabels_child1", currentLabels["Test_getGoroutineLabels"]) |
||||
} |
||||
}) |
||||
}) |
||||
} |
@ -0,0 +1,13 @@ |
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package util |
||||
|
||||
import "runtime" |
||||
|
||||
func CallerFuncName(skip int) string { |
||||
pc := make([]uintptr, 1) |
||||
runtime.Callers(skip+1, pc) |
||||
funcName := runtime.FuncForPC(pc[0]).Name() |
||||
return funcName |
||||
} |
@ -0,0 +1,32 @@ |
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package util |
||||
|
||||
import ( |
||||
"fmt" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestCallerFuncName(t *testing.T) { |
||||
s := CallerFuncName(1) |
||||
assert.Equal(t, "code.gitea.io/gitea/modules/util.TestCallerFuncName", s) |
||||
} |
||||
|
||||
func BenchmarkCallerFuncName(b *testing.B) { |
||||
// BenchmarkCaller/sprintf-12 12744829 95.49 ns/op
|
||||
b.Run("sprintf", func(b *testing.B) { |
||||
for i := 0; i < b.N; i++ { |
||||
_ = fmt.Sprintf("aaaaaaaaaaaaaaaa %s %s %s", "bbbbbbbbbbbbbbbbbbb", b.Name(), "ccccccccccccccccccccc") |
||||
} |
||||
}) |
||||
// BenchmarkCaller/caller-12 10625133 113.6 ns/op
|
||||
// It is almost as fast as fmt.Sprintf
|
||||
b.Run("caller", func(b *testing.B) { |
||||
for i := 0; i < b.N; i++ { |
||||
CallerFuncName(1) |
||||
} |
||||
}) |
||||
} |
Loading…
Reference in new issue