Skip to content

Commit 5de762f

Browse files
committed
mtypes: Fix 0 divide crash, making prior test pass
Signed-off-by: Craig Ringer <[email protected]>
1 parent a692c49 commit 5de762f

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

cmd/mtypes/main.go

+26-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"log"
24+
"math"
2425
"net/http"
2526
"net/url"
2627
"os"
@@ -78,7 +79,8 @@ func main() {
7879
if *avalancheFlagsForTotal > 0 {
7980
fmt.Fprintln(os.Stdout)
8081
fmt.Fprintln(os.Stdout, "Avalanche flags for the similar distribution to get to the adjusted series goal of:", *avalancheFlagsForTotal)
81-
flags, adjustedSum := computeAvalancheFlags(*avalancheFlagsForTotal, total, statistics)
82+
seriesCount := 10
83+
flags, adjustedSum := computeAvalancheFlags(*avalancheFlagsForTotal, seriesCount, total, statistics)
8284
for _, f := range flags {
8385
fmt.Fprintln(os.Stdout, f)
8486
}
@@ -96,10 +98,10 @@ func computeTotal(statistics map[dto.MetricType]stats) stats {
9698
return total
9799
}
98100

99-
func computeAvalancheFlags(avalancheFlagsForTotal int, total stats, statistics map[dto.MetricType]stats) ([]string, int) {
101+
func computeAvalancheFlags(avalancheFlagsForTotal, seriesCount int, total stats, statistics map[dto.MetricType]stats) ([]string, int) {
100102
// adjustedGoal is tracking the # of adjusted series we want to generate with avalanche.
101103
adjustedGoal := float64(avalancheFlagsForTotal)
102-
adjustedGoal /= 10.0 // Assuming --series-count=10
104+
adjustedGoal /= float64(seriesCount)
103105
// adjustedSum is tracking the total sum of series so far (at the end hopefully adjustedSum ~= adjustedGoal)
104106
adjustedSum := 0
105107
// Accumulate flags
@@ -113,7 +115,10 @@ func computeAvalancheFlags(avalancheFlagsForTotal int, total stats, statistics m
113115

114116
// adjustedSeriesForType is tracking (per metric type), how many unique series of that
115117
// metric type avalanche needs to create according to the ratio we got from our input.
116-
adjustedSeriesForType := int(adjustedGoal * adjustedSeriesRatio)
118+
var adjustedSeriesForType int
119+
if !math.IsNaN(adjustedSeriesRatio) {
120+
adjustedSeriesForType = int(adjustedGoal * adjustedSeriesRatio)
121+
}
117122

118123
switch mtype {
119124
case dto.MetricType_GAUGE:
@@ -123,27 +128,37 @@ func computeAvalancheFlags(avalancheFlagsForTotal int, total stats, statistics m
123128
flags = append(flags, fmt.Sprintf("--counter-metric-count=%v", adjustedSeriesForType))
124129
adjustedSum += adjustedSeriesForType
125130
case dto.MetricType_HISTOGRAM:
126-
avgBkts := s.buckets / s.series
127-
adjustedSeriesForType /= 2 + avgBkts
131+
var avgBkts int
132+
if s.series > 0 {
133+
avgBkts = s.buckets / s.series
134+
adjustedSeriesForType /= 2 + avgBkts
135+
}
128136
flags = append(flags, fmt.Sprintf("--histogram-metric-count=%v", adjustedSeriesForType))
129-
flags = append(flags, fmt.Sprintf("--histogram-metric-bucket-count=%v", avgBkts-1)) // -1 is due to caveat of additional +Inf not added by avalanche.
137+
if s.series > 0 {
138+
flags = append(flags, fmt.Sprintf("--histogram-metric-bucket-count=%v", avgBkts-1)) // -1 is due to caveat of additional +Inf not added by avalanche.
139+
}
130140
adjustedSum += adjustedSeriesForType * (2 + avgBkts)
131141
case metricType_NATIVE_HISTOGRAM:
132142
flags = append(flags, fmt.Sprintf("--native-histogram-metric-count=%v", adjustedSeriesForType))
133143
adjustedSum += adjustedSeriesForType
134144
case dto.MetricType_SUMMARY:
135-
avgObjs := s.objectives / s.series
136-
adjustedSeriesForType /= 2 + avgObjs
145+
var avgObjs int
146+
if s.series > 0 {
147+
avgObjs = s.objectives / s.series
148+
adjustedSeriesForType /= 2 + avgObjs
149+
}
137150
flags = append(flags, fmt.Sprintf("--summary-metric-count=%v", adjustedSeriesForType))
138-
flags = append(flags, fmt.Sprintf("--summary-metric-objective-count=%v", avgObjs))
151+
if s.series > 0 {
152+
flags = append(flags, fmt.Sprintf("--summary-metric-objective-count=%v", avgObjs))
153+
}
139154
adjustedSum += adjustedSeriesForType * (2 + avgObjs)
140155
default:
141156
if s.series > 0 {
142157
log.Fatalf("not supported %v metric in avalanche", mtype)
143158
}
144159
}
145160
}
146-
flags = append(flags, fmt.Sprintf("--series-count=10"))
161+
flags = append(flags, fmt.Sprintf("--series-count=%v", seriesCount))
147162
// Changes values every 5m.
148163
flags = append(flags, "--value-interval=300")
149164
// 1h series churn.

cmd/mtypes/main_test.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ func TestComputeAvalancheFlags(t *testing.T) {
11001100
for _, tc := range []struct {
11011101
testName string
11021102
avalancheFlagsForTotal int
1103+
seriesCount int
11031104
statistics map[dto.MetricType]stats
11041105
total stats
11051106
expectedSum int
@@ -1108,6 +1109,7 @@ func TestComputeAvalancheFlags(t *testing.T) {
11081109
{
11091110
testName: "samplePromInput",
11101111
avalancheFlagsForTotal: 1000,
1112+
seriesCount: 10,
11111113
statistics: map[dto.MetricType]stats{
11121114
dto.MetricType_COUNTER: {families: 104, series: 166, adjustedSeries: 166},
11131115
dto.MetricType_GAUGE: {families: 77, series: 94, adjustedSeries: 94},
@@ -1135,14 +1137,25 @@ func TestComputeAvalancheFlags(t *testing.T) {
11351137
{
11361138
testName: "noInput",
11371139
avalancheFlagsForTotal: 1000,
1140+
seriesCount: 10,
11381141
statistics: map[dto.MetricType]stats{},
11391142
total: stats{},
1140-
expectedSum: 84,
1141-
expectedFlags: []string{},
1143+
expectedSum: 0,
1144+
expectedFlags: []string{
1145+
"--gauge-metric-count=0",
1146+
"--counter-metric-count=0",
1147+
"--histogram-metric-count=0",
1148+
"--native-histogram-metric-count=0",
1149+
"--summary-metric-count=0",
1150+
"--series-count=10",
1151+
"--value-interval=300",
1152+
"--series-interval=3600",
1153+
"--metric-interval=0",
1154+
},
11421155
},
11431156
} {
11441157
t.Run(tc.testName, func(t *testing.T) {
1145-
avalancheFlags, adjustedSum := computeAvalancheFlags(tc.avalancheFlagsForTotal, tc.total, tc.statistics)
1158+
avalancheFlags, adjustedSum := computeAvalancheFlags(tc.avalancheFlagsForTotal, tc.seriesCount, tc.total, tc.statistics)
11461159
assert.Equal(t, tc.expectedSum, adjustedSum)
11471160
assert.Equal(t, tc.expectedFlags, avalancheFlags)
11481161
})

0 commit comments

Comments
 (0)