如何通过PromQL与Grafana将秒数转换为DD-HH-MM-SS格式?
Absolutely! You don’t need external code to get that clean, human-readable uptime format—PromQL and Grafana have all the tools you need. Here’s how to pull it off:
Step 1: Break Down Uptime into Components with PromQL
First, we’ll split the total uptime (in seconds) into days, hours, minutes, and seconds using PromQL functions. We’ll also ensure each component is two digits (with leading zeros for single-digit values) so the final format looks consistent.
Use these PromQL queries for each time component (replace INSTANCE and JOB with your actual values):
- Days:
printf("%02d", floor((time() - process_start_time_seconds{instance="INSTANCE",job="JOB"}) / 86400)) - Hours:
printf("%02d", floor(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"}, 86400) / 3600)) - Minutes:
printf("%02d", floor(mod(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"}, 86400), 3600) / 60)) - Seconds:
printf("%02d", floor(mod(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"}, 86400), 3600) % 60))
Note for Older Prometheus Versions
If you’re running Prometheus < 2.20 (which doesn’t support the printf function), use this workaround to add leading zeros with concat and string:
# Example for hours component if(floor(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"}, 86400) / 3600) < 10, concat("0", string(floor(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"}, 86400) / 3600))), string(floor(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"}, 86400) / 3600)))
Step 2: Assemble the Format in Grafana
Now, we’ll combine these components into the DD-HH-MM-SS format using Grafana’s features. Here are two common ways to do this:
Option 1: Use a Text Panel (Best for Direct Display)
- Add a new Text panel to your dashboard.
- Go to the Queries tab and add the four PromQL queries above (label them
Days,Hours,Minutes,Secondsfor clarity). - Switch to the Text tab and use Grafana’s template syntax to stitch the values together:
**Process Uptime**: [[query_result(Days)]]-[[query_result(Hours)]]-[[query_result(Minutes)]]-[[query_result(Seconds)]] - Save the panel—you’ll see your uptime in the clean DD-HH-MM-SS format.
Option 2: Embed in a Graph Legend
If you want the format to appear in a graph’s legend:
- Open your Graph panel’s Legend settings.
- Set the Legend format to this Go template (which works with Grafana’s templating):
Uptime: {{ printf "%02d" (floor((time() - process_start_time_seconds{instance="INSTANCE",job="JOB"})/86400)) }}-{{ printf "%02d" (floor(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"},86400)/3600)) }}-{{ printf "%02d" (floor(mod(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"},86400),3600)/60)) }}-{{ printf "%02d" (floor(mod(mod(time() - process_start_time_seconds{instance="INSTANCE",job="JOB"},86400),3600)%60) ) }} - Apply the settings—your graph’s legend will now show the formatted uptime.
Key Notes
- Make sure your Prometheus target exposes the
process_start_time_secondsmetric (most official exporters do this by default). - Test each PromQL query individually in Prometheus’s expression browser first to confirm it returns the expected values.
内容的提问来源于stack exchange,提问作者argamanza

