别再死记硬背了!用这5个真实监控场景,彻底搞懂Prometheus聚合查询(by/without实战)
5个真实监控场景实战用Prometheus聚合查询解决运维难题当监控面板上的曲线突然飙升当告警通知接连不断轰炸你的手机作为运维工程师的你是否曾面对海量指标数据却无从下手Prometheus的聚合查询功能就像一把瑞士军刀能帮你从数据迷雾中精准定位问题。本文将带你通过五个真实场景彻底掌握by、without和topk等聚合操作的实战技巧。1. 场景一快速定位K8s中CPU异常的Pod凌晨三点告警铃声刺破夜空——集群CPU使用率超过阈值。但面对上百个运行中的Pod如何快速找到罪魁祸首sum(rate(container_cpu_usage_seconds_total{namespaceproduction}[5m])) by (pod)这条查询会按Pod分组显示每个Pod在过去5分钟内的CPU使用率总和。关键在于by (pod)它告诉Prometheus按pod标签分组计算。结果可能如下podvaluefrontend-abc1230.95payment-service-xyz2.37redis-master0.12实战技巧结合topk(3, ...)快速定位最耗资源的Podtopk(3, sum(rate(container_cpu_usage_seconds_total[5m])) by (pod))排除系统组件干扰sum(rate(container_cpu_usage_seconds_total{namespaceproduction, pod!~kube-system.*}[5m])) by (pod)2. 场景二跨实例汇总微服务请求量你的订单服务部署在10台服务器上现在需要监控整个服务的总请求量而非单个实例。这时without就是你的最佳搭档sum(rate(http_requests_total{serviceorder-service}[5m])) without (instance)without (instance)表示计算时忽略instance标签。这相当于把不同实例的数据汇总。对比两种写法使用bysum(rate(http_requests_total{serviceorder-service}[5m])) by (service, method)使用withoutsum(rate(http_requests_total{serviceorder-service}[5m])) without (instance)提示当需要保留的标签较多时用without更简洁当需要保留的标签较少时用by更直观。3. 场景三找出磁盘即将爆满的主机磁盘空间告警频繁触发但每次都只显示某台主机磁盘使用率超过85%如何主动发现风险主机topk(3, max by (instance, device) ((node_filesystem_size_bytes - node_filesystem_avail_bytes) / node_filesystem_size_bytes * 100))这个查询做了三件事计算每个挂载点的使用率百分比按instance和device分组取最大值筛选出使用率最高的前三个结果进阶用法只监控特定文件系统topk(3, max by (instance, device) ( (node_filesystem_size_bytes{fstype~ext4|xfs} - node_filesystem_avail_bytes) / node_filesystem_size_bytes * 100 ))设置动态阈值告警(node_filesystem_size_bytes - node_filesystem_avail_bytes) / node_filesystem_size_bytes * 100 904. 场景四分析API接口的P99延迟用户反馈系统变慢你需要快速定位是哪个API接口的性能出了问题histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[5m])) by (le, endpoint) )这个查询结合了聚合与分位数计算sum by (le, endpoint)按分桶和端点聚合histogram_quantile计算P99延迟结果解读le0.1表示小于100ms的请求如果P99值接近某个le值说明大部分请求在该范围内优化技巧对比不同环境的延迟histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket{env~prod|staging}[5m])) by (le, endpoint, env) )监控延迟变化趋势max_over_time( histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[5m])) by (le, endpoint) )[1h:] )5. 场景五服务依赖关系拓扑分析微服务架构中一个服务变慢可能影响整个调用链。如何可视化服务间的依赖关系sum(rate(http_client_requests_seconds_count{directionoutbound}[5m])) by (client, server)这个查询可以统计每个客户端服务对服务器服务的调用频率识别异常调用模式如某个客户端突然大量调用某个服务可视化技巧在Grafana中使用Node Graph面板使用label_replace增强可读性sum(rate(http_client_requests_seconds_count{directionoutbound}[5m])) by (client, server) * on (client) group_left label_replace(kube_deployment_labels{label_app!}, client, $1, label_app, (.*))聚合查询性能优化指南当监控规模扩大时不当的聚合查询可能导致Prometheus服务器过载。以下是一些实战经验1. 避免全量数据扫描# 不推荐 - 扫描所有指标 sum(rate(http_requests_total[5m])) # 推荐 - 限定命名空间 sum(rate(http_requests_total{namespaceproduction}[5m]))2. 合理使用记录规则将常用聚合查询预计算groups: - name: http.rules rules: - record: namespace:http_requests:rate5m expr: sum(rate(http_requests_total[5m])) by (namespace)3. 子查询优化技巧# 不推荐 - 高开销子查询 max_over_time(rate(http_requests_total[5m])[1h:1m]) # 推荐 - 使用记录规则预计算 max_over_time(namespace:http_requests:rate5m[1h])4. 监控查询性能# 查询执行时间监控 prometheus_engine_query_duration_seconds{quantile0.9} # 识别慢查询 topk(10, prometheus_engine_query_duration_seconds_sum)