1
1
package metrics
2
2
3
3
import (
4
- "flag"
5
4
"fmt"
6
5
"path/filepath"
7
6
@@ -14,15 +13,15 @@ type ProcessMetricsSet struct {
14
13
processMetrics map [int ]* PrometheusProcessMetrics
15
14
namespace string
16
15
procBinaryName string
17
- procArgName string
16
+ nameFlag string
18
17
}
19
18
20
- func NewProcessMetricsSet (namespace , procBinaryName , procArgName string ) * ProcessMetricsSet {
19
+ func NewProcessMetricsSet (namespace , procBinaryName , nameFlag string ) * ProcessMetricsSet {
21
20
return & ProcessMetricsSet {
22
21
processMetrics : make (map [int ]* PrometheusProcessMetrics ),
23
22
namespace : namespace ,
24
23
procBinaryName : procBinaryName ,
25
- procArgName : procArgName ,
24
+ nameFlag : nameFlag ,
26
25
}
27
26
}
28
27
@@ -31,7 +30,7 @@ func (set *ProcessMetricsSet) UpdateMonitoredSet() {
31
30
panic ("called update on disposed ProcessMetricsSet" )
32
31
}
33
32
processIds := findProcesses (set .procBinaryName )
34
- errs := AdjustMetricsMap (set .processMetrics , processIds , set .namespace )
33
+ errs := AdjustMetricsMap (set .processMetrics , processIds , set .namespace , set . nameFlag )
35
34
for _ , err := range errs {
36
35
log .Warn (fmt .Sprintf ("Could not report metrics for process: %v." , err ))
37
36
}
@@ -52,7 +51,6 @@ func findProcesses(processName string) (pids map[int]ps.Process) {
52
51
}
53
52
for _ , proc := range procs {
54
53
name := proc .Executable ()
55
- fmt .Println (name )
56
54
if processName == "" || filepath .Base (name ) == processName {
57
55
pid := proc .Pid ()
58
56
pids [pid ] = proc
@@ -61,10 +59,10 @@ func findProcesses(processName string) (pids map[int]ps.Process) {
61
59
return pids
62
60
}
63
61
64
- func AdjustMetricsMap (metricMap map [int ]* PrometheusProcessMetrics , pids map [int ]ps.Process , metricNamespace string ) (errs []error ) {
62
+ func AdjustMetricsMap (metricMap map [int ]* PrometheusProcessMetrics , pids map [int ]ps.Process , metricNamespace string , nameFlag string ) (errs []error ) {
65
63
removePids , newPids := FindPidDifferences (metricMap , pids )
66
64
for _ , pid := range newPids {
67
- name , err := procDescriptiveName (pid )
65
+ name , err := procDescriptiveName (pid , nameFlag )
68
66
if err != nil {
69
67
errs = append (errs , fmt .Errorf ("failed to get descriptive process name for PID %d: %w" , pid , err ))
70
68
continue
@@ -82,7 +80,7 @@ func AdjustMetricsMap(metricMap map[int]*PrometheusProcessMetrics, pids map[int]
82
80
m := metricMap [pid ]
83
81
m .Unregister ()
84
82
delete (metricMap , pid )
85
- log .Infof ("Stopped monitoring of process with PID %d." , pids [ pid ]. Pid () )
83
+ log .Infof ("Stopped monitoring of process with PID %d." , pid )
86
84
}
87
85
return errs
88
86
}
@@ -116,7 +114,7 @@ func updateMetrics(pm *PrometheusProcessMetrics, withPid int) {
116
114
pm .Set (processMetrics )
117
115
}
118
116
119
- func procDescriptiveName (pid int ) (string , error ) {
117
+ func procDescriptiveName (pid int , nameFlag string ) (string , error ) {
120
118
proc , err := process .NewProcess (int32 (pid ))
121
119
if err != nil {
122
120
return "" , err
@@ -125,23 +123,25 @@ func procDescriptiveName(pid int) (string, error) {
125
123
if err != nil {
126
124
return "" , err
127
125
}
128
- return descriptiveNameFromArgs (args )
126
+ return descriptiveNameFromArgs (args , nameFlag )
129
127
}
130
128
131
- func descriptiveNameFromArgs (args []string ) (string , error ) {
129
+ func descriptiveNameFromArgs (args []string , flagName string ) (string , error ) {
132
130
if len (args ) <= 1 {
133
131
return "" , fmt .Errorf ("too few arguments" )
134
132
}
135
133
args = args [1 :]
136
- flagSet := flag .NewFlagSet ("" , flag .ContinueOnError )
137
- flagSet .Usage = func () {}
138
- name := flagSet .String ("name" , "" , "" )
139
- err := flagSet .Parse (args )
140
- if err != nil {
141
- return "" , err
134
+ nextIsName := false
135
+ for _ , arg := range args {
136
+ if nextIsName {
137
+ return arg , nil
138
+ }
139
+ if arg == "-" + flagName || arg == "--" + flagName {
140
+ nextIsName = true
141
+ }
142
142
}
143
- if * name == "" {
144
- return "" , fmt .Errorf ("name not found or empty" )
143
+ if nextIsName {
144
+ return "" , fmt .Errorf ("no value for flag \" %s \" in args %v" , flagName , args )
145
145
}
146
- return * name , nil
146
+ return "" , fmt . Errorf ( "no flag \" %s \" in args %v" , flagName , args )
147
147
}
0 commit comments