fix process, add name label
build / build (push) Successful in 53s

This commit is contained in:
tchivert committed 2026-04-28 19:16:43 +02:00
1 parent aeab295af1
commit 7f27a586c4
2 files changed
+36 -34

No files matched your search

+15 -15
View File
@@ -134,12 +134,12 @@ processes:
```
This will produce labels like:
- `process="php-fpm: pool www", pid="1234"`
- `process="php-fpm: pool staging", pid="1235"`
- `process="php-fpm", name="php-fpm: pool www", pid="1234"`
- `process="php-fpm", name="php-fpm: pool staging", pid="1235"`
Instead of:
- `process="php-fpm", pid="1234"`
- `process="php-fpm", pid="1235"`
Without `resolveName`, labels remain:
- `process="php-fpm", name="php-fpm", pid="1234"`
- `process="php-fpm", name="php-fpm", pid="1235"`
This is especially useful when process instances are short-lived and you need
to identify which pool they belong to from the metrics.
@@ -175,16 +175,16 @@ Usage of ./process-exporter:
The exporter provides the following metrics:
- `process_cpu_usage_percent`: CPU usage percentage (labels: process_name, pid)
- `process_memory_usage_bytes`: Memory usage in bytes (labels: process_name, pid)
- `process_memory_percent`: Memory usage percentage (labels: process_name, pid)
- `process_uptime_seconds`: Process uptime in seconds (labels: process_name, pid)
- `process_open_files`: Number of open file descriptors (labels: process_name, pid)
- `process_thread_count`: Number of threads (labels: process_name, pid)
- `process_resident_memory_bytes`: Resident memory size (labels: process_name, pid)
- `process_virtual_memory_bytes`: Virtual memory size (labels: process_name, pid)
- `process_read_bytes_total`: Total bytes read (labels: process_name, pid)
- `process_write_bytes_total`: Total bytes written (labels: process_name, pid)
- `process_cpu_usage_percent`: CPU usage percentage (labels: process, name, pid)
- `process_memory_usage_bytes`: Memory usage in bytes (labels: process, name, pid)
- `process_memory_percent`: Memory usage percentage (labels: process, name, pid)
- `process_uptime_seconds`: Process uptime in seconds (labels: process, name, pid)
- `process_open_files`: Number of open file descriptors (labels: process, name, pid)
- `process_thread_count`: Number of threads (labels: process, name, pid)
- `process_resident_memory_bytes`: Resident memory size (labels: process, name, pid)
- `process_virtual_memory_bytes`: Virtual memory size (labels: process, name, pid)
- `process_read_bytes_total`: Total bytes read (labels: process, name, pid)
- `process_write_bytes_total`: Total bytes written (labels: process, name, pid)
## Prometheus Configuration
+21 -19
View File
@@ -84,7 +84,7 @@ func createGaugeVec(name, help string) *prometheus.GaugeVec {
Name: name,
Help: help,
},
[]string{"process", "pid"},
[]string{"process", "name", "pid"},
)
}
@@ -94,7 +94,7 @@ func createCounterVec(name, help string) *prometheus.CounterVec {
Name: name,
Help: help,
},
[]string{"process", "pid"},
[]string{"process", "name", "pid"},
)
}
@@ -193,19 +193,21 @@ func (e *Exporter) collectAndCacheMetrics() {
if procConfig.Name != "" && procConfig.Name == baseName {
foundCounts[procConfig.Name]++
// Use resolved name if enabled
labelName := procConfig.Name
// Process label: always the base name
processLabel := procConfig.Name
// Name label: resolved full cmdline if enabled, otherwise same as process
nameLabel := procConfig.Name
if procConfig.ResolveName {
resolved := resolveProcessName(proc)
if resolved != "" {
labelName = resolved
nameLabel = resolved
}
}
e.collectProcessMetrics(proc, labelName)
e.collectProcessMetrics(proc, processLabel, nameLabel)
if e.verbose {
if procConfig.ResolveName {
log.Printf("Collected metrics for %s (PID: %d, resolved: %s)", procConfig.Name, proc.Pid, labelName)
log.Printf("Collected metrics for %s (PID: %d, name: %s)", procConfig.Name, proc.Pid, nameLabel)
} else {
log.Printf("Collected metrics for %s (PID: %d)", procConfig.Name, proc.Pid)
}
@@ -218,7 +220,7 @@ func (e *Exporter) collectAndCacheMetrics() {
// For PID-based monitoring, use the process name as the label
labelName := fmt.Sprintf("%s:%d", baseName, procConfig.Pid)
foundCounts[labelName]++
e.collectProcessMetrics(proc, labelName)
e.collectProcessMetrics(proc, labelName, labelName)
if e.verbose {
log.Printf("Collected metrics for %s (PID: %d)", baseName, proc.Pid)
}
@@ -286,11 +288,11 @@ func (pm *ProcessMetrics) resetAll() {
pm.virtualMemory.Reset()
}
func (e *Exporter) collectProcessMetrics(proc *process.Process, procName string) {
func (e *Exporter) collectProcessMetrics(proc *process.Process, processLabel, nameLabel string) {
pid := strconv.Itoa(int(proc.Pid))
if cpuPercent, err := proc.CPUPercent(); err == nil {
e.metrics.cpuUsage.WithLabelValues(procName, pid).Set(cpuPercent)
e.metrics.cpuUsage.WithLabelValues(processLabel, nameLabel, pid).Set(cpuPercent)
if e.verbose {
log.Printf(" CPU%%: %.2f%%", cpuPercent)
}
@@ -299,9 +301,9 @@ func (e *Exporter) collectProcessMetrics(proc *process.Process, procName string)
}
if memInfo, err := proc.MemoryInfo(); err == nil {
e.metrics.memoryUsage.WithLabelValues(procName, pid).Set(float64(memInfo.RSS))
e.metrics.residentMemory.WithLabelValues(procName, pid).Set(float64(memInfo.RSS))
e.metrics.virtualMemory.WithLabelValues(procName, pid).Set(float64(memInfo.VMS))
e.metrics.memoryUsage.WithLabelValues(processLabel, nameLabel, pid).Set(float64(memInfo.RSS))
e.metrics.residentMemory.WithLabelValues(processLabel, nameLabel, pid).Set(float64(memInfo.RSS))
e.metrics.virtualMemory.WithLabelValues(processLabel, nameLabel, pid).Set(float64(memInfo.VMS))
if e.verbose {
log.Printf(" Memory: RSS=%d bytes, VMS=%d bytes", memInfo.RSS, memInfo.VMS)
}
@@ -310,7 +312,7 @@ func (e *Exporter) collectProcessMetrics(proc *process.Process, procName string)
}
if memPercent, err := proc.MemoryPercent(); err == nil {
e.metrics.memoryPercent.WithLabelValues(procName, pid).Set(float64(memPercent))
e.metrics.memoryPercent.WithLabelValues(processLabel, nameLabel, pid).Set(float64(memPercent))
if e.verbose {
log.Printf(" Memory%%: %.2f%%", memPercent)
}
@@ -320,7 +322,7 @@ func (e *Exporter) collectProcessMetrics(proc *process.Process, procName string)
if createTime, err := proc.CreateTime(); err == nil {
uptimeSeconds := float64(time.Now().Unix() - int64(createTime/1000))
e.metrics.uptime.WithLabelValues(procName, pid).Set(uptimeSeconds)
e.metrics.uptime.WithLabelValues(processLabel, nameLabel, pid).Set(uptimeSeconds)
if e.verbose {
log.Printf(" Uptime: %.0f seconds", uptimeSeconds)
}
@@ -329,7 +331,7 @@ func (e *Exporter) collectProcessMetrics(proc *process.Process, procName string)
}
if openFiles, err := proc.NumFDs(); err == nil {
e.metrics.openFiles.WithLabelValues(procName, pid).Set(float64(openFiles))
e.metrics.openFiles.WithLabelValues(processLabel, nameLabel, pid).Set(float64(openFiles))
if e.verbose {
log.Printf(" Open files: %d", openFiles)
}
@@ -338,7 +340,7 @@ func (e *Exporter) collectProcessMetrics(proc *process.Process, procName string)
}
if threadCount, err := proc.NumThreads(); err == nil {
e.metrics.threadCount.WithLabelValues(procName, pid).Set(float64(threadCount))
e.metrics.threadCount.WithLabelValues(processLabel, nameLabel, pid).Set(float64(threadCount))
if e.verbose {
log.Printf(" Threads: %d", threadCount)
}
@@ -347,8 +349,8 @@ func (e *Exporter) collectProcessMetrics(proc *process.Process, procName string)
}
if ioCounters, err := proc.IOCounters(); err == nil {
e.metrics.readBytes.WithLabelValues(procName, pid).Add(float64(ioCounters.ReadBytes))
e.metrics.writeBytes.WithLabelValues(procName, pid).Add(float64(ioCounters.WriteBytes))
e.metrics.readBytes.WithLabelValues(processLabel, nameLabel, pid).Add(float64(ioCounters.ReadBytes))
e.metrics.writeBytes.WithLabelValues(processLabel, nameLabel, pid).Add(float64(ioCounters.WriteBytes))
if e.verbose {
log.Printf(" I/O: Read=%d bytes, Write=%d bytes", ioCounters.ReadBytes, ioCounters.WriteBytes)
}