PromQL搜索一个时刻的查询

huangapple go评论105阅读模式
英文:

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 * 60floor((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

huangapple
  • 本文由 发表于 2023年8月9日 17:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定