forked from dcos/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus_test.go
223 lines (198 loc) · 7.31 KB
/
prometheus_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
package prometheus
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const sampleTextFormat = `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0.00010425500000000001
go_gc_duration_seconds{quantile="0.25"} 0.000139108
go_gc_duration_seconds{quantile="0.5"} 0.00015749400000000002
go_gc_duration_seconds{quantile="0.75"} 0.000331463
go_gc_duration_seconds{quantile="1"} 0.000667154
go_gc_duration_seconds_sum 0.0018183950000000002
go_gc_duration_seconds_count 7
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 15
# HELP test_metric An untyped metric with a timestamp
# TYPE test_metric untyped
test_metric{label="value"} 1.0 1490802350000
`
func TestPrometheusGeneratesMetrics(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, sampleTextFormat)
}))
defer ts.Close()
p := &Prometheus{
URLs: []string{ts.URL},
}
var acc testutil.Accumulator
err := acc.GatherError(p.Gather)
require.NoError(t, err)
assert.True(t, acc.HasFloatField("go_gc_duration_seconds", "count"))
assert.True(t, acc.HasFloatField("go_goroutines", "gauge"))
assert.True(t, acc.HasFloatField("test_metric", "value"))
assert.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
assert.False(t, acc.HasTag("test_metric", "address"))
assert.True(t, acc.TagValue("test_metric", "url") == ts.URL+"/metrics")
}
func TestPrometheusGeneratesMetricsWithHostNameTag(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, sampleTextFormat)
}))
defer ts.Close()
p := &Prometheus{
KubernetesServices: []string{ts.URL},
}
u, _ := url.Parse(ts.URL)
tsAddress := u.Hostname()
var acc testutil.Accumulator
err := acc.GatherError(p.Gather)
require.NoError(t, err)
assert.True(t, acc.HasFloatField("go_gc_duration_seconds", "count"))
assert.True(t, acc.HasFloatField("go_goroutines", "gauge"))
assert.True(t, acc.HasFloatField("test_metric", "value"))
assert.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
assert.True(t, acc.TagValue("test_metric", "address") == tsAddress)
assert.True(t, acc.TagValue("test_metric", "url") == ts.URL)
}
func TestPrometheusGeneratesMetricsAlthoughFirstDNSFails(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, sampleTextFormat)
}))
defer ts.Close()
p := &Prometheus{
URLs: []string{ts.URL},
KubernetesServices: []string{"http://random.telegraf.local:88/metrics"},
}
var acc testutil.Accumulator
err := acc.GatherError(p.Gather)
require.NoError(t, err)
assert.True(t, acc.HasFloatField("go_gc_duration_seconds", "count"))
assert.True(t, acc.HasFloatField("go_goroutines", "gauge"))
assert.True(t, acc.HasFloatField("test_metric", "value"))
assert.True(t, acc.HasTimestamp("test_metric", time.Unix(1490802350, 0)))
}
func TestPrometheusGathersMesosMetrics(t *testing.T) {
testCases := map[string]map[string]URLAndAddress{
"empty": {},
"malformedTaskLabelIndex": {},
"taskLabelIndexOutOfRange": {},
"wrongPortName": {},
"portlabel": {
"http://127.0.0.1:12345/metrics": {
URL: unsafelyParse("http://127.0.0.1:12345/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:12345/metrics"),
Tags: map[string]string{"container_id": "abc-123"},
},
"http://127.0.0.1:12345/federate": {
URL: unsafelyParse("http://127.0.0.1:12345/federate"),
OriginalURL: unsafelyParse("http://127.0.0.1:12345/federate"),
Tags: map[string]string{"container_id": "xyz-123"},
},
},
"tasklabelViaIndex": {
"http://127.0.0.1:12345/metrics": {
URL: unsafelyParse("http://127.0.0.1:12345/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:12345/metrics"),
Tags: map[string]string{"container_id": "abc-123"},
},
},
"tasklabelViaName": {
"http://127.0.0.1:12345/metrics": {
URL: unsafelyParse("http://127.0.0.1:12345/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:12345/metrics"),
Tags: map[string]string{"container_id": "abc-123"},
},
},
"tasklabelIndexPriority": {
"http://127.0.0.1:12345/metrics": {
URL: unsafelyParse("http://127.0.0.1:12345/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:12345/metrics"),
Tags: map[string]string{"container_id": "abc-123"},
},
},
"tasklabelAlternatePath": {
"http://127.0.0.1:12345/federate": {
URL: unsafelyParse("http://127.0.0.1:12345/federate"),
OriginalURL: unsafelyParse("http://127.0.0.1:12345/federate"),
Tags: map[string]string{"container_id": "abc-123"},
},
},
"networkModes": {
// Host mode should use hostname:port
"http://127.0.0.1:7070/metrics": {
URL: unsafelyParse("http://127.0.0.1:7070/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:7070/metrics"),
Tags: map[string]string{"container_id": "host-123"},
},
// Bridge mode should use hostname:mapped-port (not hostname:container-port)
"http://127.0.0.1:8080/metrics": {
URL: unsafelyParse("http://127.0.0.1:8080/metrics"),
OriginalURL: unsafelyParse("http://127.0.0.1:8080/metrics"),
Tags: map[string]string{"container_id": "bridge-123"},
},
// Container mode should use task-ip:container-port
"http://198.0.2.3:9090/metrics": {
URL: unsafelyParse("http://198.0.2.3:9090/metrics"),
OriginalURL: unsafelyParse("http://198.0.2.3:9090/metrics"),
Tags: map[string]string{"container_id": "container-123"},
},
},
}
for scenario, expected := range testCases {
t.Run(scenario, func(t *testing.T) {
server := startTestServer(t, scenario)
defer server.Close()
p := &Prometheus{
MesosTimeout: internal.Duration{Duration: 500 * time.Millisecond},
MesosAgentUrl: server.URL,
// mesosHostname is assigned in Start()
mesosHostname: "127.0.0.1",
}
urls, err := p.GetAllURLs()
assert.Nil(t, err)
assert.Equal(t, expected, urls)
})
}
}
func TestGetMesosHostname(t *testing.T) {
goodUrls := map[string]string{
"http://localhost": "localhost",
"http://localhost:9090": "localhost",
"http://192.168.2.2": "192.168.2.2",
"http://192.168.2.2:9090": "192.168.2.2",
"https://192.168.2.2": "192.168.2.2",
"http://some-agent.testing.example.com/": "some-agent.testing.example.com",
}
badUrls := []string{
"$UNPARSED_ENVIRONMENT_VARIABLE",
"",
}
for input, expected := range goodUrls {
output, err := getMesosHostname(input)
assert.Nil(t, err)
assert.Equal(t, expected, output)
}
for _, input := range badUrls {
_, err := getMesosHostname(input)
assert.NotNil(t, err)
}
}
// unsafelyParse is a utility method that ignores errors from url.Parse
func unsafelyParse(u string) *url.URL {
result, _ := url.Parse(u)
return result
}