Skip to content

Commit

Permalink
Improve resource.Quantity to float conversion for SLOs (#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
levan-m committed Jul 25, 2024
1 parent 7b3a6ed commit 3c44b31
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions controllers/datadogslo/slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"net/url"
"strconv"

datadogapi "github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
Expand Down Expand Up @@ -73,12 +74,13 @@ func buildThreshold(sloSpec v1alpha1.DatadogSLOSpec) []datadogV1.SLOThreshold {

var warningThreshold *float64
if sloSpec.WarningThreshold != nil {
approxFloat := sloSpec.WarningThreshold.AsApproximateFloat64()
warningThreshold = &approxFloat
convertedFloat, _ := strconv.ParseFloat(sloSpec.WarningThreshold.AsDec().String(), 64)
warningThreshold = &convertedFloat
}

convertedFloat, _ := strconv.ParseFloat(sloSpec.TargetThreshold.AsDec().String(), 64)
threshold := datadogV1.SLOThreshold{
Target: sloSpec.TargetThreshold.AsApproximateFloat64(),
Target: convertedFloat,
Timeframe: *timeframe,
Warning: warningThreshold,
}
Expand Down
12 changes: 6 additions & 6 deletions controllers/datadogslo/slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func Test_buildThreshold(t *testing.T) {
mockSpec: v1alpha1.DatadogSLOSpec{
Name: "test",
Timeframe: "7d",
TargetThreshold: resource.MustParse("99.9"),
TargetThreshold: resource.MustParse("99990m"),
},
expectedResult: []datadogV1.SLOThreshold{
{
Target: 99.9,
Target: 99.99,
Timeframe: datadogV1.SLOTimeframe("7d"),
},
},
Expand All @@ -41,14 +41,14 @@ func Test_buildThreshold(t *testing.T) {
mockSpec: v1alpha1.DatadogSLOSpec{
Name: "test",
Timeframe: "30d",
TargetThreshold: resource.MustParse("99.9"),
WarningThreshold: ptrResourceQuantity(resource.MustParse("95.9")),
TargetThreshold: resource.MustParse("99.999"),
WarningThreshold: ptrResourceQuantity(resource.MustParse("95.010001")),
},
expectedResult: []datadogV1.SLOThreshold{
{
Target: 99.9,
Target: 99.999,
Timeframe: datadogV1.SLOTimeframe("30d"),
Warning: float64Ptr(95.9),
Warning: float64Ptr(95.010001),
},
},
},
Expand Down

0 comments on commit 3c44b31

Please sign in to comment.