Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
replace missing tag with empty values during metric export. (#1102)
Browse files Browse the repository at this point in the history
* replace missing tag with empty values during metric export.

* use map.
  • Loading branch information
rghetia committed Apr 25, 2019
1 parent df5e771 commit bf1b28d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
15 changes: 12 additions & 3 deletions stats/view/view_to_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,27 @@ func viewToMetricDescriptor(v *View) *metricdata.Descriptor {
}
}

func toLabelValues(row *Row) []metricdata.LabelValue {
func toLabelValues(row *Row, expectedKeys []string) []metricdata.LabelValue {
labelValues := []metricdata.LabelValue{}
tagMap := make(map[string]string)
for _, tag := range row.Tags {
labelValues = append(labelValues, metricdata.NewLabelValue(tag.Value))
tagMap[tag.Key.Name()] = tag.Value
}

for _, key := range expectedKeys {
if val, ok := tagMap[key]; ok {
labelValues = append(labelValues, metricdata.NewLabelValue(val))
} else {
labelValues = append(labelValues, metricdata.LabelValue{})
}
}
return labelValues
}

func rowToTimeseries(v *viewInternal, row *Row, now time.Time, startTime time.Time) *metricdata.TimeSeries {
return &metricdata.TimeSeries{
Points: []metricdata.Point{row.Data.toPoint(v.metricDescriptor.Type, now)},
LabelValues: toLabelValues(row),
LabelValues: toLabelValues(row, v.metricDescriptor.LabelKeys),
StartTime: startTime,
}
}
Expand Down
65 changes: 50 additions & 15 deletions stats/view/view_to_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ type testToMetrics struct {

var (
// tag objects.
tk1 tag.Key
tk2 tag.Key
tk3 tag.Key
tk1v1 tag.Tag
tk1v2 tag.Tag
tk2v2 tag.Tag
tk3v3 tag.Tag
tags []tag.Tag
labelValues []metricdata.LabelValue
labelKeys []string
tk1 tag.Key
tk2 tag.Key
tk3 tag.Key
tk1v1 tag.Tag
tk2v2 tag.Tag
tags []tag.Tag

labelValues []metricdata.LabelValue
emptyLabelValues []metricdata.LabelValue

labelKeys []string

recordsInt64 []recordValWithTag
recordsFloat64 []recordValWithTag
recordsInt64 []recordValWithTag
recordsFloat64 []recordValWithTag
recordsFloat64WoTag []recordValWithTag

// distribution objects.
aggDist *Aggregation
Expand All @@ -73,6 +75,7 @@ var (
viewTypeInt64Sum *View
viewTypeFloat64LastValue *View
viewTypeInt64LastValue *View
viewRecordWithoutLabel *View
mdTypeFloat64CumulativeDistribution metricdata.Descriptor
mdTypeInt64CumulativeDistribution metricdata.Descriptor
mdTypeInt64CumulativeCount metricdata.Descriptor
Expand All @@ -81,6 +84,7 @@ var (
mdTypeFloat64CumulativeSum metricdata.Descriptor
mdTypeInt64CumulativeLastValue metricdata.Descriptor
mdTypeFloat64CumulativeLastValue metricdata.Descriptor
mdTypeRecordWithoutLabel metricdata.Descriptor
)

const (
Expand All @@ -92,9 +96,9 @@ const (
nameFloat64SumM1 = "viewToMetricTest_Float64_Sum/m1"
nameInt64LastValueM1 = "viewToMetricTest_Int64_LastValue/m1"
nameFloat64LastValueM1 = "viewToMetricTest_Float64_LastValue/m1"
nameRecordWithoutLabel = "viewToMetricTest_RecordWithoutLabel/m1"
v1 = "v1"
v2 = "v2"
v3 = "v3"
)

func init() {
Expand All @@ -110,15 +114,17 @@ func initTags() {
tk2, _ = tag.NewKey("k2")
tk3, _ = tag.NewKey("k3")
tk1v1 = tag.Tag{Key: tk1, Value: v1}
tk1v2 = tag.Tag{Key: tk1, Value: v2}
tk2v2 = tag.Tag{Key: tk2, Value: v2}
tk3v3 = tag.Tag{Key: tk3, Value: v3}

tags = []tag.Tag{tk1v1, tk2v2}
labelValues = []metricdata.LabelValue{
{Value: v1, Present: true},
{Value: v2, Present: true},
}
emptyLabelValues = []metricdata.LabelValue{
{Value: "", Present: false},
{Value: "", Present: false},
}
labelKeys = []string{tk1.Name(), tk2.Name()}

recordsInt64 = []recordValWithTag{
Expand All @@ -129,6 +135,10 @@ func initTags() {
{tags: tags, value: float64(1.5)},
{tags: tags, value: float64(5.4)},
}
recordsFloat64WoTag = []recordValWithTag{
{value: float64(1.5)},
{value: float64(5.4)},
}
}

func initAgg() {
Expand Down Expand Up @@ -189,6 +199,12 @@ func initViews() {
Measure: stats.Float64(nameFloat64LastValueM1, "", stats.UnitDimensionless),
Aggregation: aggL,
}
viewRecordWithoutLabel = &View{
Name: nameRecordWithoutLabel,
TagKeys: []tag.Key{tk1, tk2},
Measure: stats.Float64(nameRecordWithoutLabel, "", stats.UnitDimensionless),
Aggregation: aggL,
}
}

func initMetricDescriptors() {
Expand Down Expand Up @@ -225,6 +241,10 @@ func initMetricDescriptors() {
Name: nameFloat64LastValueM1, Description: "", Unit: metricdata.UnitDimensionless,
Type: metricdata.TypeGaugeFloat64, LabelKeys: labelKeys,
}
mdTypeRecordWithoutLabel = metricdata.Descriptor{
Name: nameRecordWithoutLabel, Description: "", Unit: metricdata.UnitDimensionless,
Type: metricdata.TypeGaugeFloat64, LabelKeys: labelKeys,
}
}

func Test_ViewToMetric(t *testing.T) {
Expand Down Expand Up @@ -375,6 +395,21 @@ func Test_ViewToMetric(t *testing.T) {
},
},
},
{
view: viewRecordWithoutLabel,
recordValue: recordsFloat64WoTag,
wantMetric: &metricdata.Metric{
Descriptor: mdTypeRecordWithoutLabel,
TimeSeries: []*metricdata.TimeSeries{
{Points: []metricdata.Point{
metricdata.NewFloat64Point(now, 5.4),
},
LabelValues: emptyLabelValues,
StartTime: time.Time{},
},
},
},
},
}

wantMetrics := []*metricdata.Metric{}
Expand Down

0 comments on commit bf1b28d

Please sign in to comment.