Skip to content

Commit

Permalink
[chore] [exporter/carbon] Update tests to not use Map.Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Jan 10, 2023
1 parent e3d2bd5 commit 8fc2b58
Showing 1 changed file with 57 additions and 42 deletions.
99 changes: 57 additions & 42 deletions exporter/carbonexporter/metricdata_to_plaintext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestBuildPath(t *testing.T) {
}

func TestToPlaintext(t *testing.T) {
expectedTagsStr := ";k0=v0;k1=v1"
expectedTagsCombinations := []string{";k0=v0;k1=v1", ";k1=v1;k0=v0"}

unixSecs := int64(1574092046)
expectedUnixSecsStr := strconv.FormatInt(unixSecs, 10)
Expand All @@ -148,7 +148,7 @@ func TestToPlaintext(t *testing.T) {
tests := []struct {
name string
metricsDataFn func() pmetric.Metrics
wantLines []string
wantLines [][]string
wantNumConvertedTimeseries int
wantNumDroppedTimeseries int
}{
Expand Down Expand Up @@ -179,11 +179,13 @@ func TestToPlaintext(t *testing.T) {
return md

},
wantLines: []string{
"gauge_double_no_dims " + expectedDobuleValStr + " " + expectedUnixSecsStr,
"gauge_int_no_dims " + expectedInt64ValStr + " " + expectedUnixSecsStr,
"cumulative_double_no_dims " + expectedDobuleValStr + " " + expectedUnixSecsStr,
"cumulative_int_no_dims " + expectedInt64ValStr + " " + expectedUnixSecsStr,
wantLines: [][]string{
{
"gauge_double_no_dims " + expectedDobuleValStr + " " + expectedUnixSecsStr,
"gauge_int_no_dims " + expectedInt64ValStr + " " + expectedUnixSecsStr,
"cumulative_double_no_dims " + expectedDobuleValStr + " " + expectedUnixSecsStr,
"cumulative_int_no_dims " + expectedInt64ValStr + " " + expectedUnixSecsStr,
},
},
wantNumConvertedTimeseries: 4,
},
Expand Down Expand Up @@ -221,12 +223,18 @@ func TestToPlaintext(t *testing.T) {
dps4.At(0).SetIntValue(int64Val)
return md
},
wantLines: []string{
"gauge_double_with_dims" + expectedTagsStr + " " + expectedDobuleValStr + " " + expectedUnixSecsStr,
"gauge_int_with_dims" + expectedTagsStr + " " + expectedInt64ValStr + " " + expectedUnixSecsStr,
"cumulative_double_with_dims" + expectedTagsStr + " " + expectedDobuleValStr + " " + expectedUnixSecsStr,
"cumulative_int_with_dims" + expectedTagsStr + " " + expectedInt64ValStr + " " + expectedUnixSecsStr,
},
wantLines: func() [][]string {
combinations := make([][]string, 0, len(expectedTagsCombinations))
for _, tags := range expectedTagsCombinations {
combinations = append(combinations, []string{
"gauge_double_with_dims" + tags + " " + expectedDobuleValStr + " " + expectedUnixSecsStr,
"gauge_int_with_dims" + tags + " " + expectedInt64ValStr + " " + expectedUnixSecsStr,
"cumulative_double_with_dims" + tags + " " + expectedDobuleValStr + " " + expectedUnixSecsStr,
"cumulative_int_with_dims" + tags + " " + expectedInt64ValStr + " " + expectedUnixSecsStr,
})
}
return combinations
}(),
wantNumConvertedTimeseries: 4,
},
{
Expand All @@ -239,15 +247,14 @@ func TestToPlaintext(t *testing.T) {
dp := ms.At(0).SetEmptyHistogram().DataPoints().AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(tsUnix))
assert.NoError(t, dp.Attributes().FromRaw(map[string]interface{}{"k0": "v0", "k1": "v1"}))
dp.Attributes().Sort() // ensures result order
dp.SetCount(distributionCount)
dp.SetSum(distributionSum)
dp.ExplicitBounds().FromRaw(distributionBounds)
dp.BucketCounts().FromRaw(distributionCounts)
return md
},
wantLines: expectedDistributionLines(
"distrib", expectedTagsStr, expectedUnixSecsStr,
"distrib", expectedTagsCombinations, expectedUnixSecsStr,
distributionSum,
distributionCount,
distributionBounds,
Expand All @@ -263,7 +270,6 @@ func TestToPlaintext(t *testing.T) {
dp := ms.At(0).SetEmptySummary().DataPoints().AppendEmpty()
dp.SetTimestamp(pcommon.NewTimestampFromTime(tsUnix))
assert.NoError(t, dp.Attributes().FromRaw(map[string]interface{}{"k0": "v0", "k1": "v1"}))
dp.Attributes().Sort() // ensures result order
dp.SetCount(summaryCount)
dp.SetSum(summarySum)
for i := range summaryQuantiles {
Expand All @@ -274,7 +280,7 @@ func TestToPlaintext(t *testing.T) {
return md
},
wantLines: expectedSummaryLines(
"summary", expectedTagsStr, expectedUnixSecsStr,
"summary", expectedTagsCombinations, expectedUnixSecsStr,
summarySum,
summaryCount,
summaryQuantiles,
Expand All @@ -287,49 +293,58 @@ func TestToPlaintext(t *testing.T) {
gotLines := metricDataToPlaintext(tt.metricsDataFn())
got := strings.Split(gotLines, "\n")
got = got[:len(got)-1]
assert.Equal(t, tt.wantLines, got)
assert.Contains(t, tt.wantLines, got)
})
}
}

func expectedDistributionLines(
metricName, tags, timestampStr string,
metricName string,
tagsCombinations []string,
timestampStr string,
sum float64,
count uint64,
bounds []float64,
counts []uint64,
) []string {
lines := []string{
metricName + ".count" + tags + " " + formatInt64(int64(count)) + " " + timestampStr,
metricName + tags + " " + formatFloatForLabel(sum) + " " + timestampStr,
}

for i, bound := range bounds {
) [][]string {
combinations := make([][]string, 0, len(tagsCombinations))
for _, tags := range tagsCombinations {
lines := []string{
metricName + ".count" + tags + " " + formatInt64(int64(count)) + " " + timestampStr,
metricName + tags + " " + formatFloatForLabel(sum) + " " + timestampStr,
}
for i, bound := range bounds {
lines = append(lines,
metricName+".bucket"+tags+";upper_bound="+formatFloatForLabel(bound)+" "+formatInt64(int64(counts[i]))+" "+timestampStr)
}
lines = append(lines,
metricName+".bucket"+tags+";upper_bound="+formatFloatForLabel(bound)+" "+formatInt64(int64(counts[i]))+" "+timestampStr)
metricName+".bucket"+tags+";upper_bound=inf "+formatInt64(int64(counts[len(bounds)]))+" "+timestampStr)
combinations = append(combinations, lines)
}
lines = append(lines,
metricName+".bucket"+tags+";upper_bound=inf "+formatInt64(int64(counts[len(bounds)]))+" "+timestampStr)

return lines
return combinations
}

func expectedSummaryLines(
metricName, tags, timestampStr string,
metricName string,
tagsCombinations []string,
timestampStr string,
sum float64,
count uint64,
summaryQuantiles []float64,
summaryQuantileValues []float64,
) []string {
lines := []string{
metricName + ".count" + tags + " " + formatInt64(int64(count)) + " " + timestampStr,
metricName + tags + " " + formatFloatForValue(sum) + " " + timestampStr,
}

for i := range summaryQuantiles {
lines = append(lines,
metricName+".quantile"+tags+";quantile="+formatFloatForLabel(summaryQuantiles[i])+" "+formatFloatForValue(summaryQuantileValues[i])+" "+timestampStr)
) [][]string {
combinations := make([][]string, 0, len(tagsCombinations))
for _, tags := range tagsCombinations {
lines := []string{
metricName + ".count" + tags + " " + formatInt64(int64(count)) + " " + timestampStr,
metricName + tags + " " + formatFloatForValue(sum) + " " + timestampStr,
}
for i := range summaryQuantiles {
lines = append(lines,
metricName+".quantile"+tags+";quantile="+formatFloatForLabel(summaryQuantiles[i])+" "+formatFloatForValue(summaryQuantileValues[i])+" "+timestampStr)
}
combinations = append(combinations, lines)
}

return lines
return combinations
}

0 comments on commit 8fc2b58

Please sign in to comment.