Skip to content
This repository was archived by the owner on Nov 5, 2021. It is now read-only.

Commit 5a91de8

Browse files
authored
Add additional labels to custom metrics as well. (#657)
Also, update tests to verify that additional labels are being attached. Ref: #654 PiperOrigin-RevId: 393196944
1 parent cecb4db commit 5a91de8

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

probes/external/external.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,13 @@ func (p *Probe) readProbeReplies(done chan struct{}) error {
329329

330330
}
331331

332+
func (p *Probe) withAdditionalLabels(em *metrics.EventMetrics, target string) *metrics.EventMetrics {
333+
for _, al := range p.opts.AdditionalLabels {
334+
em.AddLabel(al.KeyValueForTarget(target))
335+
}
336+
return em
337+
}
338+
332339
func (p *Probe) defaultMetrics(target string, result *result) *metrics.EventMetrics {
333340
em := metrics.NewEventMetrics(time.Now()).
334341
AddMetric("success", metrics.NewInt(result.success)).
@@ -340,15 +347,11 @@ func (p *Probe) defaultMetrics(target string, result *result) *metrics.EventMetr
340347

341348
em.LatencyUnit = p.opts.LatencyUnit
342349

343-
for _, al := range p.opts.AdditionalLabels {
344-
em.AddLabel(al.KeyValueForTarget(target))
345-
}
346-
347350
if p.opts.Validators != nil {
348351
em.AddMetric("validation_failure", result.validationFailure)
349352
}
350353

351-
return em
354+
return p.withAdditionalLabels(em, target)
352355
}
353356

354357
func (p *Probe) labels(ep endpoint.Endpoint) map[string]string {
@@ -440,11 +443,11 @@ func (p *Probe) processProbeResult(ps *probeStatus, result *result) {
440443
if p.c.GetOutputMetricsOptions().GetAggregateInCloudprober() {
441444
result.payloadMetrics = p.payloadParser.AggregatedPayloadMetrics(result.payloadMetrics, ps.payload, ps.target)
442445
p.opts.LogMetrics(result.payloadMetrics)
443-
p.dataChan <- result.payloadMetrics
446+
p.dataChan <- p.withAdditionalLabels(result.payloadMetrics, ps.target)
444447
} else {
445448
for _, em := range p.payloadParser.PayloadMetrics(ps.payload, ps.target) {
446449
p.opts.LogMetrics(em)
447-
p.dataChan <- em
450+
p.dataChan <- p.withAdditionalLabels(em, ps.target)
448451
}
449452
}
450453
}

probes/external/external_test.go

+71-24
Original file line numberDiff line numberDiff line change
@@ -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

689697
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+
}
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
}

probes/options/labels.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ func (al *AdditionalLabel) KeyValueForTarget(targetName string) (key, val string
103103
return al.Key, al.valueForTarget[targetName]
104104
}
105105

106-
func parseAdditionalLabel(alpb *configpb.AdditionalLabel) *AdditionalLabel {
106+
// ParseAdditionalLabel parses an additional label proto message into an
107+
// AdditionalLabel struct.
108+
func ParseAdditionalLabel(alpb *configpb.AdditionalLabel) *AdditionalLabel {
107109
al := &AdditionalLabel{
108110
Key: alpb.GetKey(),
109111
}
@@ -158,7 +160,7 @@ func parseAdditionalLabels(p *configpb.ProbeDef) []*AdditionalLabel {
158160
var aLabels []*AdditionalLabel
159161

160162
for _, pb := range p.GetAdditionalLabel() {
161-
aLabels = append(aLabels, parseAdditionalLabel(pb))
163+
aLabels = append(aLabels, ParseAdditionalLabel(pb))
162164
}
163165

164166
return aLabels

probes/options/labels_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestParseAdditionalLabel(t *testing.T) {
9797

9898
for i, alpb := range configWithAdditionalLabels.GetAdditionalLabel() {
9999
t.Run(alpb.GetKey(), func(t *testing.T) {
100-
al := parseAdditionalLabel(alpb)
100+
al := ParseAdditionalLabel(alpb)
101101
if !reflect.DeepEqual(al, expected[i]) {
102102
t.Errorf("Additional labels not parsed correctly. Got=\n%#v\nWanted=\n%#v", al, expected[i])
103103
}

0 commit comments

Comments
 (0)