-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinstrumented.go
267 lines (221 loc) · 7.53 KB
/
instrumented.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.
package e2emon
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/efficientgo/core/backoff"
"github.com/efficientgo/core/errcapture"
"github.com/efficientgo/core/errors"
"github.com/efficientgo/e2e"
"github.com/prometheus/common/expfmt"
)
var errMissingMetric = errors.New("metric not found")
// Target represents scrape target for Prometheus to use.
type Target struct {
InternalEndpoint string
MetricPath string // "/metrics" by default.
Scheme string // "http" by default.
}
// Instrumented represents methods for instrumented runnable focused on accessing instrumented metrics.
type Instrumented interface {
MetricTargets() []Target
Metrics() (string, error)
WaitSumMetrics(expected MetricValueExpectation, metricNames ...string) error
WaitSumMetricsWithOptions(expected MetricValueExpectation, metricNames []string, opts ...MetricsOption) error
SumMetrics(metricNames []string, opts ...MetricsOption) ([]float64, error)
WaitRemovedMetric(metricName string, opts ...MetricsOption) error
}
var _ Instrumented = &InstrumentedRunnable{}
// InstrumentedRunnable represents runnable with instrumented Prometheus metric endpoint on a certain port.
type InstrumentedRunnable struct {
e2e.Runnable
metricPortName string
metricPath string
scheme string
waitBackoff *backoff.Backoff
}
type rOpt struct {
metricPath string
scheme string
waitBackoff *backoff.Backoff
}
// WithInstrumentedMetricPath sets a custom path for metrics page. "/metrics" by default.
func WithInstrumentedMetricPath(metricPath string) InstrumentedOption {
return func(o *rOpt) {
o.metricPath = metricPath
}
}
// WithInstrumentedScheme allows adding customized scheme. "http" or "https" values allowed. "http" by default.
// If "https" is specified, insecure TLS will be performed.
func WithInstrumentedScheme(scheme string) InstrumentedOption {
return func(o *rOpt) {
o.scheme = scheme
}
}
// WithInstrumentedWaitBackoff allows customizing wait backoff when accessing or asserting on the metric endpoint.
func WithInstrumentedWaitBackoff(waitBackoff *backoff.Backoff) InstrumentedOption {
return func(o *rOpt) {
o.waitBackoff = waitBackoff
}
}
// InstrumentedOption is a variadic option for AsInstrumented.
type InstrumentedOption func(*rOpt)
// AsInstrumented wraps e2e.Runnable with InstrumentedRunnable.
// If runnable is running during invocation AsInstrumented panics.
// NOTE(bwplotka): Caller is expected to discard passed `r` runnable and use returned InstrumentedRunnable.Runnable instead.
func AsInstrumented(r e2e.Runnable, instrumentedPortName string, opts ...InstrumentedOption) *InstrumentedRunnable {
if r.IsRunning() {
panic("can't use AsInstrumented with running runnable")
}
opt := rOpt{
metricPath: "/metrics",
scheme: "http",
waitBackoff: backoff.New(context.Background(), backoff.Config{
Min: 300 * time.Millisecond,
Max: 600 * time.Millisecond,
MaxRetries: 50, // Sometimes the CI is slow ¯\_(ツ)_/¯
})}
for _, o := range opts {
o(&opt)
}
if r.InternalEndpoint(instrumentedPortName) == "" {
return &InstrumentedRunnable{Runnable: e2e.NewFailedRunnable(
r.Name(),
errors.Newf("metric port name %v does not exists in given runnable ports", instrumentedPortName)),
}
}
instr := &InstrumentedRunnable{
Runnable: r,
metricPortName: instrumentedPortName,
metricPath: opt.metricPath,
scheme: opt.scheme,
waitBackoff: opt.waitBackoff,
}
r.SetMetadata(metaKey, Instrumented(instr))
return instr
}
func (r *InstrumentedRunnable) MetricTargets() []Target {
return []Target{{Scheme: r.scheme, MetricPath: r.metricPath, InternalEndpoint: r.InternalEndpoint(r.metricPortName)}}
}
func (r *InstrumentedRunnable) Metrics() (_ string, err error) {
if !r.IsRunning() {
return "", errors.Newf("%s is not running", r.Name())
}
// Fetch metrics.
res, err := (&http.Client{Timeout: 5 * time.Second}).Get(fmt.Sprintf("http://%s/metrics", r.Endpoint(r.metricPortName)))
if err != nil {
return "", err
}
// Check the status code.
if res.StatusCode < 200 || res.StatusCode >= 300 {
return "", errors.Newf("unexpected status code %d while fetching metrics", res.StatusCode)
}
defer errcapture.ExhaustClose(&err, res.Body, "metrics response")
body, err := io.ReadAll(res.Body)
return string(body), err
}
func (r *InstrumentedRunnable) buildMetricsOptions(opts []MetricsOption) metricsOptions {
result := metricsOptions{
getValue: getMetricValue,
waitBackoff: r.waitBackoff,
}
for _, opt := range opts {
opt(&result)
}
return result
}
// WaitSumMetrics waits for at least one instance of each given metric names to be present and their sums,
// returning true when passed to given expected(...).
func (r *InstrumentedRunnable) WaitSumMetrics(expected MetricValueExpectation, metricNames ...string) error {
return r.WaitSumMetricsWithOptions(expected, metricNames)
}
func (r *InstrumentedRunnable) WaitSumMetricsWithOptions(expected MetricValueExpectation, metricNames []string, opts ...MetricsOption) error {
var (
sums []float64
err error
options = r.buildMetricsOptions(opts)
)
for options.waitBackoff.Reset(); options.waitBackoff.Ongoing(); {
sums, err = r.SumMetrics(metricNames, opts...)
if options.waitMissingMetrics && errors.Is(err, errMissingMetric) {
options.waitBackoff.Wait()
continue
}
if err != nil {
return err
}
if expected(sums...) {
return nil
}
options.waitBackoff.Wait()
}
return errors.Newf("unable to find metrics %s with expected values after %d retries. Last error: %v. Last values: %v", metricNames, options.waitBackoff.NumRetries(), err, sums)
}
// SumMetrics returns the sum of the values of each given metric names.
func (r *InstrumentedRunnable) SumMetrics(metricNames []string, opts ...MetricsOption) ([]float64, error) {
options := r.buildMetricsOptions(opts)
sums := make([]float64, len(metricNames))
metrics, err := r.Metrics()
if err != nil {
return nil, err
}
var tp expfmt.TextParser
families, err := tp.TextToMetricFamilies(strings.NewReader(metrics))
if err != nil {
return nil, err
}
for i, m := range metricNames {
sums[i] = 0.0
// Get the metric family.
mf, ok := families[m]
if !ok {
if options.skipMissingMetrics {
continue
}
return nil, errors.Wrapf(errMissingMetric, "metric=%s service=%s", m, r.Name())
}
// Filter metrics.
metrics := filterMetrics(mf.GetMetric(), options)
if len(metrics) == 0 {
if options.skipMissingMetrics {
continue
}
return nil, errors.Wrapf(errMissingMetric, "metric=%s service=%s", m, r.Name())
}
sums[i] = SumValues(getValues(metrics, options))
}
return sums, nil
}
// WaitRemovedMetric waits until a metric disappear from the list of metrics exported by the service.
func (r *InstrumentedRunnable) WaitRemovedMetric(metricName string, opts ...MetricsOption) error {
options := r.buildMetricsOptions(opts)
for options.waitBackoff.Reset(); options.waitBackoff.Ongoing(); {
// Fetch metrics.
metrics, err := r.Metrics()
if err != nil {
return err
}
// Parse metrics.
var tp expfmt.TextParser
families, err := tp.TextToMetricFamilies(strings.NewReader(metrics))
if err != nil {
return err
}
// Get the metric family.
mf, ok := families[metricName]
if !ok {
return nil
}
// Filter metrics.
if len(filterMetrics(mf.GetMetric(), options)) == 0 {
return nil
}
options.waitBackoff.Wait()
}
return errors.Newf("the metric %s is still exported by %s", metricName, r.Name())
}