MCPcopy
hub / github.com/prometheus/client_golang / TestPush

Function TestPush

prometheus/push/push_test.go:29–309  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

27)
28
29func 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{}

Callers

nothing calls this directly

Calls 15

MustRegisterMethod · 0.95
GatherMethod · 0.95
NewCounterFunction · 0.92
NewGaugeFunction · 0.92
NewRegistryFunction · 0.92
CollectorMethod · 0.80
GroupingMethod · 0.80
GathererMethod · 0.80
NewFunction · 0.70
SetMethod · 0.65
AddMethod · 0.65
HeaderMethod · 0.45

Tested by

no test coverage detected