-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmetrics_test.go
88 lines (69 loc) · 2.5 KB
/
metrics_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
package worker_test
import (
"strings"
"testing"
"github.com/ankorstore/yokai/worker"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
)
func TestWorkerMetrics(t *testing.T) {
t.Parallel()
registry := prometheus.NewPedanticRegistry()
metrics := worker.NewWorkerMetrics("", "")
err := metrics.Register(registry)
assert.NoError(t, err)
metrics.IncrementWorkerExecutionStart("foo")
metrics.IncrementWorkerExecutionRestart("foo")
metrics.IncrementWorkerExecutionError("foo")
metrics.IncrementWorkerExecutionSuccess("foo")
expected := `
# HELP worker_executions_total Total number of workers executions
# TYPE worker_executions_total counter
worker_executions_total{status="error",worker="foo"} 1
worker_executions_total{status="restarted",worker="foo"} 1
worker_executions_total{status="started",worker="foo"} 1
worker_executions_total{status="success",worker="foo"} 1
`
err = testutil.GatherAndCompare(
registry,
strings.NewReader(expected),
"worker_executions_total",
)
assert.NoError(t, err)
}
func TestWorkerMetricsWithNamespaceAndSubsystem(t *testing.T) {
t.Parallel()
registry := prometheus.NewPedanticRegistry()
metrics := worker.NewWorkerMetrics("foo", "bar")
err := metrics.Register(registry)
assert.NoError(t, err)
metrics.IncrementWorkerExecutionStart("foo")
metrics.IncrementWorkerExecutionRestart("foo")
metrics.IncrementWorkerExecutionError("foo")
metrics.IncrementWorkerExecutionSuccess("foo")
expected := `
# HELP foo_bar_worker_executions_total Total number of workers executions
# TYPE foo_bar_worker_executions_total counter
foo_bar_worker_executions_total{status="error",worker="foo"} 1
foo_bar_worker_executions_total{status="restarted",worker="foo"} 1
foo_bar_worker_executions_total{status="started",worker="foo"} 1
foo_bar_worker_executions_total{status="success",worker="foo"} 1
`
err = testutil.GatherAndCompare(
registry,
strings.NewReader(expected),
"foo_bar_worker_executions_total",
)
assert.NoError(t, err)
}
func TestWorkerMetricsWithCollectorAlreadyRegistered(t *testing.T) {
t.Parallel()
registry := prometheus.NewPedanticRegistry()
metrics := worker.NewWorkerMetrics("foo", "bar")
err := metrics.Register(registry)
assert.NoError(t, err)
err = metrics.Register(registry)
assert.Error(t, err)
assert.Equal(t, "duplicate metrics collector registration attempted", err.Error())
}