在grafana中引入prometheus的数据监控

在完成项目后,需要跟踪项目里面的特定数据的使用情况以及服务器的运行情况进行监控,实时了解项目的运行情况,现在的监视数据图如下:

实现的监控json配置如下:

java 复制代码
{
  "__inputs": [
    {
      "name": "DS_PROMETHEUS",
      "label": "Prometheus",
      "description": "",
      "type": "datasource",
      "pluginId": "prometheus",
      "pluginName": "Prometheus"
    }
  ],
  "id": null,
  "uid": "smartcs-overview",
  "title": "智能客服系统监控",
  "tags": ["smartcs"],
  "timezone": "browser",
  "schemaVersion": 39,
  "version": 0,
  "refresh": "15s",
  "time": { "from": "now-30m", "to": "now" },
  "panels": [
    {
      "id": 1,
      "type": "stat",
      "title": "对话请求总数",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 5, "w": 4, "x": 0, "y": 0 },
      "targets": [{ "expr": "sum(smartcs_chat_requests_total)", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "short" }, "overrides": [] }
    },
    {
      "id": 2,
      "type": "stat",
      "title": "对话错误数",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 5, "w": 4, "x": 4, "y": 0 },
      "targets": [{ "expr": "sum(smartcs_chat_errors_total)", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "short", "thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }, { "color": "red", "value": 1 }] } }, "overrides": [] }
    },
    {
      "id": 3,
      "type": "stat",
      "title": "工具调用次数",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 5, "w": 4, "x": 8, "y": 0 },
      "targets": [{ "expr": "sum(smartcs_chat_tool_calls_total)", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "short" }, "overrides": [] }
    },
    {
      "id": 4,
      "type": "timeseries",
      "title": "对话请求速率 (req/s)",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 5, "w": 12, "x": 12, "y": 0 },
      "targets": [{ "expr": "sum(rate(smartcs_chat_duration_seconds_count[1m]))", "legendFormat": "req/s", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] }
    },
    {
      "id": 5,
      "type": "timeseries",
      "title": "全链路平均耗时 (e2e / LLM总 / RAG / 首Token)",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 5 },
      "targets": [
        { "expr": "sum(rate(smartcs_chat_e2e_duration_seconds_sum[5m])) / sum(rate(smartcs_chat_e2e_duration_seconds_count[5m]))", "legendFormat": "e2e", "refId": "A" },
        { "expr": "sum(rate(smartcs_chat_llm_total_duration_seconds_sum[5m])) / sum(rate(smartcs_chat_llm_total_duration_seconds_count[5m]))", "legendFormat": "llm_total", "refId": "B" },
        { "expr": "sum(rate(smartcs_chat_rag_duration_seconds_sum[5m])) / sum(rate(smartcs_chat_rag_duration_seconds_count[5m]))", "legendFormat": "rag", "refId": "C" },
        { "expr": "sum(rate(smartcs_chat_firsttoken_duration_seconds_sum[5m])) / sum(rate(smartcs_chat_firsttoken_duration_seconds_count[5m]))", "legendFormat": "first_token", "refId": "D" }
      ],
      "fieldConfig": { "defaults": { "unit": "s" }, "overrides": [] }
    },
    {
      "id": 6,
      "type": "timeseries",
      "title": "意图分类分布",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 5 },
      "targets": [{ "expr": "sum by (intent) (rate(smartcs_chat_intent_total[5m]))", "legendFormat": "{{intent}}", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] }
    },
    {
      "id": 7,
      "type": "timeseries",
      "title": "知识库缓存命中 (local/redis)",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 13 },
      "targets": [
        { "expr": "sum by (cache, level) (rate(smartcs_cache_hits_total[5m]))", "legendFormat": "hit {{cache}}/{{level}}", "refId": "A" },
        { "expr": "sum by (cache) (rate(smartcs_cache_misses_total[5m]))", "legendFormat": "miss {{cache}}", "refId": "B" }
      ],
      "fieldConfig": { "defaults": { "unit": "ops" }, "overrides": [] }
    },
    {
      "id": 8,
      "type": "timeseries",
      "title": "CPU 使用率 (进程/系统)",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 13 },
      "targets": [
        { "expr": "process_cpu_usage", "legendFormat": "process", "refId": "A" },
        { "expr": "system_cpu_usage", "legendFormat": "system", "refId": "B" }
      ],
      "fieldConfig": { "defaults": { "unit": "percentunit", "max": 1 }, "overrides": [] }
    },
    {
      "id": 9,
      "type": "timeseries",
      "title": "JVM 堆内存",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 21 },
      "targets": [{ "expr": "sum(jvm_memory_used_bytes{area=\"heap\"})", "legendFormat": "heap used", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "bytes" }, "overrides": [] }
    },
    {
      "id": 10,
      "type": "timeseries",
      "title": "HTTP 请求速率 (按URI)",
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 21 },
      "targets": [{ "expr": "sum by (uri) (rate(http_server_requests_seconds_count[5m]))", "legendFormat": "{{uri}}", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "reqps" }, "overrides": [] }
    }
  ]
}

