From 52b319bc00712da095ee4121b616be232b1e455b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 20 Dec 2024 12:17:14 +0800 Subject: 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 flexible --- modules/process/manager.go | 21 ++++++++------------- modules/process/manager_stacktraces.go | 14 ++++++++------ 2 files changed, 16 insertions(+), 19 deletions(-) (limited to 'modules/process') diff --git a/modules/process/manager.go b/modules/process/manager.go index 7b8ada786e..661511ce8d 100644 --- a/modules/process/manager.go +++ b/modules/process/manager.go @@ -11,6 +11,8 @@ import ( "sync" "sync/atomic" "time" + + "code.gitea.io/gitea/modules/gtprof" ) // TODO: This packages still uses a singleton for the Manager. @@ -25,18 +27,6 @@ var ( DefaultContext = context.Background() ) -// DescriptionPProfLabel is a label set on goroutines that have a process attached -const DescriptionPProfLabel = "process_description" - -// PIDPProfLabel is a label set on goroutines that have a process attached -const PIDPProfLabel = "pid" - -// PPIDPProfLabel is a label set on goroutines that have a process attached -const PPIDPProfLabel = "ppid" - -// ProcessTypePProfLabel is a label set on goroutines that have a process attached -const ProcessTypePProfLabel = "process_type" - // IDType is a pid type type IDType string @@ -187,7 +177,12 @@ func (pm *Manager) Add(ctx context.Context, description string, cancel context.C Trace(true, pid, description, parentPID, processType) - pprofCtx := pprof.WithLabels(ctx, pprof.Labels(DescriptionPProfLabel, description, PPIDPProfLabel, string(parentPID), PIDPProfLabel, string(pid), ProcessTypePProfLabel, processType)) + pprofCtx := pprof.WithLabels(ctx, pprof.Labels( + gtprof.LabelProcessDescription, description, + gtprof.LabelPpid, string(parentPID), + gtprof.LabelPid, string(pid), + gtprof.LabelProcessType, processType, + )) if currentlyRunning { pprof.SetGoroutineLabels(pprofCtx) } diff --git a/modules/process/manager_stacktraces.go b/modules/process/manager_stacktraces.go index e260893113..d83060f6ee 100644 --- a/modules/process/manager_stacktraces.go +++ b/modules/process/manager_stacktraces.go @@ -10,6 +10,8 @@ import ( "sort" "time" + "code.gitea.io/gitea/modules/gtprof" + "github.com/google/pprof/profile" ) @@ -202,7 +204,7 @@ func (pm *Manager) ProcessStacktraces(flat, noSystem bool) ([]*Process, int, int // Add the non-process associated labels from the goroutine sample to the Stack for name, value := range sample.Label { - if name == DescriptionPProfLabel || name == PIDPProfLabel || (!flat && name == PPIDPProfLabel) || name == ProcessTypePProfLabel { + if name == gtprof.LabelProcessDescription || name == gtprof.LabelPid || (!flat && name == gtprof.LabelPpid) || name == gtprof.LabelProcessType { continue } @@ -224,7 +226,7 @@ func (pm *Manager) ProcessStacktraces(flat, noSystem bool) ([]*Process, int, int var process *Process // Try to get the PID from the goroutine labels - if pidvalue, ok := sample.Label[PIDPProfLabel]; ok && len(pidvalue) == 1 { + if pidvalue, ok := sample.Label[gtprof.LabelPid]; ok && len(pidvalue) == 1 { pid := IDType(pidvalue[0]) // Now try to get the process from our map @@ -238,20 +240,20 @@ func (pm *Manager) ProcessStacktraces(flat, noSystem bool) ([]*Process, int, int // get the parent PID ppid := IDType("") - if value, ok := sample.Label[PPIDPProfLabel]; ok && len(value) == 1 { + if value, ok := sample.Label[gtprof.LabelPpid]; ok && len(value) == 1 { ppid = IDType(value[0]) } // format the description description := "(dead process)" - if value, ok := sample.Label[DescriptionPProfLabel]; ok && len(value) == 1 { + if value, ok := sample.Label[gtprof.LabelProcessDescription]; ok && len(value) == 1 { description = value[0] + " " + description } // override the type of the process to "code" but add the old type as a label on the first stack ptype := NoneProcessType - if value, ok := sample.Label[ProcessTypePProfLabel]; ok && len(value) == 1 { - stack.Labels = append(stack.Labels, &Label{Name: ProcessTypePProfLabel, Value: value[0]}) + if value, ok := sample.Label[gtprof.LabelProcessType]; ok && len(value) == 1 { + stack.Labels = append(stack.Labels, &Label{Name: gtprof.LabelProcessType, Value: value[0]}) } process = &Process{ PID: pid, -- cgit v1.2.3