【问题标题】:Grafana Prometheus CounterGrafana Prometheus 计数器
【发布时间】:2020-07-28 09:36:25
【问题描述】:

我试图在 Grafana 可视化中使用 Prometheus 作为时间序列数据库来获取事件的准确计数。但计数器显示不正确的记录。 2 天的计数比 7 天的计数更高,这肯定表明有问题。

首先,我在这个 promql 查询中使用了单个统计数据可视化:

sum(increase(http_server_requests_seconds_count[$__range])).

附言

我还尝试了以下方法: sum(increase(http_server_requests_seconds_count[1m])) 。 这也会给出错误的计数。

我已经尝试过使用图表并使用图例在表格中显示总计。这也给出了错误的计数。

请让我知道,随着时间范围的变化,显示计数的最佳方式是什么。

我的 json :

{
    "colorMode": "value",
    "fieldOptions": {
        "calcs": [
            "lastNotNull"
        ],
        "defaults": {
            "mappings": [],
            "thresholds": {
                "mode": "absolute",
                "steps": [{
                    "color": "green",
                    "value": null
                }]
            }
        },
        "overrides": [],
        "values": false
    },
    "graphMode": "area",
    "justifyMode": "auto",
    "orientation": "auto"
},
"pluginVersion": "6.6.1",
"targets": [{
    "expr": " sum(increase(http_server_requests_seconds_count[$__range]))",
    "hide": false,
    "instant": true,
    "refId": "A"
}],
"timeFrom": null,
"timeShift": null,
"title": "Total Number of Requests",
"type": "stat"
}

【问题讨论】:

    标签: time-series prometheus grafana metrics promql


    【解决方案1】:

    这对我有用:

    sum(increase(http_request_duration_seconds_count{ecs_cluster=~"$ecs_cluster", instance_id=~"$instance_id"}[$__range]))
    

    激活instant query并将计算设置为last not null

    这里是窗格 JSON:

    {
      "cacheTimeout": null,
      "datasource": "Prometheus",
      "description": "",
      "fieldConfig": {
        "defaults": {
          "custom": {},
          "unit": " requests",
          "decimals": 0,
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "blue",
                "value": null
              }
            ]
          },
          "mappings": [],
          "nullValueMode": "connected"
        },
        "overrides": []
      },
      "gridPos": {
        "h": 2,
        "w": 5,
        "x": 0,
        "y": 4
      },
      "id": 4,
      "interval": null,
      "links": [],
      "maxDataPoints": 100,
      "options": {
        "reduceOptions": {
          "values": false,
          "calcs": [
            "lastNotNull"
          ],
          "fields": ""
        },
        "orientation": "horizontal",
        "textMode": "auto",
        "colorMode": "value",
        "graphMode": "none",
        "justifyMode": "auto",
        "fieldOptions": {
          "calcs": [
            "lastNotNull"
          ]
        }
      },
      "pluginVersion": "7.1.0",
      "targets": [
        {
          "expr": "sum(increase(http_request_duration_seconds_count{ecs_cluster=~\"$ecs_cluster\", instance_id=~\"$instance_id\"}[$__range]))",
          "hide": false,
          "instant": true,
          "interval": "",
          "intervalFactor": 1,
          "legendFormat": "",
          "refId": "A"
        }
      ],
      "timeFrom": null,
      "timeShift": null,
      "title": "",
      "type": "stat"
    }
    

    【讨论】:

    • 这也行不通。在相同范围内进行刷新后获得不同的计数。有时计数很低,有时很高
    • 对照我的检查你的json
    • 非常感谢您的时间和帮助。我已将此升级为 devops,并会在他们解决后在此处发布解决方案。
    • 调查正在进行中。但是我们发现prometheus一切正常。这个问题发生在 Grafana 上而不是 prometheus 上。问题是当本地 grafana 并连接到云中的 prometheus 时,这种情况发生得更多,而不是当 prometheus 和 grafana 都在云上时。当两者都在云中时,我们仍在监控是否会发生这种情况。
    • @jcarlosweb,Prometheus 中的计数器是累积的。它们只会上升(并且不时重置)。仪表不是累积的。它们用于保存当前状态。对于计数器,您必须应用 rate()。我不知道一种简单的方法来获得像您要求的那样的图表。也许在 Grafana 中将步长设置为 1h,将最小间隔设置为 1h
    【解决方案2】:

    由于所选的数据模型,Prometheus 可能会从 increase() 函数返回不准确的结果 - 有关详细信息,请参阅 this issue

    如果您需要准确的结果,则存在以下选项:

    • 使用offset。尝试以下操作:sum(http_server_requests_seconds_count - http_server_requests_seconds_count offset $__range)。请注意,这种方法仅在给定指标 - http_server_requests_seconds_count 未在给定时间范围内重置为 0(又名 counter reset)时才有效。
    • 使用来自 MetricsQL 的 increase() 函数。它返回准确的值 - 有关详细信息,请参阅 these docs

    【讨论】:

    • @user7510999的问题与你链接的问题无关
    • 尝试了偏移量查询。似乎对我不起作用。它提供了长达 1 小时范围内的一些记录。但是对于超过 1 小时的范围,它表示没有数据。返回的计数也不正确。
    • 感谢您的帮助。与其他回复一样,我们正在调查 prometheus 如何废弃我们的指标。将重新检查此查询是否有效
    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    相关资源
    最近更新 更多