英文:
Can I query by natural week in Grafana with Prometheus?
问题
我想制作一个Grafana面板,以显示每周的CPU使用情况的Prometheus数据。具体来说,当我在这里选择时间范围为最近30天时:
我希望编写一个PromQL来生成一个带有四个柱形图的图表:
- 从最近30天内的第一个星期一到第一个星期日的CPU使用情况
- 从最近30天内的第二个星期一到第二个星期日的CPU使用情况
- 从最近30天内的第三个星期一到第三个星期日的CPU使用情况
- 从最近30天内的第四个星期一到现在的CPU使用情况
用于计算CPU使用情况的指标可以是container_cpu_usage_seconds_total或其他指标。
有人知道如何在Grafana和Prometheus中实现这个吗?
英文:
I want to make a grafana panel to show my cpu usage prometheus data weekly. To be specific, when I select time range here as Last 30 days:
I hope to write a PromQL to generate a bar plot with four bars:
- CPU usage from the 1st Monday to the 1st Sunday in the last 30 days
- CPU usage from the 2nd Monday to the 2nd Sunday in the last 30 days
- CPU usage from the 3rd Monday to the 3rd Sunday in the last 30 days
- CPU usage from the 4th Monday to now
The metric to compute CPU usage can be container_cpu_usage_seconds_total or something else.
Anyone knows can it and how to implement this with Grafana and Prometheus?
答案1
得分: 1
你可以通过结合使用 last_over_time
和时间函数如 day_of_week
来完成这个操作。
你的查询将类似于这样:
last_over_time(
( increase(node_cpu_seconds_total[1w])
and on() (day_of_week() == 1 and hour() == 0 and minute() >= 0 and minute() < 5)
)[1w:5m])
在这里,我们计算了一周内计数器的增加量,过滤掉除星期一的00:00 - 00:05之外的值,然后使用 last_over_time(..[1w])
来展示剩下的一周内的值。
然后,你可以在查询选项中指定最小步长为 1w
,这样你将看到4或5个值。
英文:
You can do this combining last_over_time
and time functions like day_of_week
.
Your query will look something like this:
last_over_time(
( increase(node_cpu_seconds_total[1w])
and on() (day_of_week() == 1 and hour() == 0 and minute()>=0<5)
)[1w:5m])
Here we calculate increase of the counter over the week, filter out values other than 00:00 - 00:05 on Monday, and then stretch what's left overt the week with ``last_over_time(..[1w])`.
Then you can specify min step in Query options as 1w
, so you'll be shown 4 or 5 values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论