英文:
PromQL searching for an epoch
问题
我们有一个名为activation_time
的指标,它返回一个开始时间的时间戳,例如1689550299110。该指标在应用程序启动时初始化,并始终以相同的值公开。
是否有一种方法可以获取所有指标,其值小于当前时间戳减去2个月或其他时间的值?
我们有一个应用程序,随着时间的推移变得越来越慢,我们希望在应用程序长时间未重启时创建警报。
英文:
We have a metric activation_time
that returns an epoch of a start time. For example, 1689550299110. This metric is initialized on application start and is always exposed with the same value.
Is there a way to get all metrics, values of which is less than current epoch time minus 2 months or something.
We have an application that gets slow over time and we want to create alert when app wasn't restarted in a long time.
答案1
得分: 2
你可以将指标值与函数time()
的结果进行比较。
由于你的指标值似乎包含以毫秒为单位的时间戳(违反了OpenMetrics的建议),你应该将其除以1000转换为秒。
你最终的查询可能类似于 activation_time/1000 < time() - 60 * 24 * 60 * 60
或 floor((activation_time/1000 - time()) / 24 / 60 / 60) > 60
,具体取决于你想在警报中显示什么;唯一的区别是返回结果的值:前者将返回开始的时间戳(以秒为单位),后者将返回开始时间戳与当前时刻之间的差异,以天为单位。
警报规则示例:
- alert: WasNotRestartedLately
expr: floor((activation_time/1000 - time()) / 24 / 60 / 60) > 60
annotations:
summary: 应用已经{{ $value }}天没有重新启动
英文:
You can compare metric value with result of function time()
.
Since value of your metric appears to contain timestamp in milliseconds (against OpenMetrics recommendations) you should divide it by 1000 to convert to seconds.
Your final query might be something like activation_time/1000 < time() - 60 * 24 * 60 * 60
or floor((activation_time/1000 - time()) / 24 / 60 / 60) > 60
depending on what you want to show in your alert; only difference is value of returned result: former will return timestamp of start (in seconds), latter - difference between timestamp of start and current moment, in days.
Example of alert rule:
- alert: WasNotRestartedLately
expr: floor((activation_time/1000 - time()) / 24 / 60 / 60) > 60
annotations:
summary: Application hasn't been restarted for {{ $value }} days
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论