| 1 |
// Copyright 2018 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build linux |
| 15 |
// +build linux |
| 16 |
|
| 17 |
package prometheus |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"os" |
| 24 |
"path/filepath" |
| 25 |
"regexp" |
| 26 |
"strings" |
| 27 |
"testing" |
| 28 |
|
| 29 |
"github.com/prometheus/common/expfmt" |
| 30 |
"github.com/prometheus/procfs" |
| 31 |
|
| 32 |
dto "github.com/prometheus/client_model/go" |
| 33 |
) |
| 34 |
|
| 35 |
func TestProcessCollector(t *testing.T) { |
| 36 |
if _, err := procfs.Self(); err != nil { |
| 37 |
t.Skipf("skipping TestProcessCollector, procfs not available: %s", err) |
| 38 |
} |
| 39 |
|
| 40 |
registry := NewPedanticRegistry() |
| 41 |
if err := registry.Register(NewProcessCollector(ProcessCollectorOpts{})); err != nil { |
| 42 |
t.Fatal(err) |
| 43 |
} |
| 44 |
if err := registry.Register(NewProcessCollector(ProcessCollectorOpts{ |
| 45 |
PidFn: func() (int, error) { return os.Getpid(), nil }, |
| 46 |
Namespace: "foobar", |
| 47 |
ReportErrors: true, // No errors expected, just to see if none are reported. |
| 48 |
})); err != nil { |
| 49 |
t.Fatal(err) |
| 50 |
} |
| 51 |
|
| 52 |
mfs, err := registry.Gather() |
| 53 |
if err != nil { |
| 54 |
t.Fatal(err) |
| 55 |
} |
| 56 |
|
| 57 |
var buf bytes.Buffer |
| 58 |
for _, mf := range mfs { |
| 59 |
if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
for _, re := range []*regexp.Regexp{ |
| 65 |
regexp.MustCompile("\nprocess_cpu_seconds_total [0-9]"), |
| 66 |
regexp.MustCompile("\nprocess_max_fds [1-9]"), |
| 67 |
regexp.MustCompile("\nprocess_open_fds [1-9]"), |
| 68 |
regexp.MustCompile("\nprocess_virtual_memory_max_bytes (-1|[1-9])"), |
| 69 |
regexp.MustCompile("\nprocess_virtual_memory_bytes [1-9]"), |
| 70 |
regexp.MustCompile("\nprocess_resident_memory_bytes [1-9]"), |
| 71 |
regexp.MustCompile("\nprocess_start_time_seconds [0-9.]{10,}"), |
| 72 |
regexp.MustCompile("\nprocess_network_receive_bytes_total [0-9]+"), |
| 73 |
regexp.MustCompile("\nprocess_network_transmit_bytes_total [0-9]+"), |
| 74 |
regexp.MustCompile("\nfoobar_process_cpu_seconds_total [0-9]"), |
| 75 |
regexp.MustCompile("\nfoobar_process_max_fds [1-9]"), |
| 76 |
regexp.MustCompile("\nfoobar_process_open_fds [1-9]"), |
| 77 |
regexp.MustCompile("\nfoobar_process_virtual_memory_max_bytes (-1|[1-9])"), |
| 78 |
regexp.MustCompile("\nfoobar_process_virtual_memory_bytes [1-9]"), |
| 79 |
regexp.MustCompile("\nfoobar_process_resident_memory_bytes [1-9]"), |
| 80 |
regexp.MustCompile("\nfoobar_process_start_time_seconds [0-9.]{10,}"), |
| 81 |
regexp.MustCompile("\nfoobar_process_network_receive_bytes_total [0-9]+"), |
| 82 |
regexp.MustCompile("\nfoobar_process_network_transmit_bytes_total [0-9]+"), |
| 83 |
} { |
| 84 |
if !re.Match(buf.Bytes()) { |
| 85 |
t.Errorf("want body to match %s\n%s", re, buf.String()) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
brokenProcessCollector := NewProcessCollector(ProcessCollectorOpts{ |
| 90 |
PidFn: func() (int, error) { return 0, errors.New("boo") }, |
| 91 |
ReportErrors: true, |
| 92 |
}) |
| 93 |
|
| 94 |
ch := make(chan Metric) |
| 95 |
go func() { |
| 96 |
brokenProcessCollector.Collect(ch) |
| 97 |
close(ch) |
| 98 |
}() |
| 99 |
n := 0 |
| 100 |
for m := range ch { |
| 101 |
n++ |
| 102 |
pb := &dto.Metric{} |
| 103 |
err := m.Write(pb) |
| 104 |
if err == nil { |
| 105 |
t.Error("metric collected from broken process collector is unexpectedly valid") |
| 106 |
} |
| 107 |
} |
| 108 |
if n != 1 { |
| 109 |
t.Errorf("%d metrics collected, want 1", n) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func TestNewPidFileFn(t *testing.T) { |
| 114 |
folderPath, err := os.Getwd() |
| 115 |
if err != nil { |
| 116 |
t.Error("failed to get current path") |
| 117 |
} |
| 118 |
mockPidFilePath := filepath.Join(folderPath, "mockPidFile") |
| 119 |
defer os.Remove(mockPidFilePath) |
| 120 |
|
| 121 |
testCases := []struct { |
| 122 |
mockPidFile func() |
| 123 |
expectedErrPrefix string |
| 124 |
expectedPid int |
| 125 |
desc string |
| 126 |
}{ |
| 127 |
{ |
| 128 |
mockPidFile: func() { |
| 129 |
os.Remove(mockPidFilePath) |
| 130 |
}, |
| 131 |
expectedErrPrefix: "can't read pid file", |
| 132 |
expectedPid: 0, |
| 133 |
desc: "no existed pid file", |
| 134 |
}, |
| 135 |
{ |
| 136 |
mockPidFile: func() { |
| 137 |
os.Remove(mockPidFilePath) |
| 138 |
f, _ := os.Create(mockPidFilePath) |
| 139 |
f.Write([]byte("abc")) |
| 140 |
f.Close() |
| 141 |
}, |
| 142 |
expectedErrPrefix: "can't parse pid file", |
| 143 |
expectedPid: 0, |
| 144 |
desc: "existed pid file, error pid number", |
| 145 |
}, |
| 146 |
{ |
| 147 |
mockPidFile: func() { |
| 148 |
os.Remove(mockPidFilePath) |
| 149 |
f, _ := os.Create(mockPidFilePath) |
| 150 |
f.Write([]byte("123")) |
| 151 |
f.Close() |
| 152 |
}, |
| 153 |
expectedErrPrefix: "", |
| 154 |
expectedPid: 123, |
| 155 |
desc: "existed pid file, correct pid number", |
| 156 |
}, |
| 157 |
} |
| 158 |
|
| 159 |
for _, tc := range testCases { |
| 160 |
fn := NewPidFileFn(mockPidFilePath) |
| 161 |
if fn == nil { |
| 162 |
t.Error("Should not get nil PidFileFn") |
| 163 |
} |
| 164 |
|
| 165 |
tc.mockPidFile() |
| 166 |
|
| 167 |
if pid, err := fn(); pid != tc.expectedPid || (err != nil && !strings.HasPrefix(err.Error(), tc.expectedErrPrefix)) { |
| 168 |
fmt.Println(err.Error()) |
| 169 |
t.Error(tc.desc) |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func TestDescribeAndCollectAlignment(t *testing.T) { |
| 175 |
collector := &processCollector{ |
| 176 |
pidFn: getPIDFn(), |
| 177 |
cpuTotal: NewDesc("cpu_total", "Total CPU usage", nil, nil), |
| 178 |
openFDs: NewDesc("open_fds", "Number of open file descriptors", nil, nil), |
| 179 |
maxFDs: NewDesc("max_fds", "Maximum file descriptors", nil, nil), |
| 180 |
vsize: NewDesc("vsize", "Virtual memory size", nil, nil), |
| 181 |
maxVsize: NewDesc("max_vsize", "Maximum virtual memory size", nil, nil), |
| 182 |
rss: NewDesc("rss", "Resident Set Size", nil, nil), |
| 183 |
startTime: NewDesc("start_time", "Process start time", nil, nil), |
| 184 |
inBytes: NewDesc("in_bytes", "Input bytes", nil, nil), |
| 185 |
outBytes: NewDesc("out_bytes", "Output bytes", nil, nil), |
| 186 |
} |
| 187 |
|
| 188 |
// Collect and get descriptors |
| 189 |
descCh := make(chan *Desc, 15) |
| 190 |
collector.describe(descCh) |
| 191 |
close(descCh) |
| 192 |
|
| 193 |
definedDescs := make(map[string]bool) |
| 194 |
for desc := range descCh { |
| 195 |
definedDescs[desc.String()] = true |
| 196 |
} |
| 197 |
|
| 198 |
// Collect and get metrics |
| 199 |
metricsCh := make(chan Metric, 15) |
| 200 |
collector.processCollect(metricsCh) |
| 201 |
close(metricsCh) |
| 202 |
|
| 203 |
collectedMetrics := make(map[string]bool) |
| 204 |
for metric := range metricsCh { |
| 205 |
collectedMetrics[metric.Desc().String()] = true |
| 206 |
} |
| 207 |
|
| 208 |
// Verify that all described metrics are collected |
| 209 |
for desc := range definedDescs { |
| 210 |
if !collectedMetrics[desc] { |
| 211 |
t.Errorf("Metric %s described but not collected", desc) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// Verify that no extra metrics are collected |
| 216 |
for desc := range collectedMetrics { |
| 217 |
if !definedDescs[desc] { |
| 218 |
t.Errorf("Metric %s collected but not described", desc) |
| 219 |
} |
| 220 |
} |
| 221 |
} |