Trim exec path in middle (#22)

This commit is contained in:
treydock 2023-05-06 08:00:41 -04:00 committed by GitHub
parent 826c161de6
commit 15c70a8254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

@ -239,8 +239,10 @@ func getProcInfo(pids []int, metric *CgroupMetric, logger log.Logger) {
}
if len(executable) > *collectProcMaxExec {
level.Debug(logger).Log("msg", "Executable will be truncated", "executable", executable, "len", len(executable), "pid", p)
executable = executable[len(executable)-*collectProcMaxExec:]
executable = fmt.Sprintf("...%s", executable)
trim := *collectProcMaxExec / 2
executable_prefix := executable[0:trim]
executable_suffix := executable[len(executable)-trim:]
executable = fmt.Sprintf("%s...%s", executable_prefix, executable_suffix)
}
metricLock.Lock()
executables[executable] += 1
@ -479,6 +481,7 @@ func metricsHandler(logger log.Logger) http.HandlerFunc {
exporter := NewExporter(paths, logger)
registry.MustRegister(exporter)
registry.MustRegister(version.NewCollector(fmt.Sprintf("%s_exporter", namespace)))
gatherers := prometheus.Gatherers{registry}
if !*disableExporterMetrics {

View File

@ -96,15 +96,15 @@ func TestGetProcInfo(t *testing.T) {
t.Errorf("Expected 2 /bin/bash processes, got %v", val)
}
}
varLen := 4
varLen := 6
collectProcMaxExec = &varLen
getProcInfo([]int{95521, 95525}, &metric, logger)
if val, ok := metric.processExec["...bash"]; !ok {
if val, ok := metric.processExec["/bi...ash"]; !ok {
t.Errorf("Process /bin/bash not in metrics, found: %v", metric.processExec)
return
} else {
if val != 2 {
t.Errorf("Expected 2 .../bash processes, got %v", val)
t.Errorf("Expected 2 /b...sh processes, got %v", val)
}
}
}