@@ -34,6 +34,7 @@ func checkParameterAbsent(param string, next http.Handler) http.Handler {
34
34
prometheusAPIError (w , fmt .Sprintf ("unexpected error: %v" , err ), http .StatusInternalServerError )
35
35
return
36
36
}
37
+
37
38
if len (kvs [param ]) != 0 {
38
39
prometheusAPIError (w , fmt .Sprintf ("unexpected parameter %q" , param ), http .StatusInternalServerError )
39
40
return
@@ -264,6 +265,7 @@ func TestMatch(t *testing.T) {
264
265
for _ , tc := range []struct {
265
266
labelv []string
266
267
matches []string
268
+ opts []Option
267
269
268
270
expCode int
269
271
expMatch []string
@@ -277,15 +279,15 @@ func TestMatch(t *testing.T) {
277
279
// No "match" parameter.
278
280
labelv : []string {"default" },
279
281
expCode : http .StatusOK ,
280
- expMatch : []string {`{namespace=~ "default"}` },
282
+ expMatch : []string {`{namespace="default"}` },
281
283
expBody : okResponse ,
282
284
},
283
285
{
284
286
// Single "match" parameters.
285
287
labelv : []string {"default" },
286
288
matches : []string {`{job="prometheus",__name__=~"job:.*"}` },
287
289
expCode : http .StatusOK ,
288
- expMatch : []string {`{job="prometheus",__name__=~"job:.*",namespace=~ "default"}` },
290
+ expMatch : []string {`{job="prometheus",__name__=~"job:.*",namespace="default"}` },
289
291
expBody : okResponse ,
290
292
},
291
293
{
@@ -309,15 +311,15 @@ func TestMatch(t *testing.T) {
309
311
labelv : []string {"default" },
310
312
matches : []string {`{job="prometheus",__name__=~"job:.*",namespace="default"}` },
311
313
expCode : http .StatusOK ,
312
- expMatch : []string {`{job="prometheus",__name__=~"job:.*",namespace="default",namespace=~ "default"}` },
314
+ expMatch : []string {`{job="prometheus",__name__=~"job:.*",namespace="default",namespace="default"}` },
313
315
expBody : okResponse ,
314
316
},
315
317
{
316
318
// Many "match" parameters.
317
319
labelv : []string {"default" },
318
320
matches : []string {`{job="prometheus"}` , `{__name__=~"job:.*"}` },
319
321
expCode : http .StatusOK ,
320
- expMatch : []string {`{job="prometheus",namespace=~ "default"}` , `{__name__=~"job:.*",namespace=~ "default"}` },
322
+ expMatch : []string {`{job="prometheus",namespace="default"}` , `{__name__=~"job:.*",namespace="default"}` },
321
323
expBody : okResponse ,
322
324
},
323
325
{
@@ -336,6 +338,42 @@ func TestMatch(t *testing.T) {
336
338
},
337
339
expBody : okResponse ,
338
340
},
341
+ {
342
+ // Many "match" parameters with a single regex value.
343
+ labelv : []string {".+-monitoring" },
344
+ matches : []string {
345
+ `{job="prometheus"}` ,
346
+ `{__name__=~"job:.*"}` ,
347
+ `{namespace="something"}` ,
348
+ },
349
+ opts : []Option {WithRegexMatch ()},
350
+
351
+ expCode : http .StatusOK ,
352
+ expMatch : []string {
353
+ `{job="prometheus",namespace=~".+-monitoring"}` ,
354
+ `{__name__=~"job:.*",namespace=~".+-monitoring"}` ,
355
+ `{namespace="something",namespace=~".+-monitoring"}` ,
356
+ },
357
+ expBody : okResponse ,
358
+ },
359
+ {
360
+ // A single "match" parameter with multiple regex values.
361
+ labelv : []string {"default" , "something" },
362
+ matches : []string {
363
+ `{job="prometheus"}` ,
364
+ },
365
+ opts : []Option {WithRegexMatch ()},
366
+ expCode : http .StatusBadRequest ,
367
+ },
368
+ {
369
+ // A single "match" parameter with a regex value matching the empty string.
370
+ labelv : []string {".*" },
371
+ matches : []string {
372
+ `{job="prometheus"}` ,
373
+ },
374
+ opts : []Option {WithRegexMatch ()},
375
+ expCode : http .StatusBadRequest ,
376
+ },
339
377
} {
340
378
for _ , u := range []string {
341
379
"http://prometheus.example.com/federate" ,
@@ -351,7 +389,12 @@ func TestMatch(t *testing.T) {
351
389
)
352
390
defer m .Close ()
353
391
354
- r , err := NewRoutes (m .url , proxyLabel , HTTPFormEnforcer {ParameterName : proxyLabel }, WithEnabledLabelsAPI ())
392
+ r , err := NewRoutes (
393
+ m .url ,
394
+ proxyLabel ,
395
+ HTTPFormEnforcer {ParameterName : proxyLabel },
396
+ append ([]Option {WithEnabledLabelsAPI ()}, tc .opts ... )... ,
397
+ )
355
398
if err != nil {
356
399
t .Fatalf ("unexpected error: %v" , err )
357
400
}
@@ -382,6 +425,7 @@ func TestMatch(t *testing.T) {
382
425
t .Logf ("%s" , string (body ))
383
426
t .FailNow ()
384
427
}
428
+
385
429
if resp .StatusCode != http .StatusOK {
386
430
return
387
431
}
@@ -411,15 +455,15 @@ func TestMatchWithPost(t *testing.T) {
411
455
// No "match" parameter.
412
456
labelv : []string {"default" },
413
457
expCode : http .StatusOK ,
414
- expMatch : []string {`{namespace=~ "default"}` },
458
+ expMatch : []string {`{namespace="default"}` },
415
459
expBody : okResponse ,
416
460
},
417
461
{
418
462
// Single "match" parameters.
419
463
labelv : []string {"default" },
420
464
matches : []string {`{job="prometheus",__name__=~"job:.*"}` },
421
465
expCode : http .StatusOK ,
422
- expMatch : []string {`{job="prometheus",__name__=~"job:.*",namespace=~ "default"}` },
466
+ expMatch : []string {`{job="prometheus",__name__=~"job:.*",namespace="default"}` },
423
467
expBody : okResponse ,
424
468
},
425
469
{
@@ -443,15 +487,15 @@ func TestMatchWithPost(t *testing.T) {
443
487
labelv : []string {"default" },
444
488
matches : []string {`{job="prometheus",__name__=~"job:.*",namespace="default"}` },
445
489
expCode : http .StatusOK ,
446
- expMatch : []string {`{job="prometheus",__name__=~"job:.*",namespace="default",namespace=~ "default"}` },
490
+ expMatch : []string {`{job="prometheus",__name__=~"job:.*",namespace="default",namespace="default"}` },
447
491
expBody : okResponse ,
448
492
},
449
493
{
450
494
// Many "match" parameters.
451
495
labelv : []string {"default" },
452
496
matches : []string {`{job="prometheus"}` , `{__name__=~"job:.*"}` },
453
497
expCode : http .StatusOK ,
454
- expMatch : []string {`{job="prometheus",namespace=~ "default"}` , `{__name__=~"job:.*",namespace=~ "default"}` },
498
+ expMatch : []string {`{job="prometheus",namespace="default"}` , `{__name__=~"job:.*",namespace="default"}` },
455
499
expBody : okResponse ,
456
500
},
457
501
{
@@ -546,14 +590,14 @@ func TestSeries(t *testing.T) {
546
590
{
547
591
name : `No "match[]" parameter returns 200 with empty body` ,
548
592
labelv : []string {"default" },
549
- expMatch : []string {`{namespace=~ "default"}` },
593
+ expMatch : []string {`{namespace="default"}` },
550
594
expResponse : okResponse ,
551
595
expCode : http .StatusOK ,
552
596
},
553
597
{
554
598
name : `No "match[]" parameter returns 200 with empty body for POSTs` ,
555
599
labelv : []string {"default" },
556
- expMatch : []string {`{namespace=~ "default"}` },
600
+ expMatch : []string {`{namespace="default"}` },
557
601
expResponse : okResponse ,
558
602
expCode : http .StatusOK ,
559
603
},
@@ -562,7 +606,7 @@ func TestSeries(t *testing.T) {
562
606
labelv : []string {"default" },
563
607
promQuery : "up" ,
564
608
expCode : http .StatusOK ,
565
- expMatch : []string {`{__name__="up",namespace=~ "default"}` },
609
+ expMatch : []string {`{__name__="up",namespace="default"}` },
566
610
expResponse : okResponse ,
567
611
},
568
612
{
@@ -586,7 +630,7 @@ func TestSeries(t *testing.T) {
586
630
labelv : []string {"default" },
587
631
promQuery : `up{instance="localhost:9090"}` ,
588
632
expCode : http .StatusOK ,
589
- expMatch : []string {`{instance="localhost:9090",__name__="up",namespace=~ "default"}` },
633
+ expMatch : []string {`{instance="localhost:9090",__name__="up",namespace="default"}` },
590
634
expResponse : okResponse ,
591
635
},
592
636
{
@@ -679,15 +723,15 @@ func TestSeriesWithPost(t *testing.T) {
679
723
name : `No "match[]" parameter returns 200 with empty body` ,
680
724
labelv : []string {"default" },
681
725
method : http .MethodPost ,
682
- expMatch : []string {`{namespace=~ "default"}` },
726
+ expMatch : []string {`{namespace="default"}` },
683
727
expResponse : okResponse ,
684
728
expCode : http .StatusOK ,
685
729
},
686
730
{
687
731
name : `No "match[]" parameter returns 200 with empty body for POSTs` ,
688
732
method : http .MethodPost ,
689
733
labelv : []string {"default" },
690
- expMatch : []string {`{namespace=~ "default"}` },
734
+ expMatch : []string {`{namespace="default"}` },
691
735
expResponse : okResponse ,
692
736
expCode : http .StatusOK ,
693
737
},
@@ -697,7 +741,7 @@ func TestSeriesWithPost(t *testing.T) {
697
741
promQueryBody : "up" ,
698
742
method : http .MethodPost ,
699
743
expCode : http .StatusOK ,
700
- expMatch : []string {`{__name__="up",namespace=~ "default"}` },
744
+ expMatch : []string {`{__name__="up",namespace="default"}` },
701
745
expResponse : okResponse ,
702
746
},
703
747
{
@@ -724,7 +768,7 @@ func TestSeriesWithPost(t *testing.T) {
724
768
promQueryBody : `up{instance="localhost:9090"}` ,
725
769
method : http .MethodPost ,
726
770
expCode : http .StatusOK ,
727
- expMatch : []string {`{instance="localhost:9090",__name__="up",namespace=~ "default"}` },
771
+ expMatch : []string {`{instance="localhost:9090",__name__="up",namespace="default"}` },
728
772
expResponse : okResponse ,
729
773
},
730
774
{
0 commit comments