Skip to content

Commit 0416a4d

Browse files
committed
Make test succeed
1 parent 81ef07a commit 0416a4d

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

cmd/exporter/flags/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "flag"
55
func Parse(arguments []string) (namespace, binary, argName string, port int) {
66
flagSet := flag.NewFlagSet("flags", flag.ExitOnError)
77
namespacePtr := flagSet.String("namespace", "mine", "Prometheus metric namespace.")
8-
binaryPtr := flagSet.String("binary", "", "Filter which processes to watch by binary name.")
8+
binaryPtr := flagSet.String("binary", "", "Filter which processes to watch by binary name. This is limited to 15 bytes because of the kernel.")
99
nameFlagPtr := flagSet.String("nameflag", "name", "Set Prometheus \"name\"-label value to value of this command line argument of the monitored processes.")
1010
portPtr := flagSet.Int("port", 80, "Port on which to listen to requests to /metrics.")
1111
err := flagSet.Parse(arguments)

cmd/exporter/metrics/prom_proc_metrics.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
type PrometheusProcessMetrics struct {
12+
lastMetricsSnapshot *ProcessMetrics
1213
cpuGauge prometheus.Gauge
1314
ramGauge prometheus.Gauge
1415
swapGauge prometheus.Gauge
@@ -22,6 +23,7 @@ type PrometheusProcessMetrics struct {
2223

2324
func newProcessMetrics(proc ps.Process, descriptiveName, metricNamespace string) (processMetrics *PrometheusProcessMetrics) {
2425
processMetrics = &PrometheusProcessMetrics{}
26+
processMetrics.lastMetricsSnapshot = &ProcessMetrics{}
2527
binaryName := filepath.Base(proc.Executable())
2628
pid := fmt.Sprintf("%d", proc.Pid())
2729
processMetrics.makeGauges(metricNamespace, pid, binaryName, descriptiveName)
@@ -132,4 +134,12 @@ func (pm *PrometheusProcessMetrics) Update() {
132134
func (pm *PrometheusProcessMetrics) Set(processMetrics *ProcessMetrics) {
133135
pm.cpuGauge.Set(processMetrics.cpu)
134136
pm.ramGauge.Set(float64(processMetrics.ram))
137+
pm.swapGauge.Set(float64(processMetrics.swap))
138+
pm.diskReadBytesCounter.Add(float64(processMetrics.diskReadBytes - pm.lastMetricsSnapshot.diskReadBytes))
139+
pm.diskWriteBytesCounter.Add(float64(processMetrics.diskWriteBytes - pm.lastMetricsSnapshot.diskWriteBytes))
140+
pm.diskReadCountCounter.Add(float64(processMetrics.diskReadCount - pm.lastMetricsSnapshot.diskReadCount))
141+
pm.diskWriteCountCounter.Add(float64(processMetrics.diskWriteCount - pm.lastMetricsSnapshot.diskWriteCount))
142+
pm.networkInBytesCounter.Add(float64(processMetrics.networkInBytes - pm.lastMetricsSnapshot.networkInBytes))
143+
pm.networkOutBytesCounter.Add(float64(processMetrics.networkOutBytes - pm.lastMetricsSnapshot.networkOutBytes))
144+
pm.lastMetricsSnapshot = processMetrics
135145
}

test/dummy/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const dummyFilePath = "dummyfile"
1111
func main() {
1212
defer os.Remove(dummyFilePath)
1313
writeTestFile()
14-
fmt.Printf(".")
14+
fmt.Printf("\x2A")
1515
ioutil.ReadAll(os.Stdin)
1616
}
1717

@@ -21,5 +21,7 @@ func writeTestFile() {
2121
panic(err)
2222
}
2323
defer f.Close()
24-
f.WriteString("What a beautiful string") // 23 bytes
24+
for i := 0; i < 16; i++ { // Write 1024 bytes
25+
f.WriteString("What a beautiful string which is exactly 64 bytes in length!!!!!")
26+
}
2527
}

test/integration/integration_test.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,32 @@ package integration
33
import (
44
"fmt"
55
"io"
6+
"io/ioutil"
67
"net/http"
78
"os"
89
"os/exec"
10+
"strconv"
11+
"strings"
912
"testing"
13+
"time"
1014
)
1115

1216
func TestIntegration(t *testing.T) {
1317
const port = "8771"
18+
const dummyBinaryName = "prcexpintdum"
19+
const dummyDescripiveName = "iamdummy"
1420
run("go", "build", "-o", "exporter", "github.com/setlog/process_exporter/cmd/exporter")
1521
defer os.Remove("exporter")
16-
run("go", "build", "-o", "proc_exporter_integration_dummy", "github.com/setlog/process_exporter/test/dummy")
17-
defer os.Remove("proc_exporter_integration_dummy")
18-
cmd := exec.Command("exporter", "-port", port, "-binary", "proc_exporter_integration_dummy")
22+
run("go", "build", "-o", dummyBinaryName, "github.com/setlog/process_exporter/test/dummy")
23+
defer os.Remove(dummyBinaryName)
24+
cmd := exec.Command("./exporter", "-port", port, "-binary", dummyBinaryName)
1925
err := cmd.Start()
2026
if err != nil {
2127
panic(err)
2228
}
23-
cmdDummy := exec.Command("proc_exporter_integration_dummy", "-name", "iamdummy")
29+
defer cmd.Process.Signal(os.Interrupt)
30+
31+
cmdDummy := exec.Command("./"+dummyBinaryName, "-name", dummyDescripiveName)
2432
dummyReader, err := cmdDummy.StdoutPipe()
2533
if err != nil {
2634
panic(err)
@@ -29,26 +37,53 @@ func TestIntegration(t *testing.T) {
2937
if err != nil {
3038
panic(err)
3139
}
40+
defer dummyWriter.Close()
3241
err = cmdDummy.Start()
3342
if err != nil {
3443
panic(err)
3544
}
36-
b := make([]byte, 1, 1)
45+
b := []byte{0}
3746
_, err = io.ReadAtLeast(dummyReader, b, 1)
3847
if err != nil {
3948
panic(err)
4049
}
41-
resp, err := http.Get("http://localhost:" + port)
50+
if b[0] != 0x2A {
51+
panic(fmt.Sprintf("Byte was %x. Expected 0x2A", b[0]))
52+
}
53+
time.Sleep(time.Second)
54+
55+
resp, err := http.Get("http://localhost:" + port + "/metrics")
4256
if err != nil {
4357
panic(err)
4458
}
59+
defer resp.Body.Close()
60+
dummyWriter.Close()
4561
if resp.StatusCode != http.StatusOK {
4662
panic(fmt.Sprintf("got code %d when %d expected", resp.StatusCode, http.StatusOK))
4763
}
48-
err = dummyWriter.Close()
64+
contents, err := ioutil.ReadAll(resp.Body)
4965
if err != nil {
5066
panic(err)
5167
}
68+
contentsString := string(contents)
69+
lines := strings.Split(contentsString, "\n")
70+
gotLine := false
71+
for _, line := range lines {
72+
prefix := fmt.Sprintf("mine_disk_write_bytes{bin=\"%s\",name=\"%s\",pid=\"%d\"} ", dummyBinaryName, dummyDescripiveName, cmdDummy.Process.Pid)
73+
if strings.HasPrefix(line, prefix) {
74+
writeBytes, err := strconv.Atoi(line[len(prefix):])
75+
if err != nil {
76+
panic(fmt.Sprintf("failed to parse write_bytes from line %s: %v", line, err))
77+
}
78+
if writeBytes < 1024 || writeBytes > 8192 {
79+
panic(fmt.Sprintf("exporter reported %d bytes written. Expected something in the range [1024;8192].", writeBytes))
80+
}
81+
gotLine = true
82+
}
83+
}
84+
if !gotLine {
85+
panic(fmt.Sprintf("exporter did not report io byte write count"))
86+
}
5287
err = cmdDummy.Wait()
5388
if err != nil {
5489
panic(err)

0 commit comments

Comments
 (0)