forked from efficientgo/e2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumented.go
224 lines (180 loc) · 5.78 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
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.
package e2e
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/efficientgo/tools/core/pkg/backoff"
"github.com/efficientgo/tools/core/pkg/errcapture"
"github.com/pkg/errors"
"github.com/prometheus/common/expfmt"
)
var errMissingMetric = errors.New("metric not found")
type MetricTarget struct {
InternalEndpoint string
MetricPath string
}
// Instrumented is implemented by all instrumented runnables.
type Instrumented interface {
MetricTargets() []MetricTarget
}
var _ Instrumented = &InstrumentedRunnable{}
// InstrumentedRunnable represents opinionated microservice with one port marked as HTTP port with metric endpoint.
type InstrumentedRunnable struct {
Runnable
name string
metricPortName string
metricPath string
ports map[string]int
waitBackoff *backoff.Backoff
}
type FutureInstrumentedRunnable struct {
FutureRunnable
r *InstrumentedRunnable
}
func NewInstrumentedRunnable(
env Environment,
name string,
ports map[string]int,
metricPortName string,
) *FutureInstrumentedRunnable {
f := &FutureInstrumentedRunnable{
r: &InstrumentedRunnable{name: name, ports: ports, metricPortName: metricPortName, metricPath: "/metrics"},
}
if _, ok := ports[metricPortName]; !ok {
f.FutureRunnable = NewErrorer(name, errors.Errorf("metric port name %v does not exists in given ports", metricPortName))
return f
}
f.FutureRunnable = env.Runnable(name).WithPorts(ports).WithConcreteType(f.r).Future()
return f
}
func NewErrInstrumentedRunnable(name string, err error) *InstrumentedRunnable {
return &InstrumentedRunnable{
Runnable: NewErrorer(name, err),
}
}
func (r *FutureInstrumentedRunnable) Init(opts StartOptions) *InstrumentedRunnable {
if opts.WaitReadyBackoff == nil {
opts.WaitReadyBackoff = &backoff.Config{
Min: 300 * time.Millisecond,
Max: 600 * time.Millisecond,
MaxRetries: 50, // Sometimes the CI is slow ¯\_(ツ)_/¯.
}
}
r.r.waitBackoff = backoff.New(context.Background(), *opts.WaitReadyBackoff)
r.r.Runnable = r.FutureRunnable.Init(opts)
return r.r
}
func (r *InstrumentedRunnable) MetricTargets() []MetricTarget {
return []MetricTarget{{MetricPath: r.metricPath, InternalEndpoint: r.InternalEndpoint(r.metricPortName)}}
}
func (r *InstrumentedRunnable) Metrics() (_ string, err error) {
if !r.IsRunning() {
return "", errors.Errorf("%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.Errorf("unexpected status code %d while fetching metrics", res.StatusCode)
}
defer errcapture.ExhaustClose(&err, res.Body, "metrics response")
body, err := ioutil.ReadAll(res.Body)
return string(body), err
}
// 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 = buildMetricsOptions(opts)
)
for r.waitBackoff.Reset(); r.waitBackoff.Ongoing(); {
sums, err = r.SumMetrics(metricNames, opts...)
if options.waitMissingMetrics && errors.Is(err, errMissingMetric) {
continue
}
if err != nil {
return err
}
if expected(sums...) {
return nil
}
r.waitBackoff.Wait()
}
return errors.Errorf("unable to find metrics %s with expected values after %d retries. Last error: %v. Last values: %v", metricNames, r.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 := 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 := buildMetricsOptions(opts)
for r.waitBackoff.Reset(); r.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
}
r.waitBackoff.Wait()
}
return errors.Errorf("the metric %s is still exported by %s", metricName, r.Name())
}