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

Commit dd3a274

Browse files
songy23bogdandrutu
authored andcommitted
Fix label matching and add tests (#198)
1 parent b183b1e commit dd3a274

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

metrics_proto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func labelsPerTimeSeries(defaults map[string]labelValue, labelKeys []string, lab
289289

290290
for i, labelKey := range labelKeys {
291291
labelValue := labelValues[i]
292-
if labelValue.GetHasValue() {
292+
if !labelValue.GetHasValue() {
293293
continue
294294
}
295295
labels[labelKey] = labelValue.GetValue()

metrics_proto_test.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
4545
}
4646

4747
tests := []struct {
48+
name string
4849
in *metricspb.Metric
4950
want []*monitoringpb.CreateTimeSeriesRequest
5051
wantErr string
5152
statsExporter *statsExporter
5253
}{
5354
{
55+
name: "Test converting Distribution",
5456
in: &metricspb.Metric{
5557
MetricDescriptor: &metricspb.MetricDescriptor{
5658
Name: "with_metric_descriptor",
@@ -133,6 +135,65 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
133135
},
134136
},
135137
},
138+
{
139+
name: "Test some label keys don't have values",
140+
in: &metricspb.Metric{
141+
MetricDescriptor: &metricspb.MetricDescriptor{
142+
Name: "with_metric_descriptor_2",
143+
Description: "This is a test",
144+
Unit: "By",
145+
LabelKeys: []*metricspb.LabelKey{{Key: "key1"}, {Key: "key2"}, {Key: "key3"}},
146+
},
147+
Timeseries: []*metricspb.TimeSeries{
148+
{
149+
StartTimestamp: startTimestamp,
150+
LabelValues: []*metricspb.LabelValue{{}, {}, {HasValue: true, Value: "val3"}},
151+
Points: []*metricspb.Point{
152+
{
153+
Timestamp: endTimestamp,
154+
Value: &metricspb.Point_DoubleValue{
155+
DoubleValue: 25.0,
156+
},
157+
},
158+
},
159+
},
160+
},
161+
},
162+
statsExporter: &statsExporter{
163+
o: Options{ProjectID: "foo", MapResource: defaultMapResource},
164+
},
165+
want: []*monitoringpb.CreateTimeSeriesRequest{
166+
{
167+
Name: "projects/foo",
168+
TimeSeries: []*monitoringpb.TimeSeries{
169+
{
170+
Metric: &googlemetricpb.Metric{
171+
Type: "custom.googleapis.com/opencensus/with_metric_descriptor_2",
172+
Labels: map[string]string{"key3": "val3"},
173+
},
174+
Resource: &monitoredrespb.MonitoredResource{
175+
Type: "global",
176+
},
177+
MetricKind: googlemetricpb.MetricDescriptor_CUMULATIVE,
178+
ValueType: googlemetricpb.MetricDescriptor_DISTRIBUTION,
179+
Points: []*monitoringpb.Point{
180+
{
181+
Interval: &monitoringpb.TimeInterval{
182+
StartTime: startTimestamp,
183+
EndTime: endTimestamp,
184+
},
185+
Value: &monitoringpb.TypedValue{
186+
Value: &monitoringpb.TypedValue_DoubleValue{
187+
DoubleValue: 25.0,
188+
},
189+
},
190+
},
191+
},
192+
},
193+
},
194+
},
195+
},
196+
},
136197
}
137198

138199
seenResources := make(map[*resourcepb.Resource]*monitoredrespb.MonitoredResource)
@@ -145,12 +206,12 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
145206
allTss, err := protoMetricToTimeSeries(context.Background(), se, se.getResource(nil, tt.in, seenResources), tt.in)
146207
if tt.wantErr != "" {
147208
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
148-
t.Errorf("#%d: unmatched error. Got\n\t%v\nWant\n\t%v", i, err, tt.wantErr)
209+
t.Errorf("#%v: unmatched error. Got\n\t%v\nWant\n\t%v", tt.name, err, tt.wantErr)
149210
}
150211
continue
151212
}
152213
if err != nil {
153-
t.Errorf("#%d: unexpected error: %v", i, err)
214+
t.Errorf("#%v: unexpected error: %v", tt.name, err)
154215
continue
155216
}
156217

0 commit comments

Comments
 (0)