解决Prometheus时区问题,改成本地时间上海的时区

Prometheus默认使用UTC时间,可通过挂载上海时区文件到容器解决,如修改`/etc/localtime`。然而,由于内部统一使用Unix时间,Prometheus代码中进行了UTC转换。官方建议避免时区混淆,推荐在UI或外部程序进行时间本地化处理,如前端JS转换。相关讨论见GitHub issue #500。

官方Prometheus镜像,使用的不是咱们上海的时区,我们可以用上海时区文件创建一个configmap,然后挂载到启动的Prometheus容器里:

kubectl create configmap prometheus-time-zone --from-file=/usr/share/zoneinfo/Asia/Shanghai
然后挂载:

volumeMounts:
- name: prometheus-time-zone
mountPath: /etc/localtime
subPath: Shanghai



然后可以把容器里的时区改成上海时区,东八区


用date 验证时间是本地时间了

但这个只是改了Prometheus容器得本地时区文件而已。centos系统 就是/etc/localtime   Ubuntu是/etc/timezone   

但是Prometheus 是在代码里,统一做得UTC转换

类似这样:

所以机器上(容器里)的时区并不起作用。

至于为什么这么做,官方给了说明:

Can I change the timezone? Why is everything in UTC?

To avoid any kind of timezone confusion, especially when the so-called daylight saving time is involved, we decided to exclusively use Unix time internally and UTC for display purposes in all components of Prometheus. A carefully done timezone selection could be introduced into the UI. Contributions are welcome. See issue #500 for the current state of this effort.

github  讨论:

https://github.com/prometheus/prometheus/issues/500

所以想做时间本地化,就直接在外部程序做好了。

其实官方也是这样做的:

这里的use local time 就是输出后,前端js做了一次转换。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值