-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathpromql_test.go
297 lines (260 loc) · 7.94 KB
/
promql_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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package test
import (
"context"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/prometheus/common/route"
"github.com/prometheus/prometheus/config"
_ "github.com/prometheus/prometheus/discovery/install" // Register service discovery implementations.
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/testutil"
v1 "github.com/prometheus/prometheus/web/api/v1"
"github.com/sirupsen/logrus"
proxyconfig "github.com/jacksontj/promxy/pkg/config"
"github.com/jacksontj/promxy/pkg/proxystorage"
)
func init() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
const rawPSConfig = `
promxy:
http_client:
tls_config:
insecure_skip_verify: true
server_groups:
- static_configs:
- targets:
- localhost:8083
`
const rawPSRemoteReadConfig = `
promxy:
server_groups:
- static_configs:
- targets:
- localhost:8083
remote_read: true
http_client:
tls_config:
insecure_skip_verify: true
`
const rawDoublePSConfig = `
promxy:
server_groups:
- static_configs:
- targets:
- localhost:8083
labels:
az: a
- static_configs:
- targets:
- localhost:8085
labels:
az: b
`
const rawDoublePSConfigRR = `
promxy:
server_groups:
- static_configs:
- targets:
- localhost:8083
labels:
az: a
remote_read: true
- static_configs:
- targets:
- localhost:8085
labels:
az: b
remote_read: true
`
func getProxyStorage(cfg string) *proxystorage.ProxyStorage {
// Create promxy in front of it
pstorageConfig := &proxyconfig.Config{}
if err := yaml.Unmarshal([]byte(cfg), &pstorageConfig); err != nil {
panic(err)
}
ps, err := proxystorage.NewProxyStorage(func(rangeMillis int64) int64 {
return int64(config.DefaultGlobalConfig.EvaluationInterval) / int64(time.Millisecond)
})
if err != nil {
logrus.Fatalf("Error creating proxy: %v", err)
}
if err := ps.ApplyConfig(pstorageConfig); err != nil {
logrus.Fatalf("Unable to apply config: %v", err)
}
return ps
}
func startAPIForTest(s storage.Storage, listen string) (*http.Server, chan struct{}) {
// Start up API server for engine
cfgFunc := func() config.Config { return config.DefaultConfig }
// Return 503 until ready (for us there isn't much startup, so this might not need to be implemented
readyFunc := func(f http.HandlerFunc) http.HandlerFunc { return f }
api := v1.NewAPI(
promql.NewEngine(promql.EngineOpts{
Timeout: 10 * time.Minute,
MaxSamples: 50000000,
NoStepSubqueryIntervalFn: func(int64) int64 { return (1 * time.Minute).Milliseconds() },
EnableAtModifier: true,
}), // Query Engine
s.(storage.SampleAndChunkQueryable), // SampleAndChunkQueryable
nil, //appendable
nil, // exemplarQueryable
nil, //factoryTr
nil, //factoryAr
cfgFunc,
nil, // flags
v1.GlobalURLOptions{
ListenAddress: listen,
Host: "localhost",
Scheme: "http",
}, // global URL options
readyFunc, // ready
nil, // local storage
"", // tsdb dir
false, // enable admin API
nil, // logger
nil, // FactoryRr
50000000, // RemoteReadSampleLimit
1000, // RemoteReadConcurrencyLimit
1048576, // RemoteReadBytesInFrame
false, // isAgent
nil, // CORSOrigin
nil, // runtimeInfo
nil, // buildInfo
nil, // gatherer
nil, // registerer
nil, // statsRenderer
)
apiRouter := route.New()
api.Register(apiRouter.WithPrefix("/api/v1"))
startChan := make(chan struct{})
stopChan := make(chan struct{})
srv := &http.Server{Addr: listen, Handler: apiRouter}
go func() {
defer close(stopChan)
close(startChan)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Println("Error listening to", listen, err)
}
}()
<-startChan
return srv, stopChan
}
func TestUpstreamEvaluations(t *testing.T) {
files, err := filepath.Glob("../vendor/github.com/prometheus/prometheus/promql/testdata/*.test")
if err != nil {
t.Fatal(err)
}
for i, psConfig := range []string{rawPSConfig, rawPSRemoteReadConfig} {
for _, fn := range files {
// Upstream prom is using a StaleNan to determine if a given timeseries has gone
// NaN -- the problem being that for range vectors they filter out all "stale" samples
// meaning that it isn't possible to get a "raw" dump of data through the v1 API
// The only option that exists in reality is the "remote read" API -- which suffers
// from the same memory-balooning problems that the HTTP+JSON API originally had.
// It has **less** of a problem (its 2x memory instead of 14x) so it is a viable option.
// NOTE: Skipped only when promxy isn't configured to use the remote_read API
if psConfig == rawPSConfig && strings.Contains(fn, "staleness.test") {
continue
}
t.Run(strconv.Itoa(i)+fn, func(t *testing.T) {
test, err := newTestFromFile(t, fn)
if err != nil {
t.Errorf("error creating test for %s: %s", fn, err)
}
// Create API for the storage engine
srv, stopChan := startAPIForTest(test.Storage(), ":8083")
ps := getProxyStorage(psConfig)
lStorage := &LayeredStorage{ps, test.Storage()}
// Replace the test storage with the promxy one
test.SetStorage(lStorage)
test.QueryEngine().NodeReplacer = ps.NodeReplacer
err = test.Run()
if err != nil {
t.Errorf("error running test %s: %s", fn, err)
}
test.Close()
// stop server
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
srv.Shutdown(ctx)
<-stopChan
})
}
}
}
func TestEvaluations(t *testing.T) {
files, err := filepath.Glob("testdata/*.test")
if err != nil {
t.Fatal(err)
}
for i, psConfig := range []string{rawDoublePSConfig, rawDoublePSConfigRR} {
for _, fn := range files {
t.Run(strconv.Itoa(i)+fn, func(t *testing.T) {
test, err := newTestFromFile(t, fn)
if err != nil {
t.Errorf("error creating test for %s: %s", fn, err)
}
// Create API for the storage engine
srv, stopChan := startAPIForTest(test.Storage(), ":8083")
srv2, stopChan2 := startAPIForTest(test.Storage(), ":8085")
ps := getProxyStorage(psConfig)
lStorage := &LayeredStorage{ps, test.Storage()}
// Replace the test storage with the promxy one
test.SetStorage(lStorage)
test.QueryEngine().NodeReplacer = ps.NodeReplacer
err = test.Run()
if err != nil {
t.Errorf("error running test %s: %s", fn, err)
}
test.Close()
// stop server
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
srv.Shutdown(ctx)
srv2.Shutdown(ctx)
<-stopChan
<-stopChan2
})
}
}
}
func newTestFromFile(t testutil.T, filename string) (*promql.Test, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return promql.NewTest(t, string(content))
}
// Create a wrapper for the storage that will proxy reads but not writes
type LayeredStorage struct {
proxyStorage storage.Storage
baseStorage storage.Storage
}
func (p *LayeredStorage) Querier(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
return p.proxyStorage.Querier(ctx, mint, maxt)
}
func (p *LayeredStorage) StartTime() (int64, error) {
return p.baseStorage.StartTime()
}
func (p *LayeredStorage) Appender(ctx context.Context) storage.Appender {
return p.baseStorage.Appender(ctx)
}
func (p *LayeredStorage) Close() error {
return p.baseStorage.Close()
}
func (p *LayeredStorage) ChunkQuerier(ctx context.Context, mint, maxt int64) (storage.ChunkQuerier, error) {
return p.baseStorage.ChunkQuerier(ctx, mint, maxt)
}