Skip to content

Commit 469ec27

Browse files
committed
add the NewPidFileFn to helper
Signed-off-by: sbookworm <[email protected]>
1 parent 47cfdc9 commit 469ec27

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

prometheus/process_collector.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ package prometheus
1515

1616
import (
1717
"errors"
18+
"fmt"
19+
"io/ioutil"
1820
"os"
21+
"strconv"
22+
"strings"
1923
)
2024

2125
type processCollector struct {
@@ -149,3 +153,20 @@ func (c *processCollector) reportError(ch chan<- Metric, desc *Desc, err error)
149153
}
150154
ch <- NewInvalidMetric(desc, err)
151155
}
156+
157+
// NewPidFileFn returns a function that retrieves a pid from the specified file.
158+
// It is meant to be used for the PidFn field in ProcessCollectorOpts.
159+
func NewPidFileFn(pidFilePath string) func() (int, error) {
160+
return func() (int, error) {
161+
content, err := ioutil.ReadFile(pidFilePath)
162+
if err != nil {
163+
return 0, fmt.Errorf("can't read pid file %q: %+v", pidFilePath, err)
164+
}
165+
pid, err := strconv.Atoi(strings.TrimSpace(string(content)))
166+
if err != nil {
167+
return 0, fmt.Errorf("can't parse pid file %q: %+v", pidFilePath, err)
168+
}
169+
170+
return pid, nil
171+
}
172+
}

prometheus/process_collector_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ package prometheus
1818
import (
1919
"bytes"
2020
"errors"
21+
"fmt"
2122
"os"
23+
"path/filepath"
2224
"regexp"
25+
"strings"
2326
"testing"
2427

2528
"github.com/prometheus/common/expfmt"
@@ -101,3 +104,64 @@ func TestProcessCollector(t *testing.T) {
101104
t.Errorf("%d metrics collected, want 1", n)
102105
}
103106
}
107+
108+
func TestNewPidFileFn(t *testing.T) {
109+
folderPath, err := os.Getwd()
110+
if err != nil {
111+
t.Error("failed to get current path")
112+
}
113+
mockPidFilePath := filepath.Join(folderPath, "mockPidFile")
114+
defer os.Remove(mockPidFilePath)
115+
116+
testCases := []struct {
117+
mockPidFile func()
118+
expectedErrPrefix string
119+
expectedPid int
120+
desc string
121+
}{
122+
{
123+
mockPidFile: func() {
124+
os.Remove(mockPidFilePath)
125+
},
126+
expectedErrPrefix: "can't read pid file",
127+
expectedPid: 0,
128+
desc: "no existed pid file",
129+
},
130+
{
131+
mockPidFile: func() {
132+
os.Remove(mockPidFilePath)
133+
f, _ := os.Create(mockPidFilePath)
134+
f.Write([]byte("abc"))
135+
f.Close()
136+
},
137+
expectedErrPrefix: "can't parse pid file",
138+
expectedPid: 0,
139+
desc: "existed pid file, error pid number",
140+
},
141+
{
142+
mockPidFile: func() {
143+
os.Remove(mockPidFilePath)
144+
f, _ := os.Create(mockPidFilePath)
145+
f.Write([]byte("123"))
146+
f.Close()
147+
},
148+
expectedErrPrefix: "",
149+
expectedPid: 123,
150+
desc: "existed pid file, correct pid number",
151+
},
152+
}
153+
154+
for _, tc := range testCases {
155+
fn := NewPidFileFn(mockPidFilePath)
156+
if fn == nil {
157+
t.Error("Should not get nil PidFileFn")
158+
}
159+
160+
tc.mockPidFile()
161+
162+
if pid, err := fn(); pid != tc.expectedPid || (err != nil && !strings.HasPrefix(err.Error(), tc.expectedErrPrefix)) {
163+
fmt.Println(err.Error())
164+
t.Error(tc.desc)
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)