-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
metrics_test.go
165 lines (146 loc) · 4.68 KB
/
metrics_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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package httpjson
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"golang.org/x/sync/errgroup"
"github.com/elastic/beats/v7/libbeat/monitoring/inputmon"
beattest "github.com/elastic/beats/v7/libbeat/publisher/testing"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/monitoring"
)
func TestMetrics(t *testing.T) {
testCases := []struct {
name string
setupServer func(*testing.T, http.HandlerFunc, map[string]interface{})
baseConfig map[string]interface{}
handler http.HandlerFunc
expectedEvents []string
assertMetrics func(reg *monitoring.Registry) error
}{
{
name: "Test pagination metrics",
setupServer: func(t *testing.T, h http.HandlerFunc, config map[string]interface{}) {
server := httptest.NewServer(h)
config["request.url"] = server.URL
t.Cleanup(server.Close)
},
baseConfig: map[string]interface{}{
"interval": time.Millisecond,
"request.method": http.MethodPost,
"request.body": map[string]interface{}{
"field": "value",
},
"response.split": map[string]interface{}{
"target": "body.items",
"transforms": []interface{}{
map[string]interface{}{
"set": map[string]interface{}{
"target": "body.page",
"value": "[[.last_response.page]]",
},
},
},
},
"response.pagination": []interface{}{
map[string]interface{}{
"set": map[string]interface{}{
"target": "url.params.page",
"value": "[[.last_response.body.nextPageToken]]",
"fail_on_template_error": true,
},
},
},
},
handler: paginationHandler(),
expectedEvents: []string{
`{"foo":"a","page":"0"}`, `{"foo":"b","page":"1"}`, `{"foo":"c","page":"0"}`, `{"foo":"d","page":"0"}`,
`{"foo":"a","page":"0"}`, `{"foo":"b","page":"1"}`, `{"foo":"c","page":"0"}`, `{"foo":"d","page":"0"}`,
},
assertMetrics: func(reg *monitoring.Registry) error {
checkHasValue := func(v interface{}) bool {
switch t := v.(type) {
case int64:
return t > 0
case map[string]interface{}:
h := t["histogram"].(map[string]interface{})
return h["count"].(int64) > 0 && h["max"].(int64) > 0
}
return false
}
snapshot := monitoring.CollectStructSnapshot(reg, monitoring.Full, true)
for _, m := range []string{
"http_request_body_bytes", "http_request_post_total",
"http_request_total", "http_response_2xx_total",
"http_response_body_bytes", "http_response_total",
"http_round_trip_time", "httpjson_interval_execution_time",
"httpjson_interval_pages_execution_time", "httpjson_interval_total",
} {
if !checkHasValue(snapshot[m]) {
return fmt.Errorf("expected non zero value for metric %s", m)
}
}
return nil
},
},
}
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
tc.setupServer(t, tc.handler, tc.baseConfig)
cfg := conf.MustNewConfigFrom(tc.baseConfig)
conf := defaultConfig()
assert.NoError(t, cfg.Unpack(&conf))
chanClient := beattest.NewChanClient(len(tc.expectedEvents))
t.Cleanup(func() { _ = chanClient.Close() })
ctx, cancel := newV2Context("httpjson-foo-eb837d4c-5ced-45ed-b05c-de658135e248::https://somesource/someapi")
t.Cleanup(cancel)
reg, unreg := inputmon.NewInputRegistry("httpjson-test", ctx.ID, nil)
t.Cleanup(unreg)
var g errgroup.Group
g.Go(func() error {
pub := statelessPublisher{wrapped: chanClient}
return run(ctx, conf, pub, nil, reg)
})
timeout := time.NewTimer(5 * time.Second)
t.Cleanup(func() { _ = timeout.Stop() })
if len(tc.expectedEvents) == 0 {
select {
case <-timeout.C:
case got := <-chanClient.Channel:
t.Errorf("unexpected event: %v", got)
}
cancel()
assert.NoError(t, g.Wait())
return
}
var receivedCount int
wait:
for {
select {
case <-timeout.C:
t.Errorf("timed out waiting for %d events", len(tc.expectedEvents))
cancel()
return
case got := <-chanClient.Channel:
val, err := got.Fields.GetValue("message")
assert.NoError(t, err)
assert.JSONEq(t, tc.expectedEvents[receivedCount], val.(string))
receivedCount += 1
if receivedCount == len(tc.expectedEvents) {
cancel()
break wait
}
}
}
assert.NoError(t, g.Wait())
assert.NoError(t, tc.assertMetrics(reg))
})
}
}