Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 366afe7

Browse files
Check .Present before setting label value (#268)
1 parent 1fcdb6a commit 366afe7

2 files changed

Lines changed: 161 additions & 7 deletions

File tree

metrics.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ func metricLabelsToTsLabels(defaults map[string]labelValue, labelKeys []metricda
212212

213213
for i, labelKey := range labelKeys {
214214
labelValue := labelValues[i]
215-
labels[sanitize(labelKey.Key)] = labelValue.Value
215+
if labelValue.Present {
216+
labels[sanitize(labelKey.Key)] = labelValue.Value
217+
}
216218
}
217219

218220
return labels, nil

metrics_test.go

Lines changed: 158 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,12 @@ func TestResourceByDescriptor(t *testing.T) {
606606
},
607607
LabelValues: []metricdata.LabelValue{
608608
{
609-
Value: "v11",
609+
Present: true,
610+
Value: "v11",
610611
},
611612
{
612-
Value: "v12",
613+
Present: true,
614+
Value: "v12",
613615
},
614616
},
615617
},
@@ -650,6 +652,152 @@ func TestResourceByDescriptor(t *testing.T) {
650652
},
651653
},
652654
},
655+
{
656+
in: &metricdata.Metric{
657+
Descriptor: metricdata.Descriptor{
658+
Name: "custom_resource_one",
659+
Description: "This is a test when resource labels are not present",
660+
Unit: metricdata.UnitBytes,
661+
Type: metricdata.TypeCumulativeInt64,
662+
LabelKeys: []metricdata.LabelKey{
663+
{
664+
Key: "k11",
665+
},
666+
{
667+
Key: "k12",
668+
},
669+
},
670+
},
671+
Resource: nil,
672+
TimeSeries: []*metricdata.TimeSeries{
673+
{
674+
StartTime: startTime,
675+
Points: []metricdata.Point{
676+
{
677+
Time: endTime,
678+
Value: int64(5),
679+
},
680+
},
681+
LabelValues: []metricdata.LabelValue{
682+
{
683+
Present: false,
684+
Value: "v11",
685+
},
686+
{
687+
Present: true,
688+
Value: "v12",
689+
},
690+
},
691+
},
692+
},
693+
},
694+
want: []*monitoringpb.CreateTimeSeriesRequest{
695+
{
696+
Name: "projects/foo",
697+
TimeSeries: []*monitoringpb.TimeSeries{
698+
{
699+
Metric: &googlemetricpb.Metric{
700+
Type: "custom.googleapis.com/opencensus/custom_resource_one",
701+
Labels: map[string]string{
702+
"k12": "v12",
703+
},
704+
},
705+
Resource: &monitoredrespb.MonitoredResource{
706+
Type: "one",
707+
Labels: map[string]string{
708+
"k11": "",
709+
},
710+
},
711+
Points: []*monitoringpb.Point{
712+
{
713+
Interval: &monitoringpb.TimeInterval{
714+
StartTime: startTimestamp,
715+
EndTime: endTimestamp,
716+
},
717+
Value: &monitoringpb.TypedValue{
718+
Value: &monitoringpb.TypedValue_Int64Value{
719+
Int64Value: 5,
720+
},
721+
},
722+
},
723+
},
724+
},
725+
},
726+
},
727+
},
728+
},
729+
{
730+
in: &metricdata.Metric{
731+
Descriptor: metricdata.Descriptor{
732+
Name: "custom_resource_one",
733+
Description: "This is a test when metric labels are not present",
734+
Unit: metricdata.UnitBytes,
735+
Type: metricdata.TypeCumulativeInt64,
736+
LabelKeys: []metricdata.LabelKey{
737+
{
738+
Key: "k11",
739+
},
740+
{
741+
Key: "k12",
742+
},
743+
},
744+
},
745+
Resource: nil,
746+
TimeSeries: []*metricdata.TimeSeries{
747+
{
748+
StartTime: startTime,
749+
Points: []metricdata.Point{
750+
{
751+
Time: endTime,
752+
Value: int64(5),
753+
},
754+
},
755+
LabelValues: []metricdata.LabelValue{
756+
{
757+
Present: true,
758+
Value: "v11",
759+
},
760+
{
761+
Present: false,
762+
Value: "v12",
763+
},
764+
},
765+
},
766+
},
767+
},
768+
want: []*monitoringpb.CreateTimeSeriesRequest{
769+
{
770+
Name: "projects/foo",
771+
TimeSeries: []*monitoringpb.TimeSeries{
772+
{
773+
Metric: &googlemetricpb.Metric{
774+
Type: "custom.googleapis.com/opencensus/custom_resource_one",
775+
Labels: map[string]string{},
776+
},
777+
Resource: &monitoredrespb.MonitoredResource{
778+
Type: "one",
779+
Labels: map[string]string{
780+
"k11": "v11",
781+
},
782+
},
783+
Points: []*monitoringpb.Point{
784+
{
785+
Interval: &monitoringpb.TimeInterval{
786+
StartTime: startTimestamp,
787+
EndTime: endTimestamp,
788+
},
789+
Value: &monitoringpb.TypedValue{
790+
Value: &monitoringpb.TypedValue_Int64Value{
791+
Int64Value: 5,
792+
},
793+
},
794+
},
795+
},
796+
},
797+
},
798+
},
799+
},
800+
},
653801
{
654802
in: &metricdata.Metric{
655803
Descriptor: metricdata.Descriptor{
@@ -678,10 +826,12 @@ func TestResourceByDescriptor(t *testing.T) {
678826
},
679827
LabelValues: []metricdata.LabelValue{
680828
{
681-
Value: "v21",
829+
Present: true,
830+
Value: "v21",
682831
},
683832
{
684-
Value: "v22",
833+
Present: true,
834+
Value: "v22",
685835
},
686836
},
687837
},
@@ -750,10 +900,12 @@ func TestResourceByDescriptor(t *testing.T) {
750900
},
751901
LabelValues: []metricdata.LabelValue{
752902
{
753-
Value: "v31",
903+
Present: true,
904+
Value: "v31",
754905
},
755906
{
756-
Value: "v32",
907+
Present: true,
908+
Value: "v32",
757909
},
758910
},
759911
},

0 commit comments

Comments
 (0)