-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
93 lines (76 loc) · 2.4 KB
/
middleware_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
package stats
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/supershal/stats/metrics"
)
func TestHTTPResponseStat(t *testing.T) {
metrics.Reset()
w := &StatsWriter{
Writer: httptest.NewRecorder(),
StartTime: time.Now(),
}
tags := map[string]string{
"foo": "bar",
}
content := []byte("baz")
w.Write(content)
f := makeHttpResponseStat()
f(w, tags)
c, g := metrics.Snapshot()
assert.Equal(t, 2, len(c)) // "200" and "total"
assert.Equal(t, 7, len(g)) // "size" and 6 "latencies"
assert.Equal(t, uint64(1), c["http_response,foo=bar 200"])
assert.Equal(t, uint64(1), c["http_response,foo=bar total"])
assert.Equal(t, int64(len(content)), g["http_response,foo=bar size"])
assert.Contains(t, g, "http_response,foo=bar latency.P50")
assert.Contains(t, g, "http_response,foo=bar latency.P75")
assert.Contains(t, g, "http_response,foo=bar latency.P90")
assert.Contains(t, g, "http_response,foo=bar latency.P95")
assert.Contains(t, g, "http_response,foo=bar latency.P99")
assert.Contains(t, g, "http_response,foo=bar latency.P999")
}
func TestHTTPRequestStat(t *testing.T) {
metrics.Reset()
r := http.Request{
Method: "GET",
}
tags := map[string]string{
"foo": "bar",
}
f := makeHttpRequestStat()
f(r, tags)
c, _ := metrics.Snapshot()
assert.Equal(t, 1, len(c))
assert.Equal(t, uint64(1), c["http_request,foo=bar GET"])
}
func TestHTTPMetricsSnapshotLines(t *testing.T) {
metrics.Reset()
w := &StatsWriter{
Writer: httptest.NewRecorder(),
StartTime: time.Now(),
}
tags := map[string]string{
"foo": "bar",
}
content := []byte("baz")
w.Write(content)
f := makeHttpResponseStat()
f(w, tags)
lines := HTTPMetricsSnapshotLines()
// "200" + "total" + "size" + 6 "latencies"
assert.Equal(t, 9, len(strings.Split(strings.Trim(lines, "\n"), "\n")))
assert.Contains(t, lines, "http_response,foo=bar 200=1")
assert.Contains(t, lines, "http_response,foo=bar total=1")
assert.Contains(t, lines, "http_response,foo=bar size=3")
assert.Contains(t, lines, "http_response,foo=bar latency.P50=")
assert.Contains(t, lines, "http_response,foo=bar latency.P75=")
assert.Contains(t, lines, "http_response,foo=bar latency.P90=")
assert.Contains(t, lines, "http_response,foo=bar latency.P95=")
assert.Contains(t, lines, "http_response,foo=bar latency.P99=")
assert.Contains(t, lines, "http_response,foo=bar latency.P999=")
}