@@ -36,6 +36,7 @@ import (
3636 serverpb "github.com/google/cloudprober/probes/external/proto"
3737 "github.com/google/cloudprober/probes/external/serverutils"
3838 "github.com/google/cloudprober/probes/options"
39+ probeconfigpb "github.com/google/cloudprober/probes/proto"
3940 "github.com/google/cloudprober/targets"
4041 "github.com/google/cloudprober/targets/endpoint"
4142)
@@ -650,9 +651,11 @@ func TestUpdateTargets(t *testing.T) {
650651 }
651652}
652653
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 ) {
654655 t .Helper ()
655656
657+ t .Log (val )
658+
656659 testTarget := "test-target"
657660 if r .success != success {
658661 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
676679 }
677680
678681 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 )
681688 }
689+
682690 for key , val := range expectedLabels {
683691 if em .Label (key ) != val {
684692 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
687695}
688696
689697func 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+ }
693741
742+ for _ , test := range tests {
743+ t .Run (test .desc , func (t * testing.T ) {
694744 p := & Probe {}
695745 opts := options .DefaultOptions ()
696746 opts .ProbeConf = & configpb.ProbeConf {
697747 OutputMetricsOptions : & payloadconfigpb.OutputMetricsOptions {
698- AggregateInCloudprober : proto .Bool (agg ),
748+ AggregateInCloudprober : proto .Bool (test . aggregate ),
699749 },
700750 Command : proto .String ("./testCommand" ),
701751 }
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+ }
702758 err := p .Init ("testprobe" , opts )
703759 if err != nil {
704760 t .Fatal (err )
@@ -710,39 +766,30 @@ func TestProcessProbeResult(t *testing.T) {
710766 latency : metrics .NewFloat (0 ),
711767 }
712768
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-
724769 // First run
725770 p .processProbeResult (& probeStatus {
726771 target : "test-target" ,
727772 success : true ,
728773 latency : time .Millisecond ,
729- payload : fmt . Sprintf ( "%s 14" , payloadMetricName [ agg ]) ,
774+ payload : test . payloads [ 0 ] ,
730775 }, r )
731776
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 )
733779
734780 // Second run
735781 p .processProbeResult (& probeStatus {
736782 target : "test-target" ,
737783 success : true ,
738784 latency : time .Millisecond ,
739- payload : fmt . Sprintf ( "%s 11" , payloadMetricName [ agg ]) ,
785+ payload : test . payloads [ 1 ] ,
740786 }, r )
787+ wantSuccess ++
741788
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 )
744791 } 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 )
746793 }
747794 })
748795 }
0 commit comments