@@ -36,6 +36,7 @@ import (
36
36
serverpb "github.com/google/cloudprober/probes/external/proto"
37
37
"github.com/google/cloudprober/probes/external/serverutils"
38
38
"github.com/google/cloudprober/probes/options"
39
+ probeconfigpb "github.com/google/cloudprober/probes/proto"
39
40
"github.com/google/cloudprober/targets"
40
41
"github.com/google/cloudprober/targets/endpoint"
41
42
)
@@ -650,9 +651,11 @@ func TestUpdateTargets(t *testing.T) {
650
651
}
651
652
}
652
653
653
- func verifyProcessedResult (t * testing.T , p * Probe , r * result , success int64 , name string , val int64 , payloadLabels [][ 2 ]string ) {
654
+ func verifyProcessedResult (t * testing.T , p * Probe , r * result , success int64 , name string , val int64 , extraLabels map [ string ]string ) {
654
655
t .Helper ()
655
656
657
+ t .Log (val )
658
+
656
659
testTarget := "test-target"
657
660
if r .success != success {
658
661
t .Errorf ("r.success=%d, expected=%d" , r .success , success )
@@ -676,9 +679,14 @@ func verifyProcessedResult(t *testing.T, p *Probe, r *result, success int64, nam
676
679
}
677
680
678
681
expectedLabels := map [string ]string {"ptype" : "external" , "probe" : "testprobe" , "dst" : "test-target" }
679
- for _ , kv := range payloadLabels {
680
- expectedLabels [kv [0 ]] = kv [1 ]
682
+ for k , v := range extraLabels {
683
+ expectedLabels [k ] = v
684
+ }
685
+
686
+ if len (em .LabelsKeys ()) != len (expectedLabels ) {
687
+ t .Errorf ("Labels mismatch: got=%v, expected=%v" , em .LabelsKeys (), expectedLabels )
681
688
}
689
+
682
690
for key , val := range expectedLabels {
683
691
if em .Label (key ) != val {
684
692
t .Errorf ("r.payloadMetrics.Label(%s)=%s, expected=%s" , key , r .payloadMetrics .Label (key ), val )
@@ -687,18 +695,66 @@ func verifyProcessedResult(t *testing.T, p *Probe, r *result, success int64, nam
687
695
}
688
696
689
697
func TestProcessProbeResult (t * testing.T ) {
690
- for _ , agg := range []bool {true , false } {
691
-
692
- t .Run (fmt .Sprintf ("With aggregation: %v" , agg ), func (t * testing.T ) {
698
+ tests := []struct {
699
+ desc string
700
+ aggregate bool
701
+ payloads []string
702
+ additionalLabels map [string ]string
703
+ wantValues []int64
704
+ wantExtraLabels map [string ]string
705
+ }{
706
+ {
707
+ desc : "with-aggregation-enabled" ,
708
+ aggregate : true ,
709
+ wantValues : []int64 {14 , 25 },
710
+ payloads : []string {"p-failures 14" , "p-failures 11" },
711
+ },
712
+ {
713
+ desc : "with-aggregation-disabled" ,
714
+ aggregate : false ,
715
+ payloads : []string {
716
+ "p-failures{service=serviceA,db=dbA} 14" ,
717
+ "p-failures{service=serviceA,db=dbA} 11" ,
718
+ },
719
+ wantValues : []int64 {14 , 11 },
720
+ wantExtraLabels : map [string ]string {
721
+ "service" : "serviceA" ,
722
+ "db" : "dbA" ,
723
+ },
724
+ },
725
+ {
726
+ desc : "with-additional-labels" ,
727
+ aggregate : false ,
728
+ payloads : []string {
729
+ "p-failures{service=serviceA,db=dbA} 14" ,
730
+ "p-failures{service=serviceA,db=dbA} 11" ,
731
+ },
732
+ additionalLabels : map [string ]string {"dc" : "xx" },
733
+ wantValues : []int64 {14 , 11 },
734
+ wantExtraLabels : map [string ]string {
735
+ "service" : "serviceA" ,
736
+ "db" : "dbA" ,
737
+ "dc" : "xx" ,
738
+ },
739
+ },
740
+ }
693
741
742
+ for _ , test := range tests {
743
+ t .Run (test .desc , func (t * testing.T ) {
694
744
p := & Probe {}
695
745
opts := options .DefaultOptions ()
696
746
opts .ProbeConf = & configpb.ProbeConf {
697
747
OutputMetricsOptions : & payloadconfigpb.OutputMetricsOptions {
698
- AggregateInCloudprober : proto .Bool (agg ),
748
+ AggregateInCloudprober : proto .Bool (test . aggregate ),
699
749
},
700
750
Command : proto .String ("./testCommand" ),
701
751
}
752
+ for k , v := range test .additionalLabels {
753
+ opts .AdditionalLabels = append (opts .AdditionalLabels , options .ParseAdditionalLabel (& probeconfigpb.AdditionalLabel {
754
+ Key : proto .String (k ),
755
+ Value : proto .String (v ),
756
+ }))
757
+ }
702
758
err := p .Init ("testprobe" , opts )
703
759
if err != nil {
704
760
t .Fatal (err )
@@ -710,39 +766,30 @@ func TestProcessProbeResult(t *testing.T) {
710
766
latency : metrics .NewFloat (0 ),
711
767
}
712
768
713
- payloadMetricName := map [bool ]string {
714
- false : "p-failures{service=serviceA,db=dbA}" ,
715
- true : "p-failures" ,
716
- }
717
- payloadLabels := map [bool ][][2 ]string {
718
- false : [][2 ]string {
719
- [2 ]string {"service" , "serviceA" },
720
- [2 ]string {"db" , "dbA" },
721
- },
722
- }
723
-
724
769
// First run
725
770
p .processProbeResult (& probeStatus {
726
771
target : "test-target" ,
727
772
success : true ,
728
773
latency : time .Millisecond ,
729
- payload : fmt . Sprintf ( "%s 14" , payloadMetricName [ agg ]) ,
774
+ payload : test . payloads [ 0 ] ,
730
775
}, r )
731
776
732
- verifyProcessedResult (t , p , r , 1 , "p-failures" , 14 , payloadLabels [agg ])
777
+ wantSuccess := int64 (1 )
778
+ verifyProcessedResult (t , p , r , wantSuccess , "p-failures" , test .wantValues [0 ], test .wantExtraLabels )
733
779
734
780
// Second run
735
781
p .processProbeResult (& probeStatus {
736
782
target : "test-target" ,
737
783
success : true ,
738
784
latency : time .Millisecond ,
739
- payload : fmt . Sprintf ( "%s 11" , payloadMetricName [ agg ]) ,
785
+ payload : test . payloads [ 1 ] ,
740
786
}, r )
787
+ wantSuccess ++
741
788
742
- if agg {
743
- verifyProcessedResult (t , p , r , 2 , "p-failures" , 25 , payloadLabels [ agg ] )
789
+ if test . aggregate {
790
+ verifyProcessedResult (t , p , r , wantSuccess , "p-failures" , test . wantValues [ 1 ], test . wantExtraLabels )
744
791
} else {
745
- verifyProcessedResult (t , p , r , 2 , "p-failures" , 11 , payloadLabels [ agg ] )
792
+ verifyProcessedResult (t , p , r , wantSuccess , "p-failures" , test . wantValues [ 1 ], test . wantExtraLabels )
746
793
}
747
794
})
748
795
}
0 commit comments