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

Method to pretty-print duration from seconds or milliseconds #12849

Open
hyperair opened this issue May 2, 2024 · 0 comments
Open

Method to pretty-print duration from seconds or milliseconds #12849

hyperair opened this issue May 2, 2024 · 0 comments
Labels
type/feature Something new we should do

Comments

@hyperair
Copy link

hyperair commented May 2, 2024

Is your feature request related to a problem? Please describe.
I have many log lines with labels like duration=1000 for 1 second, and duration=3600000 for 1 hour. I'd like to have this pretty printed into either a hh:mm:ss kind of format, or even something like 1h 30m 1.123s

Currently the closest I've gotten is to use | label_format duration_pretty={{ if .duration }}{{ .duration | printf "%sms" | duration | printf "%.3f" }}s{{ end }}`

Describe the solution you'd like
I would like a template function that's exactly the reverse of duration_seconds, i.e. I would like it to take a number (of seconds), and convert it into a humanized time duration.

Describe alternatives you've considered
This is the current workaround I've arrived at:

  | label_format duration_h=`{{ if .duration }}{{ div .duration 3600000 }}{{ end }}`, duration_m=`{{ if .duration }}{{ div .duration 60000 }}{{ end }}`, duration_s=`{{ if .duration }}{{ div .duration 1000 }}{{ end }}`
  | label_format duration_pretty=`{{ if .duration }}{{ if .duration_h | int }}{{ .duration_h | int | printf "%d" }}h {{ end }}{{ if mod .duration_m 60 }}{{ mod .duration_m 60 | printf "%d"}}m {{ end }}{{ mod .duration_s 60 | printf "%d"}}.{{ mod .duration 1000 | printf "%03d"}}s{{ end }}`

Additional context
N/A

@JStickler JStickler added the type/feature Something new we should do label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feature Something new we should do
Projects
None yet
Development

No branches or pull requests

2 participants