Debian Codegraph @

prometheus/push/push_test.go

raw
1 // Copyright 2016 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 package push
15
16 import (
17 "bytes"
18 "errors"
19 "io"
20 "net/http"
21 "net/http/httptest"
22 "testing"
23
24 "github.com/prometheus/common/expfmt"
25
26 "github.com/prometheus/client_golang/prometheus"
27 )
28
29 func TestPush(t *testing.T) {
30 var (
31 lastMethod string
32 lastBody []byte
33 lastPath string
34 lastHeader http.Header
35 )
36
37 // Fake a Pushgateway that responds with 202 to DELETE and with 200 in
38 // all other cases.
39 pgwOK := httptest.NewServer(
40 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
41 lastMethod = r.Method
42 lastHeader = r.Header
43 var err error
44 lastBody, err = io.ReadAll(r.Body)
45 if err != nil {
46 t.Fatal(err)
47 }
48 lastPath = r.URL.EscapedPath()
49 w.Header().Set("Content-Type", `text/plain; charset=utf-8`)
50 if r.Method == http.MethodDelete {
51 w.WriteHeader(http.StatusAccepted)
52 return
53 }
54 w.WriteHeader(http.StatusOK)
55 }),
56 )
57 defer pgwOK.Close()
58
59 // Fake a Pushgateway that always responds with 500.
60 pgwErr := httptest.NewServer(
61 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
62 http.Error(w, "fake error", http.StatusInternalServerError)
63 }),
64 )
65 defer pgwErr.Close()
66
67 metric1 := prometheus.NewCounter(prometheus.CounterOpts{
68 Name: "testname1",
69 Help: "testhelp1",
70 })
71 metric2 := prometheus.NewGauge(prometheus.GaugeOpts{
72 Name: "testname2",
73 Help: "testhelp2",
74 ConstLabels: prometheus.Labels{"foo": "bar", "dings": "bums"},
75 })
76
77 reg := prometheus.NewRegistry()
78 reg.MustRegister(metric1)
79 reg.MustRegister(metric2)
80
81 mfs, err := reg.Gather()
82 if err != nil {
83 t.Fatal(err)
84 }
85
86 buf := &bytes.Buffer{}
87 enc := expfmt.NewEncoder(buf, expfmt.NewFormat(expfmt.TypeProtoDelim))
88
89 for _, mf := range mfs {
90 if err := enc.Encode(mf); err != nil {
91 t.Fatal(err)
92 }
93 }
94 wantBody := buf.Bytes()
95
96 // Push some Collectors, all good.
97 if err := New(pgwOK.URL, "testjob").
98 Collector(metric1).
99 Collector(metric2).
100 Push(); err != nil {
101 t.Fatal(err)
102 }
103 if lastMethod != http.MethodPut {
104 t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
105 }
106 if !bytes.Equal(lastBody, wantBody) {
107 t.Errorf("got body %v, want %v", lastBody, wantBody)
108 }
109 if lastPath != "/metrics/job/testjob" {
110 t.Error("unexpected path:", lastPath)
111 }
112
113 // Add some Collectors, with nil grouping, all good.
114 if err := New(pgwOK.URL, "testjob").
115 Collector(metric1).
116 Collector(metric2).
117 Add(); err != nil {
118 t.Fatal(err)
119 }
120 if lastMethod != http.MethodPost {
121 t.Errorf("got method %q for Add, want %q", lastMethod, http.MethodPost)
122 }
123 if !bytes.Equal(lastBody, wantBody) {
124 t.Errorf("got body %v, want %v", lastBody, wantBody)
125 }
126 if lastPath != "/metrics/job/testjob" {
127 t.Error("unexpected path:", lastPath)
128 }
129
130 // Pushes that require base64 encoding.
131 if err := New(pgwOK.URL, "test/job").
132 Collector(metric1).
133 Collector(metric2).
134 Push(); err != nil {
135 t.Fatal(err)
136 }
137 if lastMethod != http.MethodPut {
138 t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
139 }
140 if !bytes.Equal(lastBody, wantBody) {
141 t.Errorf("got body %v, want %v", lastBody, wantBody)
142 }
143 if lastPath != "/metrics/job@base64/dGVzdC9qb2I" {
144 t.Error("unexpected path:", lastPath)
145 }
146 if err := New(pgwOK.URL, "testjob").
147 Grouping("foobar", "bu/ms").
148 Collector(metric1).
149 Collector(metric2).
150 Push(); err != nil {
151 t.Fatal(err)
152 }
153 if lastMethod != http.MethodPut {
154 t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
155 }
156 if !bytes.Equal(lastBody, wantBody) {
157 t.Errorf("got body %v, want %v", lastBody, wantBody)
158 }
159 if lastPath != "/metrics/job/testjob/foobar@base64/YnUvbXM" {
160 t.Error("unexpected path:", lastPath)
161 }
162
163 // Push that requires URL encoding.
164 if err := New(pgwOK.URL, "testjob").
165 Grouping("titan", "Προμηθεύς").
166 Collector(metric1).
167 Collector(metric2).
168 Push(); err != nil {
169 t.Fatal(err)
170 }
171 if lastMethod != http.MethodPut {
172 t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
173 }
174 if !bytes.Equal(lastBody, wantBody) {
175 t.Errorf("got body %v, want %v", lastBody, wantBody)
176 }
177 if lastPath != "/metrics/job/testjob/titan/%CE%A0%CF%81%CE%BF%CE%BC%CE%B7%CE%B8%CE%B5%CF%8D%CF%82" {
178 t.Error("unexpected path:", lastPath)
179 }
180
181 // Empty label value triggers special base64 encoding.
182 if err := New(pgwOK.URL, "testjob").
183 Grouping("empty", "").
184 Collector(metric1).
185 Collector(metric2).
186 Push(); err != nil {
187 t.Fatal(err)
188 }
189 if lastMethod != http.MethodPut {
190 t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
191 }
192 if !bytes.Equal(lastBody, wantBody) {
193 t.Errorf("got body %v, want %v", lastBody, wantBody)
194 }
195 if lastPath != "/metrics/job/testjob/empty@base64/=" {
196 t.Error("unexpected path:", lastPath)
197 }
198
199 // Empty job name results in error.
200 if err := New(pgwErr.URL, "").
201 Collector(metric1).
202 Collector(metric2).
203 Push(); err == nil {
204 t.Error("push with empty job succeeded")
205 } else {
206 if want := errJobEmpty; !errors.Is(err, want) {
207 t.Errorf("got error %q, want %q", err, want)
208 }
209 }
210
211 // Push some Collectors with a broken PGW.
212 if err := New(pgwErr.URL, "testjob").
213 Collector(metric1).
214 Collector(metric2).
215 Push(); err == nil {
216 t.Error("push to broken Pushgateway succeeded")
217 } else {
218 if got, want := err.Error(), "unexpected status code 500 while pushing to "+pgwErr.URL+"/metrics/job/testjob: fake error\n"; got != want {
219 t.Errorf("got error %q, want %q", got, want)
220 }
221 }
222
223 // Push some Collectors with invalid grouping or job.
224 if err := New(pgwOK.URL, "testjob").
225 Grouping("foo", "bums").
226 Collector(metric1).
227 Collector(metric2).
228 Push(); err == nil {
229 t.Error("push with grouping contained in metrics succeeded")
230 }
231 if err := New(pgwOK.URL, "testjob").
232 Grouping("foo\x80bar", "bums").
233 Collector(metric1).
234 Collector(metric2).
235 Push(); err == nil {
236 t.Error("push with invalid grouping succeeded")
237 }
238
239 // Push registry, all good.
240 if err := New(pgwOK.URL, "testjob").
241 Gatherer(reg).
242 Push(); err != nil {
243 t.Fatal(err)
244 }
245 if lastMethod != http.MethodPut {
246 t.Errorf("got method %q for Push, want %q", lastMethod, http.MethodPut)
247 }
248 if !bytes.Equal(lastBody, wantBody) {
249 t.Errorf("got body %v, want %v", lastBody, wantBody)
250 }
251
252 // Add registry, all good.
253 if err := New(pgwOK.URL, "testjob").
254 Grouping("a", "x").
255 Grouping("b", "y").
256 Gatherer(reg).
257 Add(); err != nil {
258 t.Fatal(err)
259 }
260 if lastMethod != http.MethodPost {
261 t.Errorf("got method %q for Add, want %q", lastMethod, http.MethodPost)
262 }
263 if !bytes.Equal(lastBody, wantBody) {
264 t.Errorf("got body %v, want %v", lastBody, wantBody)
265 }
266 if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
267 t.Error("unexpected path:", lastPath)
268 }
269
270 // Delete, all good.
271 if err := New(pgwOK.URL, "testjob").
272 Grouping("a", "x").
273 Grouping("b", "y").
274 Delete(); err != nil {
275 t.Fatal(err)
276 }
277 if lastMethod != http.MethodDelete {
278 t.Errorf("got method %q for Delete, want %q", lastMethod, http.MethodDelete)
279 }
280 if len(lastBody) != 0 {
281 t.Errorf("got body of length %d, want empty body", len(lastBody))
282 }
283 if lastPath != "/metrics/job/testjob/a/x/b/y" && lastPath != "/metrics/job/testjob/b/y/a/x" {
284 t.Error("unexpected path:", lastPath)
285 }
286
287 // Push some Collectors with custom header, all good.
288 header := make(http.Header)
289 header.Set("Authorization", "Bearer Token")
290 if err := New(pgwOK.URL, "testjob").
291 Collector(metric1).
292 Collector(metric2).
293 Header(header).
294 Push(); err != nil {
295 t.Fatal(err)
296 }
297 if lastMethod != http.MethodPut {
298 t.Errorf("got method %q for Add, want %q", lastMethod, http.MethodPut)
299 }
300 if !bytes.Equal(lastBody, wantBody) {
301 t.Errorf("got body %v, want %v", lastBody, wantBody)
302 }
303 if lastPath != "/metrics/job/testjob" {
304 t.Error("unexpected path:", lastPath)
305 }
306 if lastHeader == nil || lastHeader.Get("Authorization") == "" {
307 t.Error("empty Authorization header")
308 }
309 }