-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store: Add Time & duration based partitioning
- Loading branch information
Showing
7 changed files
with
315 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/prometheus/common/model" | ||
"github.com/prometheus/prometheus/pkg/timestamp" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
// TimeOrDurationValue is a custom kingping parser for time in RFC3339 | ||
// or duration in Go's duration format, such as "300ms", "-1.5h" or "2h45m". | ||
// Only one will be set. | ||
type TimeOrDurationValue struct { | ||
Time *time.Time | ||
Dur *model.Duration | ||
} | ||
|
||
// Set converts string to TimeOrDurationValue. | ||
func (tdv *TimeOrDurationValue) Set(s string) error { | ||
t, err := time.Parse(time.RFC3339, s) | ||
if err == nil { | ||
tdv.Time = &t | ||
return nil | ||
} | ||
|
||
// error parsing time, let's try duration. | ||
var minus bool | ||
if s[0] == '-' { | ||
minus = true | ||
s = s[1:] | ||
} | ||
dur, err := model.ParseDuration(s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if minus { | ||
dur = dur * -1 | ||
} | ||
tdv.Dur = &dur | ||
return nil | ||
} | ||
|
||
// String returns either tume or duration. | ||
func (tdv *TimeOrDurationValue) String() string { | ||
switch { | ||
case tdv.Time != nil: | ||
return tdv.Time.String() | ||
case tdv.Dur != nil: | ||
return tdv.Dur.String() | ||
} | ||
|
||
return "nil" | ||
} | ||
|
||
// PrometheusTimestamp returns TimeOrDurationValue converted to PrometheusTimestamp | ||
// if duration is set now+duration is converted to Timestamp. | ||
func (tdv *TimeOrDurationValue) PrometheusTimestamp() int64 { | ||
switch { | ||
case tdv.Time != nil: | ||
return timestamp.FromTime(*tdv.Time) | ||
case tdv.Dur != nil: | ||
return timestamp.FromTime(time.Now().Add(time.Duration(*tdv.Dur))) | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
// TimeOrDuration helper for parsing TimeOrDuration with kingpin. | ||
func TimeOrDuration(flags *kingpin.FlagClause) *TimeOrDurationValue { | ||
value := new(TimeOrDurationValue) | ||
flags.SetValue(value) | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package model_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/prometheus/pkg/timestamp" | ||
"github.com/thanos-io/thanos/pkg/model" | ||
"github.com/thanos-io/thanos/pkg/testutil" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
func TestTimeOrDurationValue(t *testing.T) { | ||
cmd := kingpin.New("test", "test") | ||
|
||
minTime := model.TimeOrDuration(cmd.Flag("min-time", "Start of time range limit to serve")) | ||
|
||
maxTime := model.TimeOrDuration(cmd.Flag("max-time", "End of time range limit to serve"). | ||
Default("9999-12-31T23:59:59Z")) | ||
|
||
_, err := cmd.Parse([]string{"--min-time", "10s"}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
testutil.Equals(t, "10s", minTime.String()) | ||
testutil.Equals(t, "9999-12-31 23:59:59 +0000 UTC", maxTime.String()) | ||
|
||
prevTime := timestamp.FromTime(time.Now()) | ||
afterTime := timestamp.FromTime(time.Now().Add(15 * time.Second)) | ||
|
||
testutil.Assert(t, minTime.PrometheusTimestamp() > prevTime, "minTime prometheus timestamp is less than time now.") | ||
testutil.Assert(t, minTime.PrometheusTimestamp() < afterTime, "minTime prometheus timestamp is more than time now + 15s") | ||
|
||
testutil.Assert(t, 253402300799000 == maxTime.PrometheusTimestamp(), "maxTime is not equal to 253402300799000") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.