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

Support 0 for model.Duration. #226

Merged
merged 3 commits into from
Mar 26, 2020

Conversation

cyriltovena
Copy link
Contributor

This allows to pass the string "0" to parse a model.Duration. This is because time.Duration also support this zero value and so it is easier to interchange model.Duration with time.Duration.

time.Duration does parses 0 using this:

	// Special case: if all that is left is "0", this is zero.
	if s == "0" {
		return 0, nil
	}

see https://golang.org/src/time/format.go?s=40541:40587#L1389

Signed-off-by: Cyril Tovena cyril.tovena@gmail.com

This allows to pass the string "0" to parse a model.Duration. This is because `time.Duration` also support this zero value and so it is to interchange model.Duration with time.Duration.

time.Duration does parses 0 using this:

```
	// Special case: if all that is left is "0", this is zero.
	if s == "0" {
		return 0, nil
	}
```

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
@brian-brazil
Copy link
Contributor

We always require a non-zero value when this is used in Prometheus, so I think requiring a unit is reasonable.

@cyriltovena
Copy link
Contributor Author

Ok !

@gouthamve
Copy link
Member

We always require a non-zero value when this is used in Prometheus

This is not true, we can now set the retention duration as 0s. I'd say this is a very obscure message and can be made better with this PR:

➜  prometheus git:(master) ./prometheus --storage.tsdb.retention.time=0 --storage.tsdb.retention.size=1GB 
Error parsing commandline arguments: not a valid duration string: "0"
usage: prometheus [<flags>]

@gouthamve gouthamve reopened this Mar 23, 2020
@brian-brazil
Copy link
Contributor

This is not true, we can now set the retention duration as 0s.

I'd argue that that's not a sane setup, as you'll still have 2 hours in the head.

@gouthamve
Copy link
Member

Nope, this is when I want only size based retention, and not time based. The way to disable time based is to set it to 0, if I'm not wrong?

@brian-brazil
Copy link
Contributor

That's true.

model/time.go Outdated
@@ -186,6 +186,10 @@ var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$")
// ParseDuration parses a string into a time.Duration, assuming that a year
// always has 365d, a week always has 7d, and a day always has 24h.
func ParseDuration(durationStr string) (Duration, error) {
// shortcut if the value is 0
Copy link
Contributor

Choose a reason for hiding this comment

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

This implies that this is a fast path, which it isn't.

Copy link
Member

Choose a reason for hiding this comment

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

Why isn't it a fast path? If I don't have to spin up the regexp machinery, I'd totally call it a fast path.

In different news: Should we accept "-0"? https://golang.org/pkg/time/#ParseDuration does, BTW…

In yet different news, comment should start capitalized and end with a period.

Copy link
Member

Choose a reason for hiding this comment

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

I guess what you might refer to is that "0" would not pass the code below, and thus it's not only fast, but also handling a special case. (In general, explaining your thoughts a tad more detailed might help…)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll improve this comment should we also support "-0" ? You tell me.

Copy link
Member

Choose a reason for hiding this comment

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

Thinking about it: We do not support negative durations in general, so we should not support "-0" either.

@beorn7
Copy link
Member

beorn7 commented Mar 24, 2020

Note: https://golang.org/pkg/time/#ParseDuration also doesn't need a unit iff the value is "0".

@cyriltovena
Copy link
Contributor Author

Note: https://golang.org/pkg/time/#ParseDuration also doesn't need a unit iff the value is "0".

Yeah that's what I was trying to explain in the PR description.

@beorn7
Copy link
Member

beorn7 commented Mar 24, 2020

You did @cyriltovena . I have to apologize for my limited attention span. (Pretending to be a millennial… :o)

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
model/time.go Outdated
@@ -186,7 +186,7 @@ var durationRE = regexp.MustCompile("^([0-9]+)(y|w|d|h|m|s|ms)$")
// ParseDuration parses a string into a time.Duration, assuming that a year
// always has 365d, a week always has 7d, and a day always has 24h.
func ParseDuration(durationStr string) (Duration, error) {
// shortcut if the value is 0
// Special case: if we receive a "0" string we can directly return a zero value without further processing.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit extreme, just "Allow 0 without a unit." would do.

@brian-brazil
Copy link
Contributor

Can you add the DCO?

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
@cyriltovena cyriltovena force-pushed the default-mode-duration branch from fc259e5 to 83be373 Compare March 25, 2020 22:49
@beorn7
Copy link
Member

beorn7 commented Mar 26, 2020

Everyone seems happy now. Merging…

@beorn7 beorn7 merged commit 442ad05 into prometheus:master Mar 26, 2020
alanprot pushed a commit to alanprot/common that referenced this pull request Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants