Skip to content

Conversation

iamhalje
Copy link
Contributor

This PR extend the ParseDuration function to support negative durations.

negative durations need be useful in Loki alerts templates where time offsets such as '-5m' are common in range calculations and comparisons.

Changes:

  • update ParseDuration to handle negative values like -1h, -30m, etc.
  • preserves compatibility with existing valid duration strings.
  • includes error checks for unit order and duration overflows.

related to promethes/template changes for Loki alerting - see prometheus/prometheus#16669

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Looks good in general. I just have a nit about redundant code comments and overly short variable names.

However, there is also a more general concern: My assumption was this function is just used for templating, but it is used for many other purposes, too. It is not unlikely that those other purposes rely on the return never being negative.

Let's play safe for now and better introduce a new function that allows negative durations (e.g. ParseDurationAllowNegative or something). It will be easy to wire it to parseDuration in the templating.

WDYT?

@iamhalje
Copy link
Contributor Author

iamhalje commented Jun 12, 2025

@beorn7 actually has similar question before, i agree. what would you say now? mean what think about String method and the tests?

@iamhalje iamhalje changed the title feat: Support negative duration in ParseDuration feat: Support negative duration in new function ParseDurationAllowNegative Jun 12, 2025
Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Thanks. The code is correct, but I have a bunch of suggestions to make it more DRY and compact.

@iamhalje
Copy link
Contributor Author

@beorn7 i really like everything, thanks, and i also improved TestParseDuration function in tests

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Thanks. A few more tweaks.

@iamhalje
Copy link
Contributor Author

@beorn7 LGTY?

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

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

Looks great. I'll also tag a new release so that you can use this change in prometheus/prometheus#16619

iamhalje added 10 commits June 23, 2025 00:44
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
Signed-off-by: Dmitry Ponomaryov <iamhalje@gmail.com>
@beorn7 beorn7 force-pushed the add-negative-duration-support branch from 21391c0 to c099408 Compare June 22, 2025 22:44
@beorn7 beorn7 merged commit 75c3814 into prometheus:main Jun 22, 2025
8 checks passed
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.

2 participants