-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmetrics_test.go
More file actions
95 lines (75 loc) · 2.57 KB
/
Copy pathmetrics_test.go
File metadata and controls
95 lines (75 loc) · 2.57 KB
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
package main
import (
"io"
"net"
"net/http"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
)
func getCounterValue(t *testing.T, cv *prometheus.CounterVec, labels ...string) float64 {
t.Helper()
m := &dto.Metric{}
c, err := cv.GetMetricWithLabelValues(labels...)
require.NoError(t, err)
require.NoError(t, c.Write(m))
return m.GetCounter().GetValue()
}
func TestMetricsIncrement(t *testing.T) {
t.Parallel()
m := newMetricsCollector()
// All counters start at zero.
require.Equal(t, float64(0), getCounterValue(t, m.requestsTotal, "PostLogSource", "success"))
require.Equal(t, float64(0), getCounterValue(t, m.requestsTotal, "PostLogSource", "failure"))
require.Equal(t, float64(0), getCounterValue(t, m.requestsTotal, "SendLog", "success"))
// Simulate success
m.record(methodPostLogSource, nil)
require.Equal(t, float64(1), getCounterValue(t, m.requestsTotal, "PostLogSource", "success"))
// Simulate failure
m.record(methodPostLogSource, io.ErrUnexpectedEOF)
require.Equal(t, float64(1), getCounterValue(t, m.requestsTotal, "PostLogSource", "failure"))
// Simulate send success
m.record(methodSendLog, nil)
require.Equal(t, float64(1), getCounterValue(t, m.requestsTotal, "SendLog", "success"))
}
func TestMetricsHandler(t *testing.T) {
t.Parallel()
m := newMetricsCollector()
handler := m.handler()
require.NotNil(t, handler)
}
func TestMetricsEndpoint(t *testing.T) {
t.Parallel()
m := newMetricsCollector()
// Pick a random free port.
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
addr := listener.Addr().String()
_ = listener.Close()
mux := http.NewServeMux()
mux.Handle("/metrics", m.handler())
srv := &http.Server{Addr: addr, Handler: mux}
go func() { _ = srv.ListenAndServe() }()
t.Cleanup(func() { _ = srv.Close() })
// Wait for the server to be ready.
require.Eventually(t, func() bool {
resp, err := http.Get("http://" + addr + "/metrics")
if err != nil {
return false
}
_ = resp.Body.Close()
return resp.StatusCode == http.StatusOK
}, 2*time.Second, 50*time.Millisecond)
// Bump a counter and verify it appears in the output.
m.record(methodPostLogSource, nil)
resp, err := http.Get("http://" + addr + "/metrics")
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.True(t, strings.Contains(string(body), "coder_logstream_requests_total"),
"expected coder_logstream_requests_total in metrics output")
}