目前可以采集的数据有:

一、首先实现对话请求监控

1.首先我们要确定数据来源是Prometheus,panel选择stat类型,将所有的请求数进行求和操作

2.完成后的效果,panel的大小可以在页面根据实际需要拖动进行放大缩小

二、对话错误数监控

1.这里和对话请求配置一样,不同的地方就是求和的数量不一样

2.实现效果如下:

三、工具调用次数监控

1.这里和对话请求配置一样,不同的地方就是求和的数量不一样

2.实现后的效果如下:

四、对话请求速率(req/s)监控

1.这里的区别注意在于panel的不同和unit单位选择以及参数的设计

2.实现后的效果如下:

五、全链路平均耗时 (e2e / LLM总 / RAG / 首Token)监控

1.这里我们一共监控了4个数值,因此创建了4个query来实现

六、意图分类分布监控

1.这里主要变化在于查询参数的变化

2.效果图如下:

七、知识库缓存命中 (local/redis)

1.针对缓存命中,里面记录了多个不同的cache和level

3.实现的效果如下:

八、CPU 使用率 (进程/系统)监控

1.添加cpu监控

2.实现效果如下:

九、JVM 堆内存监控

1.直接进行jvm加载

2.实现效果如下:

十、HTTP 请求速率 (按URI)监控

1.实现请求监控

2.实现效果如下:

到此完成了总的监控,如果还需要实现其他的监控,可以参考实现以及对应的监控参数

相关推荐
川石课堂软件测试3 天前
涨薪技术|Prometheus之HTTP API中使用PromQL
网络协议·测试工具·jmeter·http·单元测试·postman·prometheus
IT界的老黄牛4 天前
Prometheus TSDB 拆不出来、也存不了一年:4 条外部存储出路 + 容量测算
prometheus·时序数据库·监控·thanos·victoriametrics·remote_write
Ningcode_cloud4 天前
Kubernetes 弹性伸缩实验手册:从集群搭建到 HPA / VPA 自动扩缩容
云原生·k8s·prometheus
qq_452396235 天前
第十二篇:《数据采集:Grafana Alloy、Fluent Bit、Vector 的选型与配置》
java·贪心算法·grafana
2601_962097365 天前
可观测性实战:Prometheus+Grafana+OTel构建监控
grafana·prometheus·微服务架构·可观测性·opentelemetry
Joker-Full-stack6 天前
higress Grafana生产事故复盘:Loki 2.9 WAL 竞态 Bug 导致 5 天审计日志消失
bug·grafana·higress
IT界的老黄牛6 天前
Jenkins 监控一条龙:接入、看板 9964、11 条告警规则直接抄走
jenkins·grafana·prometheus·监控·ci-cd·告警规则
陈皮糖..6 天前
基于 Keepalived 的传统 Web 高可用架构的容器化改造与可观测性升级
运维·docker·性能优化·架构·云计算·prometheus
heimeiyingwang6 天前
【Prometheus·部署篇】存储与容量规划:本地存储、远程存储与长期保存
prometheus
heimeiyingwang6 天前
【Prometheus·可视化篇】Grafana 集成:数据源配置与 Dashboard 设计原则
grafana·prometheus