You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PromQL与Grafana将秒数转换为DD-HH-MM-SS格式?

Can I Convert Process Uptime (in Seconds) to DD-HH-MM-SS Format Using Only PromQL and Grafana?

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)

  1. Add a new Text panel to your dashboard.
  2. Go to the Queries tab and add the four PromQL queries above (label them Days, Hours, Minutes, Seconds for clarity).
  3. 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)]]
    
  4. 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:

  1. Open your Graph panel’s Legend settings.
  2. 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) ) }}
    
  3. 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_seconds metric (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 23:42:41