Skip to content

Commit

Permalink
load-generator: Avoid crashing on empty duration string
Browse files Browse the repository at this point in the history
Instant queries typically have empty start and end times.

Also use Prometheus ParseDuration to give more flexibility.

Signed-off-by: Bryan Boreham <[email protected]>
  • Loading branch information
bboreham committed Sep 20, 2024
1 parent 183a6ef commit c22e2ad
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions tools/load-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/model"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -184,12 +185,14 @@ func (q *Querier) query(expr string) {
}

func durationSeconds(s string) time.Duration {
num := s[:len(s)-1]
value, err := time.ParseDuration(num + string(s[len(s)-1]))
if s == "" {
return 0
}
value, err := model.ParseDuration(s)
if err != nil {
log.Fatalf("Invalid duration: %s", s)
log.Fatalf("%s", err.Error())
}
return value
return time.Duration(value)
}

func main() {
Expand Down

0 comments on commit c22e2ad

Please sign in to comment.