Skip to content

Commit

Permalink
utils: use the ParseDuration function of prometheus model (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac authored Oct 28, 2024
1 parent b6443e8 commit 3ea565e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/prometheus/client_golang v1.20.4
github.com/prometheus/common v0.59.1
go.opentelemetry.io/contrib/propagators/b3 v1.28.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0
Expand All @@ -33,7 +34,6 @@ require (
github.com/klauspost/compress v1.17.10 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.29.0 // indirect
Expand Down
9 changes: 5 additions & 4 deletions utils/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/go-viper/mapstructure/v2"
"github.com/google/uuid"
"github.com/prometheus/common/model"
)

var (
Expand Down Expand Up @@ -902,20 +903,20 @@ func DecodeNullableDuration(value any) (*time.Duration, error) {
case *time.Duration:
result = *v
case string:
dur, err := time.ParseDuration(v)
dur, err := model.ParseDuration(v)
if err != nil {
return nil, err
}
result = dur
result = time.Duration(dur)
case *string:
if IsNil(v) {
return nil, nil
}
dur, err := time.ParseDuration(*v)
dur, err := model.ParseDuration(*v)
if err != nil {
return nil, err
}
result = dur
result = time.Duration(dur)
default:
i64, err := DecodeNullableInt[int64](v)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions utils/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ func TestDecodeDuration(t *testing.T) {

invalidStr := "test"
_, err = DecodeDuration(invalidStr)
assert.ErrorContains(t, err, "time: invalid duration \"test\"")
assert.ErrorContains(t, err, "not a valid duration string: \"test\"")

_, err = DecodeDuration(&invalidStr)
assert.ErrorContains(t, err, "time: invalid duration \"test\"")
assert.ErrorContains(t, err, "not a valid duration string: \"test\"")

var nilStr *string
nilValue, err := DecodeNullableDuration(nilStr)
Expand Down

0 comments on commit 3ea565e

Please sign in to comment.