Skip to content

Commit 35903f0

Browse files
committed
Fix test reporting cached result when it should not
1 parent 0416a4d commit 35903f0

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
/cmd/exporter/exporter
2-
proc_exporter_integration_dummy
1+
process_exporter
2+
prcexpintdum
33
dummyfile

test/integration/integration_test.go renamed to cmd/exporter/metrics/integration_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package integration
1+
package metrics_test
22

33
import (
44
"fmt"
@@ -15,13 +15,14 @@ import (
1515

1616
func TestIntegration(t *testing.T) {
1717
const port = "8771"
18+
const exporterBinaryName = "process_exporter"
1819
const dummyBinaryName = "prcexpintdum"
1920
const dummyDescripiveName = "iamdummy"
20-
run("go", "build", "-o", "exporter", "github.com/setlog/process_exporter/cmd/exporter")
21-
defer os.Remove("exporter")
21+
run("go", "build", "-o", exporterBinaryName, "github.com/setlog/process_exporter/cmd/exporter")
22+
defer os.Remove(exporterBinaryName)
2223
run("go", "build", "-o", dummyBinaryName, "github.com/setlog/process_exporter/test/dummy")
2324
defer os.Remove(dummyBinaryName)
24-
cmd := exec.Command("./exporter", "-port", port, "-binary", dummyBinaryName)
25+
cmd := exec.Command("./"+exporterBinaryName, "-port", port, "-binary", dummyBinaryName)
2526
err := cmd.Start()
2627
if err != nil {
2728
panic(err)

cmd/exporter/metrics/prom_proc_metrics.go

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ import (
99
)
1010

1111
type PrometheusProcessMetrics struct {
12-
lastMetricsSnapshot *ProcessMetrics
13-
cpuGauge prometheus.Gauge
14-
ramGauge prometheus.Gauge
15-
swapGauge prometheus.Gauge
16-
diskReadBytesCounter prometheus.Counter
17-
diskWriteBytesCounter prometheus.Counter
18-
diskReadCountCounter prometheus.Counter
19-
diskWriteCountCounter prometheus.Counter
20-
networkInBytesCounter prometheus.Counter
21-
networkOutBytesCounter prometheus.Counter
12+
cpuGauge prometheus.Gauge
13+
ramGauge prometheus.Gauge
14+
swapGauge prometheus.Gauge
15+
diskReadBytesGauge prometheus.Gauge
16+
diskWriteBytesGauge prometheus.Gauge
17+
diskReadCountGauge prometheus.Gauge
18+
diskWriteCountGauge prometheus.Gauge
19+
networkInBytesGauge prometheus.Gauge
20+
networkOutBytesGauge prometheus.Gauge
2221
}
2322

2423
func newProcessMetrics(proc ps.Process, descriptiveName, metricNamespace string) (processMetrics *PrometheusProcessMetrics) {
2524
processMetrics = &PrometheusProcessMetrics{}
26-
processMetrics.lastMetricsSnapshot = &ProcessMetrics{}
2725
binaryName := filepath.Base(proc.Executable())
2826
pid := fmt.Sprintf("%d", proc.Pid())
2927
processMetrics.makeGauges(metricNamespace, pid, binaryName, descriptiveName)
30-
processMetrics.makeDiskCounters(metricNamespace, pid, binaryName, descriptiveName)
31-
processMetrics.makeNetworkCounters(metricNamespace, pid, binaryName, descriptiveName)
28+
processMetrics.makeDiskGauges(metricNamespace, pid, binaryName, descriptiveName)
29+
processMetrics.makeNetworkGauges(metricNamespace, pid, binaryName, descriptiveName)
3230
return processMetrics
3331
}
3432

@@ -53,41 +51,41 @@ func (pm *PrometheusProcessMetrics) makeGauges(metricNamespace, pid, binaryName,
5351
})
5452
}
5553

56-
func (pm *PrometheusProcessMetrics) makeDiskCounters(metricNamespace, pid, binaryName, descriptiveName string) {
57-
pm.diskReadBytesCounter = prometheus.NewCounter(prometheus.CounterOpts{
54+
func (pm *PrometheusProcessMetrics) makeDiskGauges(metricNamespace, pid, binaryName, descriptiveName string) {
55+
pm.diskReadBytesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
5856
Namespace: metricNamespace,
5957
Name: "disk_read_bytes",
6058
Help: "Total read from disk (bytes)",
6159
ConstLabels: prometheus.Labels{"pid": pid, "bin": binaryName, "name": descriptiveName},
6260
})
63-
pm.diskWriteBytesCounter = prometheus.NewCounter(prometheus.CounterOpts{
61+
pm.diskWriteBytesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
6462
Namespace: metricNamespace,
6563
Name: "disk_write_bytes",
6664
Help: "Total written to disk (bytes)",
6765
ConstLabels: prometheus.Labels{"pid": pid, "bin": binaryName, "name": descriptiveName},
6866
})
69-
pm.diskReadCountCounter = prometheus.NewCounter(prometheus.CounterOpts{
67+
pm.diskReadCountGauge = prometheus.NewGauge(prometheus.GaugeOpts{
7068
Namespace: metricNamespace,
7169
Name: "disk_reads",
7270
Help: "Total reads from disk",
7371
ConstLabels: prometheus.Labels{"pid": pid, "bin": binaryName, "name": descriptiveName},
7472
})
75-
pm.diskWriteCountCounter = prometheus.NewCounter(prometheus.CounterOpts{
73+
pm.diskWriteCountGauge = prometheus.NewGauge(prometheus.GaugeOpts{
7674
Namespace: metricNamespace,
7775
Name: "disk_writes",
7876
Help: "Total writes to disk",
7977
ConstLabels: prometheus.Labels{"pid": pid, "bin": binaryName, "name": descriptiveName},
8078
})
8179
}
8280

83-
func (pm *PrometheusProcessMetrics) makeNetworkCounters(metricNamespace, pid, binaryName, descriptiveName string) {
84-
pm.networkInBytesCounter = prometheus.NewCounter(prometheus.CounterOpts{
81+
func (pm *PrometheusProcessMetrics) makeNetworkGauges(metricNamespace, pid, binaryName, descriptiveName string) {
82+
pm.networkInBytesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
8583
Namespace: metricNamespace,
8684
Name: "net_read_bytes",
8785
Help: "Total read from disk (bytes)",
8886
ConstLabels: prometheus.Labels{"pid": pid, "bin": binaryName, "name": descriptiveName},
8987
})
90-
pm.networkOutBytesCounter = prometheus.NewCounter(prometheus.CounterOpts{
88+
pm.networkOutBytesGauge = prometheus.NewGauge(prometheus.GaugeOpts{
9189
Namespace: metricNamespace,
9290
Name: "net_write_bytes",
9391
Help: "Total written to disk (bytes)",
@@ -102,12 +100,12 @@ func (pm *PrometheusProcessMetrics) Register() error {
102100
pm.cpuGauge,
103101
pm.ramGauge,
104102
pm.swapGauge,
105-
pm.diskReadBytesCounter,
106-
pm.diskWriteBytesCounter,
107-
pm.diskReadCountCounter,
108-
pm.diskWriteCountCounter,
109-
pm.networkInBytesCounter,
110-
pm.networkOutBytesCounter,
103+
pm.diskReadBytesGauge,
104+
pm.diskWriteBytesGauge,
105+
pm.diskReadCountGauge,
106+
pm.diskWriteCountGauge,
107+
pm.networkInBytesGauge,
108+
pm.networkOutBytesGauge,
111109
} {
112110
err = prometheus.Register(collector)
113111
if err != nil {
@@ -135,11 +133,10 @@ func (pm *PrometheusProcessMetrics) Set(processMetrics *ProcessMetrics) {
135133
pm.cpuGauge.Set(processMetrics.cpu)
136134
pm.ramGauge.Set(float64(processMetrics.ram))
137135
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
136+
pm.diskReadBytesGauge.Set(float64(processMetrics.diskReadBytes))
137+
pm.diskWriteBytesGauge.Set(float64(processMetrics.diskWriteBytes))
138+
pm.diskReadCountGauge.Set(float64(processMetrics.diskReadCount))
139+
pm.diskWriteCountGauge.Set(float64(processMetrics.diskWriteCount))
140+
pm.networkInBytesGauge.Set(float64(processMetrics.networkInBytes))
141+
pm.networkOutBytesGauge.Set(float64(processMetrics.networkOutBytes))
145142
}

0 commit comments

Comments
 (0)