-
Notifications
You must be signed in to change notification settings - Fork 4
/
tailer_test.go
164 lines (147 loc) · 3.57 KB
/
tailer_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
package main
import (
"bytes"
"net/http/httptest"
"strings"
"testing"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func TestHandleLine(t *testing.T) {
patterns := []*PatternConfig{
&PatternConfig{
Match: ".*foo.*",
Metric: "lines_with_foo",
Help: "number of lines which contained 'foo'",
Type: "counter",
Action: "inc",
Value: "1",
Continue: true,
},
&PatternConfig{
Match: "this sets.*bar.*",
Metric: "current_bar",
Help: "current bar value",
Type: "gauge",
Action: "set",
Value: "1",
},
&PatternConfig{
Match: "with labels.*",
Metric: "with_labels",
Help: "with labels",
Type: "gauge",
Action: "set",
Value: "1",
Labels: map[string]string{
"foo": "bar",
},
},
&PatternConfig{
Match: "calculated_label=(?P<a>.+)",
Metric: "calculated_label",
Help: "calculated label",
Type: "counter",
Action: "inc",
Value: "1",
Labels: map[string]string{
"calculated": "$a",
},
},
&PatternConfig{
Match: "request status: failed",
Metric: "request_status_failed",
Help: "test",
Type: "counter",
Action: "inc",
Value: "1",
Continue: true,
},
&PatternConfig{
Match: "request status: .*",
Metric: "request_status_total",
Help: "test",
Type: "counter",
Action: "inc",
Value: "1",
},
&PatternConfig{
Match: "send status: failed",
Metric: "send_status_failed",
Help: "test",
Type: "counter",
Action: "inc",
Value: "1",
Continue: false,
},
&PatternConfig{
Match: "send status: .*",
Metric: "send_status_exceptfailed",
Help: "test",
Type: "counter",
Action: "inc",
Value: "1",
},
&PatternConfig{
Match: "starting job",
Metric: "jobs_inflight",
Help: "Currently running jobs",
Type: "gauge",
Action: "inc",
Value: "1",
},
&PatternConfig{
Match: "finishing job",
Metric: "jobs_inflight",
Help: "Currently running jobs",
Type: "gauge",
Action: "dec",
Value: "1",
},
}
err := registerMetrics(patterns)
if err != nil {
t.Fatalf("failed to register metrics %s", err)
}
handleLine("uncounted", patterns)
handler := promhttp.Handler()
want := func(wanted string) {
req := httptest.NewRequest("GET", "/metrics", &bytes.Buffer{})
rc := httptest.NewRecorder()
handler.ServeHTTP(rc, req)
got := rc.Body.String()
if !strings.Contains(got, wanted) {
t.Fatalf("did not find '%s' in output", wanted)
}
}
handleLine("counted as 'foo' appears", patterns)
want("\nlines_with_foo 1\n")
handleLine("this sets current bar...", patterns)
want("\ncurrent_bar 1\n")
handleLine("with labels...", patterns)
want("\nwith_labels{foo=\"bar\"} 1\n")
handleLine("calculated_label=foo", patterns)
want("\ncalculated_label{calculated=\"foo\"} 1\n")
handleLine("request status: failed", patterns)
want("\nrequest_status_total 1\n")
want("\nrequest_status_failed 1\n")
handleLine("send status: ok", patterns)
handleLine("send status: failed", patterns)
want("\nsend_status_failed 1\n")
want("\nsend_status_exceptfailed 1\n")
handleLine("starting job", patterns)
want("\njobs_inflight 1\n")
handleLine("starting job", patterns)
want("\njobs_inflight 2\n")
handleLine("finishing job", patterns)
want("\njobs_inflight 1\n")
handleLine("finishing job", patterns)
want("\njobs_inflight 0\n")
}
func TestRunTailer(t *testing.T) {
c := &Config{}
tailers := NewSafeTailers()
err := runTailer(c, "doc/example.yaml", tailers)
if err != nil {
t.Fatalf("Got error: %v, wanted: nil", err)
}
}