Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Cloud Monitoring: Fix bucket bound for distributions #56565

Merged
merged 1 commit into from Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/tsdb/cloudmonitoring/cloudmonitoring.go
Expand Up @@ -596,7 +596,12 @@ func calcBucketBound(bucketOptions cloudMonitoringBucketOptions, n int) string {
case bucketOptions.ExponentialBuckets != nil:
bucketBound = strconv.FormatInt(int64(bucketOptions.ExponentialBuckets.Scale*math.Pow(bucketOptions.ExponentialBuckets.GrowthFactor, float64(n-1))), 10)
case bucketOptions.ExplicitBuckets != nil:
bucketBound = fmt.Sprintf("%g", bucketOptions.ExplicitBuckets.Bounds[n])
if n < len(bucketOptions.ExplicitBuckets.Bounds) {
bucketBound = fmt.Sprintf("%g", bucketOptions.ExplicitBuckets.Bounds[n])
} else {
lastBound := bucketOptions.ExplicitBuckets.Bounds[len(bucketOptions.ExplicitBuckets.Bounds)-1]
bucketBound = fmt.Sprintf("%g+", lastBound)
}
Comment on lines +599 to +604
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we simplify with ternary operator:

Suggested change
if n < len(bucketOptions.ExplicitBuckets.Bounds) {
bucketBound = fmt.Sprintf("%g", bucketOptions.ExplicitBuckets.Bounds[n])
} else {
lastBound := bucketOptions.ExplicitBuckets.Bounds[len(bucketOptions.ExplicitBuckets.Bounds)-1]
bucketBound = fmt.Sprintf("%g+", lastBound)
}
i := n < len(bucketOptions.ExplicitBuckets.Bounds) ? n : len(bucketOptions.ExplicitBuckets.Bounds)-1
bucketBound = fmt.Sprintf("%g+",bucketOptions.ExplicitBuckets.Bounds[i])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no ternary operator in Go :) Also, the + of %g+ is only added for the case in which the index is outside bounds

}
return bucketBound
}
Expand Down
Expand Up @@ -103,7 +103,16 @@
"4",
"7",
"12",
"8"
"8",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
Comment on lines +106 to +115
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not really represent reallity since in BucketCounts if the rest of values are 0, it's just ommited for performance reasons (that's why usually BucketCounts.length is smaller than the ExplicitBounds) but it's helpful to represent the issue in a unit test.

]
}
}
Expand Down Expand Up @@ -198,7 +207,16 @@
"5",
"7",
"11",
"8"
"8",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0",
"0"
]
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/tsdb/cloudmonitoring/time_series_filter_test.go
Expand Up @@ -185,8 +185,8 @@ func TestTimeSeriesFilter(t *testing.T) {
require.NoError(t, err)
frames := res.Frames
require.NoError(t, err)
assert.Equal(t, 33, len(frames))
for i := 0; i < 33; i++ {
assert.Equal(t, 42, len(frames))
for i := 0; i < 42; i++ {
if i == 0 {
assert.Equal(t, "0", frames[i].Fields[1].Name)
}
Expand Down