Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the NewPidFileFn to helper #804

Merged
merged 1 commit into from Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions prometheus/process_collector.go
Expand Up @@ -15,7 +15,11 @@ package prometheus

import (
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)

type processCollector struct {
Expand Down Expand Up @@ -149,3 +153,20 @@ func (c *processCollector) reportError(ch chan<- Metric, desc *Desc, err error)
}
ch <- NewInvalidMetric(desc, err)
}

// NewPidFileFn returns a function that retrieves a pid from the specified file.
// It is meant to be used for the PidFn field in ProcessCollectorOpts.
func NewPidFileFn(pidFilePath string) func() (int, error) {
return func() (int, error) {
content, err := ioutil.ReadFile(pidFilePath)
if err != nil {
return 0, fmt.Errorf("can't read pid file %q: %+v", pidFilePath, err)
}
pid, err := strconv.Atoi(strings.TrimSpace(string(content)))
if err != nil {
return 0, fmt.Errorf("can't parse pid file %q: %+v", pidFilePath, err)
}

return pid, nil
}
}
64 changes: 64 additions & 0 deletions prometheus/process_collector_test.go
Expand Up @@ -18,8 +18,11 @@ package prometheus
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"

"github.com/prometheus/common/expfmt"
Expand Down Expand Up @@ -101,3 +104,64 @@ func TestProcessCollector(t *testing.T) {
t.Errorf("%d metrics collected, want 1", n)
}
}

func TestNewPidFileFn(t *testing.T) {
folderPath, err := os.Getwd()
if err != nil {
t.Error("failed to get current path")
}
mockPidFilePath := filepath.Join(folderPath, "mockPidFile")
defer os.Remove(mockPidFilePath)

testCases := []struct {
mockPidFile func()
expectedErrPrefix string
expectedPid int
desc string
}{
{
mockPidFile: func() {
os.Remove(mockPidFilePath)
},
expectedErrPrefix: "can't read pid file",
expectedPid: 0,
desc: "no existed pid file",
},
{
mockPidFile: func() {
os.Remove(mockPidFilePath)
f, _ := os.Create(mockPidFilePath)
f.Write([]byte("abc"))
f.Close()
},
expectedErrPrefix: "can't parse pid file",
expectedPid: 0,
desc: "existed pid file, error pid number",
},
{
mockPidFile: func() {
os.Remove(mockPidFilePath)
f, _ := os.Create(mockPidFilePath)
f.Write([]byte("123"))
f.Close()
},
expectedErrPrefix: "",
expectedPid: 123,
desc: "existed pid file, correct pid number",
},
}

for _, tc := range testCases {
fn := NewPidFileFn(mockPidFilePath)
if fn == nil {
t.Error("Should not get nil PidFileFn")
}

tc.mockPidFile()

if pid, err := fn(); pid != tc.expectedPid || (err != nil && !strings.HasPrefix(err.Error(), tc.expectedErrPrefix)) {
fmt.Println(err.Error())
t.Error(tc.desc)
}
}
}