-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathp4prom_test.go
373 lines (348 loc) · 14.2 KB
/
p4prom_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package main
import (
"context"
"fmt"
"os"
"regexp"
"runtime"
"sort"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/perforce/p4prometheus/config"
metrics "github.com/rcowham/go-libp4dlog/metrics"
"github.com/sirupsen/logrus"
)
var (
eol = regexp.MustCompile("\r\n|\n")
logger = &logrus.Logger{Out: os.Stderr,
Formatter: &logrus.TextFormatter{TimestampFormat: "15:04:05.000", FullTimestamp: true},
// Level: logrus.DebugLevel}
Level: logrus.InfoLevel}
)
func getResult(output chan string) []string {
lines := []string{}
for line := range output {
lines = append(lines, line)
}
return lines
}
func funcName() string {
fpcs := make([]uintptr, 1)
// Skip 2 levels to get the caller
n := runtime.Callers(2, fpcs)
if n == 0 {
return ""
}
caller := runtime.FuncForPC(fpcs[0] - 1)
if caller == nil {
return ""
}
return caller.Name()
}
// Assuming there are several outputs - this returns the latest one unless historical
func getOutput(testchan chan string, historical bool) []string {
result := make([]string, 0)
lastoutput := ""
if historical {
for output := range testchan {
for _, line := range eol.Split(output, -1) {
if len(line) > 0 && !strings.HasPrefix(line, "#") {
result = append(result, line)
}
}
}
} else {
for output := range testchan {
lastoutput = output
}
for _, line := range eol.Split(lastoutput, -1) {
if len(line) > 0 && !strings.HasPrefix(line, "#") {
result = append(result, line)
}
}
}
sort.Strings(result)
return result
}
func hasPrefix(prefixes []string, line string) bool {
for _, p := range prefixes {
if strings.HasPrefix(line, p) {
return true
}
}
return false
}
func compareOutput(t *testing.T, expected, actual []string) {
nExpected := make([]string, 0)
nActual := make([]string, 0)
// Ignore these elements as the contents varies per test run
ignorePrefixes := []string{"p4_prom_cmds_pending", "p4_prom_cpu_user", "p4_prom_cpu_system",
"p4_cmd_cpu_", "p4_prom_memory"}
for _, line := range expected {
if !hasPrefix(ignorePrefixes, line) {
nExpected = append(nExpected, line)
}
}
for _, line := range actual {
if !hasPrefix(ignorePrefixes, line) {
nActual = append(nActual, line)
}
}
sort.Strings(nActual)
sort.Strings(nExpected)
assert.Equal(t, nExpected, nActual)
}
func basicTest(t *testing.T, cfg *config.Config, input string, historical bool) []string {
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: "15:04:05.000", FullTimestamp: true})
logger.SetReportCaller(true)
logger.Debugf("Function: %s", funcName())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
linesChan := make(chan string, 100)
version := &metrics.P4DMetricsVersion{
Revision: "testrevision",
GoVersion: runtime.Version(),
Version: "test",
}
mconfig := &metrics.Config{
ServerID: "myserverid",
UpdateInterval: 10 * time.Millisecond,
OutputCmdsByUser: cfg.OutputCmdsByUser,
OutputCmdsByUserRegex: cfg.OutputCmdsByUserRegex,
OutputCmdsByIP: cfg.OutputCmdsByIP}
p4m := metrics.NewP4DMetricsLogParser(mconfig, version, logger, historical)
_, metricsChan := p4m.ProcessEvents(ctx, linesChan, false)
var wg sync.WaitGroup
for _, l := range eol.Split(input, -1) {
linesChan <- l
}
output := []string{}
go func() {
defer wg.Done()
time.Sleep(20 * time.Millisecond)
logger.Debugf("Waiting for metrics")
output = getOutput(metricsChan, historical)
}()
wg.Add(1)
time.Sleep(50 * time.Millisecond)
close(linesChan)
logger.Debugf("Waiting for finish")
wg.Wait()
logger.Debugf("Finished")
return output
}
func TestP4PromBasic(t *testing.T) {
cfg := &config.Config{
ServerID: "myserverid",
UpdateInterval: 10 * time.Millisecond,
OutputCmdsByUser: false,
OutputCmdsByUserRegex: "", // No match so don't output
OutputCmdsByIP: false,
}
input := `
Perforce server info:
2015/09/02 15:23:09 pid 1616 robert@robert-test 127.0.0.1 [p4/2016.2/LINUX26X86_64/1598668] 'user-sync //...'
Perforce server info:
2015/09/02 15:23:09 pid 1616 compute end .031s
Perforce server info:
2015/09/02 15:23:09 pid 1616 completed .031s
`
cmdTime, _ := time.Parse(p4timeformat, "2015/09/02 15:23:09")
historical := false
output := basicTest(t, cfg, input, historical)
baseExpected := eol.Split(`p4_cmd_counter{serverid="myserverid",cmd="user-sync"} 1
p4_cmd_cumulative_seconds{serverid="myserverid",cmd="user-sync"} 0.031
p4_cmd_program_counter{serverid="myserverid",program="p4/2016.2/LINUX26X86_64/1598668"} 1
p4_cmd_program_cumulative_seconds{serverid="myserverid",program="p4/2016.2/LINUX26X86_64/1598668"} 0.031
p4_cmd_running{serverid="myserverid"} 1
p4_prom_cmds_pending{serverid="myserverid"} 0
p4_prom_cmds_processed{serverid="myserverid"} 1
p4_prom_log_lines_read{serverid="myserverid"} 8
p4_sync_bytes_added{serverid="myserverid"} 0
p4_sync_bytes_updated{serverid="myserverid"} 0
p4_sync_files_added{serverid="myserverid"} 0
p4_sync_files_deleted{serverid="myserverid"} 0
p4_sync_files_updated{serverid="myserverid"} 0
p4_cmd_mem_mb{serverid="myserverid"} 0
p4_cmd_mem_peak_mb{serverid="myserverid"} 0
p4_cmds_paused_cumulative{serverid="myserverid"} 0.000
p4_cmds_paused_errors{serverid="myserverid"} 0
p4_cmds_paused_max{serverid="myserverid"} 0
p4_cmds_paused{serverid="myserverid"} 0
p4_cmds_running_max{serverid="myserverid"} 0
p4_cmds_running{serverid="myserverid"} 1
p4_lbr_binary_checkins{serverid="myserverid"} 0
p4_lbr_binary_closes{serverid="myserverid"} 0
p4_lbr_binary_copies{serverid="myserverid"} 0
p4_lbr_binary_digests{serverid="myserverid"} 0
p4_lbr_binary_exists{serverid="myserverid"} 0
p4_lbr_binary_filesizes{serverid="myserverid"} 0
p4_lbr_binary_modtimes{serverid="myserverid"} 0
p4_lbr_binary_opens{serverid="myserverid"} 0
p4_lbr_binary_readbytes{serverid="myserverid"} 0
p4_lbr_binary_reads{serverid="myserverid"} 0
p4_lbr_binary_writebytes{serverid="myserverid"} 0
p4_lbr_binary_writes{serverid="myserverid"} 0
p4_lbr_compress_checkins{serverid="myserverid"} 0
p4_lbr_compress_closes{serverid="myserverid"} 0
p4_lbr_compress_copies{serverid="myserverid"} 0
p4_lbr_compress_digests{serverid="myserverid"} 0
p4_lbr_compress_exists{serverid="myserverid"} 0
p4_lbr_compress_filesizes{serverid="myserverid"} 0
p4_lbr_compress_modtimes{serverid="myserverid"} 0
p4_lbr_compress_opens{serverid="myserverid"} 0
p4_lbr_compress_readbytes{serverid="myserverid"} 0
p4_lbr_compress_reads{serverid="myserverid"} 0
p4_lbr_compress_writebytes{serverid="myserverid"} 0
p4_lbr_compress_writes{serverid="myserverid"} 0
p4_lbr_rcs_checkins{serverid="myserverid"} 0
p4_lbr_rcs_closes{serverid="myserverid"} 0
p4_lbr_rcs_copies{serverid="myserverid"} 0
p4_lbr_rcs_digests{serverid="myserverid"} 0
p4_lbr_rcs_exists{serverid="myserverid"} 0
p4_lbr_rcs_filesizes{serverid="myserverid"} 0
p4_lbr_rcs_modtimes{serverid="myserverid"} 0
p4_lbr_rcs_opens{serverid="myserverid"} 0
p4_lbr_rcs_readbytes{serverid="myserverid"} 0
p4_lbr_rcs_reads{serverid="myserverid"} 0
p4_lbr_rcs_writebytes{serverid="myserverid"} 0
p4_lbr_rcs_writes{serverid="myserverid"} 0
p4_lbr_uncompress_checkins{serverid="myserverid"} 0
p4_lbr_uncompress_closes{serverid="myserverid"} 0
p4_lbr_uncompress_copies{serverid="myserverid"} 0
p4_lbr_uncompress_digests{serverid="myserverid"} 0
p4_lbr_uncompress_exists{serverid="myserverid"} 0
p4_lbr_uncompress_filesizes{serverid="myserverid"} 0
p4_lbr_uncompress_modtimes{serverid="myserverid"} 0
p4_lbr_uncompress_opens{serverid="myserverid"} 0
p4_lbr_uncompress_readbytes{serverid="myserverid"} 0
p4_lbr_uncompress_reads{serverid="myserverid"} 0
p4_lbr_uncompress_writebytes{serverid="myserverid"} 0
p4_lbr_uncompress_writes{serverid="myserverid"} 0
p4_pause_rate_cpu{serverid="myserverid"} 0
p4_pause_rate_mem{serverid="myserverid"} 0
p4_pause_state_cpu{serverid="myserverid"} 0
p4_pause_state_mem{serverid="myserverid"} 0
p4_prom_build_info{serverid="myserverid",goversion="go1.21.5",revision="testrevision",version="test"} 1
p4_prom_memory{serverid="myserverid"} 8283400
p4_prom_svr_events_processed{serverid="myserverid"} 0`, -1)
compareOutput(t, baseExpected, output)
historical = true
output = basicTest(t, cfg, input, historical)
// Cross check appropriate time is being produced for historical runs
assert.Contains(t, output[0], fmt.Sprintf("%d", cmdTime.Unix()))
baseExpectedHistorical := eol.Split(`p4_cmd_counter;serverid=myserverid;cmd=user-sync 1 1441207389
p4_cmd_cumulative_seconds;serverid=myserverid;cmd=user-sync 0.031 1441207389
p4_cmd_program_counter;serverid=myserverid;program=p4/2016.2/LINUX26X86_64/1598668 1 1441207389
p4_cmd_program_cumulative_seconds;serverid=myserverid;program=p4/2016.2/LINUX26X86_64/1598668 0.031 1441207389
p4_cmd_running;serverid=myserverid 1 1441207389
p4_prom_cmds_pending;serverid=myserverid 0 1441207389
p4_prom_cmds_processed;serverid=myserverid 1 1441207389
p4_prom_log_lines_read;serverid=myserverid 8 1441207389
p4_sync_bytes_added;serverid=myserverid 0 1441207389
p4_sync_bytes_updated;serverid=myserverid 0 1441207389
p4_sync_files_added;serverid=myserverid 0 1441207389
p4_sync_files_deleted;serverid=myserverid 0 1441207389
p4_sync_files_updated;serverid=myserverid 0 1441207389
p4_cmd_mem_mb;serverid=myserverid 0 1441207389
p4_cmd_mem_peak_mb;serverid=myserverid 0 1441207389
p4_cmds_paused;serverid=myserverid 0 1441207389
p4_cmds_paused_cumulative;serverid=myserverid 0.000 1441207389
p4_cmds_paused_errors;serverid=myserverid 0 1441207389
p4_cmds_paused_max;serverid=myserverid 0 1441207389
p4_cmds_running;serverid=myserverid 1 1441207389
p4_cmds_running_max;serverid=myserverid 0 1441207389
p4_lbr_binary_checkins;serverid=myserverid 0 1441207389
p4_lbr_binary_closes;serverid=myserverid 0 1441207389
p4_lbr_binary_copies;serverid=myserverid 0 1441207389
p4_lbr_binary_digests;serverid=myserverid 0 1441207389
p4_lbr_binary_exists;serverid=myserverid 0 1441207389
p4_lbr_binary_filesizes;serverid=myserverid 0 1441207389
p4_lbr_binary_modtimes;serverid=myserverid 0 1441207389
p4_lbr_binary_opens;serverid=myserverid 0 1441207389
p4_lbr_binary_readbytes;serverid=myserverid 0 1441207389
p4_lbr_binary_reads;serverid=myserverid 0 1441207389
p4_lbr_binary_writebytes;serverid=myserverid 0 1441207389
p4_lbr_binary_writes;serverid=myserverid 0 1441207389
p4_lbr_compress_checkins;serverid=myserverid 0 1441207389
p4_lbr_compress_closes;serverid=myserverid 0 1441207389
p4_lbr_compress_copies;serverid=myserverid 0 1441207389
p4_lbr_compress_digests;serverid=myserverid 0 1441207389
p4_lbr_compress_exists;serverid=myserverid 0 1441207389
p4_lbr_compress_filesizes;serverid=myserverid 0 1441207389
p4_lbr_compress_modtimes;serverid=myserverid 0 1441207389
p4_lbr_compress_opens;serverid=myserverid 0 1441207389
p4_lbr_compress_readbytes;serverid=myserverid 0 1441207389
p4_lbr_compress_reads;serverid=myserverid 0 1441207389
p4_lbr_compress_writebytes;serverid=myserverid 0 1441207389
p4_lbr_compress_writes;serverid=myserverid 0 1441207389
p4_lbr_rcs_checkins;serverid=myserverid 0 1441207389
p4_lbr_rcs_closes;serverid=myserverid 0 1441207389
p4_lbr_rcs_copies;serverid=myserverid 0 1441207389
p4_lbr_rcs_digests;serverid=myserverid 0 1441207389
p4_lbr_rcs_exists;serverid=myserverid 0 1441207389
p4_lbr_rcs_filesizes;serverid=myserverid 0 1441207389
p4_lbr_rcs_modtimes;serverid=myserverid 0 1441207389
p4_lbr_rcs_opens;serverid=myserverid 0 1441207389
p4_lbr_rcs_readbytes;serverid=myserverid 0 1441207389
p4_lbr_rcs_reads;serverid=myserverid 0 1441207389
p4_lbr_rcs_writebytes;serverid=myserverid 0 1441207389
p4_lbr_rcs_writes;serverid=myserverid 0 1441207389
p4_lbr_uncompress_checkins;serverid=myserverid 0 1441207389
p4_lbr_uncompress_closes;serverid=myserverid 0 1441207389
p4_lbr_uncompress_copies;serverid=myserverid 0 1441207389
p4_lbr_uncompress_digests;serverid=myserverid 0 1441207389
p4_lbr_uncompress_exists;serverid=myserverid 0 1441207389
p4_lbr_uncompress_filesizes;serverid=myserverid 0 1441207389
p4_lbr_uncompress_modtimes;serverid=myserverid 0 1441207389
p4_lbr_uncompress_opens;serverid=myserverid 0 1441207389
p4_lbr_uncompress_readbytes;serverid=myserverid 0 1441207389
p4_lbr_uncompress_reads;serverid=myserverid 0 1441207389
p4_lbr_uncompress_writebytes;serverid=myserverid 0 1441207389
p4_lbr_uncompress_writes;serverid=myserverid 0 1441207389
p4_pause_rate_cpu;serverid=myserverid 0 1441207389
p4_pause_rate_mem;serverid=myserverid 0 1441207389
p4_pause_state_cpu;serverid=myserverid 0 1441207389
p4_pause_state_mem;serverid=myserverid 0 1441207389
p4_prom_build_info;serverid=myserverid;goversion=go1.21.5;revision=testrevision;version=test 1 1441207389
p4_prom_memory;serverid=myserverid 9405704 1441207389
p4_prom_svr_events_processed;serverid=myserverid 0 1441207389`, -1)
compareOutput(t, baseExpectedHistorical, output)
// Now change config and expect some extra metrics to be output
cfg = &config.Config{
ServerID: "myserverid",
UpdateInterval: 10 * time.Millisecond,
OutputCmdsByUser: true,
OutputCmdsByUserRegex: ".*", // all users
OutputCmdsByIP: true,
}
expected := eol.Split(`p4_cmd_ip_counter{serverid="myserverid",ip="127.0.0.1"} 1
p4_cmd_ip_cumulative_seconds{serverid="myserverid",ip="127.0.0.1"} 0.031
p4_cmd_user_counter{serverid="myserverid",user="robert"} 1
p4_cmd_user_cumulative_seconds{serverid="myserverid",user="robert"} 0.031
p4_cmd_user_detail_counter{serverid="myserverid",user="robert",cmd="user-sync"} 1
p4_cmd_user_detail_cumulative_seconds{serverid="myserverid",user="robert",cmd="user-sync"} 0.031`, -1)
for _, l := range baseExpected {
expected = append(expected, l)
}
sort.Strings(expected)
historical = false
output = basicTest(t, cfg, input, historical)
compareOutput(t, expected, output)
expected = eol.Split(`p4_cmd_ip_counter;serverid=myserverid;ip=127.0.0.1 1 1441207389
p4_cmd_ip_cumulative_seconds;serverid=myserverid;ip=127.0.0.1 0.031 1441207389
p4_cmd_user_counter;serverid=myserverid;user=robert 1 1441207389
p4_cmd_user_cumulative_seconds;serverid=myserverid;user=robert 0.031 1441207389
p4_cmd_user_detail_counter;serverid=myserverid;user=robert;cmd=user-sync 1 1441207389
p4_cmd_user_detail_cumulative_seconds;serverid=myserverid;user=robert;cmd=user-sync 0.031 1441207389`, -1)
for _, l := range baseExpectedHistorical {
expected = append(expected, l)
}
sort.Strings(expected)
historical = true
output = basicTest(t, cfg, input, historical)
compareOutput(t, expected, output)
}