18.4 Prometheus + Grafana 仪表盘设计
对于全栈工程师而言,构建一个可观测的 SEO/GEO 监控系统是保障双引擎优化效果持续性的关键。Prometheus 作为时序数据库,搭配 Grafana 作为可视化面板,能够将分散的日志、API 响应和爬虫行为数据转化为实时、可追溯的仪表盘。本节将指导你从零搭建一套专为搜索优化设计的监控体系。
18.4.1 架构概览
整个监控链路分为四个层次:
- 数据采集层:通过 Exporter 或自定义脚本收集 Nginx 日志、Search Console API、生成式引擎引用数据。
- 存储层:Prometheus Server 拉取或接收推送数据,以时间序列形式存储。
- 可视化层:Grafana 连接 Prometheus 数据源,创建仪表盘与告警规则。
- 告警层:通过 Grafana Alerting 或 Alertmanager 发送通知到邮件、钉钉、飞书。
18.4.2 核心指标定义
在开始配置之前,需要明确监控哪些关键指标。建议分为三类:
18.4.2.1 传统 SEO 指标
| 指标名称 | 类型 | 说明 |
|---|---|---|
crawl_rate_total | Counter | 总爬取请求数(按 User-Agent 区分) |
crawl_errors_total | Counter | 爬取返回 4xx/5xx 错误数 |
indexed_pages_total | Gauge | Search Console 中已索引页面数 |
core_web_vital_lcp | Histogram | LCP 时间分布 |
core_web_vital_cls | Histogram | CLS 分数分布 |
18.4.2.2 GEO 生成引擎指标
| 指标名称 | 类型 | 说明 |
|---|---|---|
geo_reference_total | Counter | 页面在生成式摘要中被引用的次数 |
geo_reference_sentiment | Gauge | 引用情感分数(-1 负面,0 中立,1 正面) |
geo_answer_presence | Gauge | 特定关键词是否出现在生成答案中(0/1) |
geo_bot_traffic_bytes | Counter | AI 机器人(GPTBot、ClaudeBot 等)消耗的带宽 |
18.4.2.3 基础设施指标
| 指标名称 | 类型 | 说明 |
|---|---|---|
http_response_time_seconds | Histogram | 页面响应时间 |
server_cpu_usage | Gauge | CPU 使用率 |
server_memory_usage | Gauge | 内存使用率 |
cdn_cache_hit_ratio | Gauge | CDN 缓存命中率 |
18.4.3 数据采集配置
18.4.3.1 Nginx 日志转 Prometheus 指标
使用 nginx-prometheus-exporter 或自定义 log_format 配合 prometheus 模块。推荐使用 nginx-lua-prometheus 库,在 Nginx 配置中直接暴露指标端点。
# nginx.conf
http {
lua_package_path "/path/to/prometheus.lua";
init_worker_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics");
metric_crawl = prometheus:counter("nginx_crawl_requests_total", "Total crawl requests", {"user_agent", "status"});
metric_response_time = prometheus:histogram("nginx_response_time_seconds", "Response time", {"method", "path"});
}
log_by_lua_block {
local ua = ngx.var.http_user_agent
local status = ngx.var.status
metric_crawl:inc(1, {ua, status})
metric_response_time:observe(tonumber(ngx.var.request_time), {ngx.var.request_method, ngx.var.uri})
}
}
18.4.3.2 Search Console API 导出
编写 Python 脚本,使用 Google API 客户端定期拉取索引覆盖报告和核心网页指标数据,并通过 prometheus_client 库暴露为 HTTP 端点。
from prometheus_client import start_http_server, Gauge
from google.oauth2 import service_account
from googleapiclient.discovery import build
import time
# 定义指标
indexed_pages = Gauge('search_console_indexed_pages', 'Total indexed pages', ['site_url'])
lcp_p75 = Gauge('search_console_lcp_p75', 'LCP 75th percentile', ['site_url'])
def fetch_search_console_data():
credentials = service_account.Credentials.from_service_account_file('key.json')
service = build('webmasters', 'v3', credentials=credentials)
# 拉取数据并更新指标
# ...
if __name__ == '__main__':
start_http_server(8000)
while True:
fetch_search_console_data()
time.sleep(3600)
18.4.3.3 生成引擎引用监控
参考第18.1节的方法,定时查询生成式引擎的答案,将是否包含目标 URL 作为布尔指标推送至 Prometheus 的 Pushgateway。
# 使用 curl 推送指标到 Pushgateway
cat <<EOF | curl --data-binary @- http://pushgateway:9091/metrics/job/geo_monitor/instance/example
# HELP geo_answer_presence Whether target URL appears in generated answer
# TYPE geo_answer_presence gauge
geo_answer_presence{engine="perplexity",keyword="SEO tools"} 1
geo_answer_presence{engine="bing_chat",keyword="SEO tools"} 0
EOF
18.4.4 Grafana 仪表盘设计
18.4.4.1 创建数据源
在 Grafana 中配置 Prometheus 数据源,URL 指向 Prometheus Server 地址(例如 http://prometheus:9090),保存并测试连接。
18.4.4.2 核心仪表盘布局
建议创建三个主要仪表盘:
仪表盘一:SEO 健康度总览
- 面板1:爬虫流量趋势(时间序列图)
- 查询:
sum(rate(nginx_crawl_requests_total{user_agent=~"Googlebot|Bingbot"}[5m])) by (user_agent) - 说明:观察主流搜索引擎爬虫的活跃度变化。
- 查询:
- 面板2:索引覆盖率(Stat 面板)
- 查询:
search_console_indexed_pages - 说明:显示当前已索引页面总数,与前一天对比。
- 查询:
- 面板3:核心网页指标分布(热力图)
- 查询:
histogram_quantile(0.75, sum(rate(core_web_vital_lcp_bucket[5m])) by (le)) - 说明:LCP 的 P75 分布,判断性能瓶颈。
- 查询:
仪表盘二:GEO 引用监控
- 面板1:生成引擎引用次数(时间序列图)
- 查询:
sum(rate(geo_reference_total[1h])) by (engine) - 说明:对比 Perplexity、Bing Chat 等引擎对站点的引用趋势。
- 查询:
- 面板2:关键词答案出现率(表格)
- 查询:
geo_answer_presence - 说明:列出监控的关键词及其在生成答案中的出现状态。
- 查询:
- 面板3:AI 机器人流量占比(饼图)
- 查询:
sum(rate(nginx_crawl_requests_total{user_agent=~"GPTBot|ClaudeBot|Bytespider"}[5m])) - 说明:了解 AI 爬虫消耗的服务器资源比例。
- 查询:
仪表盘三:基础设施与性能
- 面板1:服务器负载(仪表盘)
- 查询:
avg(server_cpu_usage) - 说明:CPU 和内存使用率实时监控。
- 查询:
- 面板2:CDN 缓存效率(时间序列图)
- 查询:
cdn_cache_hit_ratio - 说明:缓存命中率低于 90% 时需检查配置。
- 查询:
- 面板3:响应时间 P99(时间序列图)
- 查询:
histogram_quantile(0.99, sum(rate(http_response_time_seconds_bucket[5m])) by (le)) - 说明:确保长尾请求不拖累整体体验。
- 查询:
18.4.5 告警规则配置
在 Grafana 中为关键指标设置告警:
- 爬虫错误率过高:当
rate(nginx_crawl_requests_total{status=~"5.."}[5m]) > 0.05时触发。 - 索引页面骤降:当
search_console_indexed_pages在 1 小时内下降超过 10% 时触发。 - 生成引擎引用消失:当
geo_answer_presence从 1 变为 0 且持续 2 小时时触发。 - LCP 超标:当
histogram_quantile(0.75, ...)超过 2.5 秒时触发。
告警通知渠道建议配置为飞书 Webhook,以便工程师第一时间响应。
18.4.6 进阶:自定义 Exporter
如果标准 Exporter 无法满足需求,可以开发自定义 Exporter。例如,编写一个 Go 语言程序,定期调用 DeepSeek API 并暴露指标:
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
deepSeekReference = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "deepseek_reference_presence",
Help: "Whether site appears in DeepSeek answer",
},
[]string{"keyword"},
)
)
func queryDeepSeek() {
// 调用 DeepSeek API,检查是否包含目标 URL
// 更新 deepSeekReference 指标
}
func main() {
prometheus.MustRegister(deepSeekReference)
http.Handle("/metrics", promhttp.Handler())
go func() {
for {
queryDeepSeek()
time.Sleep(30 * time.Minute)
}
}()
http.ListenAndServe(":8080", nil)
}
18.4.7 维护与优化
- 数据保留策略:Prometheus 默认保留 15 天数据,对于长期趋势分析,建议配置远程存储(如 Thanos 或 VictoriaMetrics)。
- 仪表盘版本控制:将 Grafana 仪表盘的 JSON 模型导出并存入 Git 仓库,便于团队协作和回滚。
- 定期复盘:每周检查仪表盘中的异常模式,调整告警阈值,避免告警疲劳。
通过 Prometheus + Grafana 的组合,你可以将 SEO/GEO 优化从“黑盒猜测”转变为“数据驱动”的工程实践,确保每一次优化都有据可查、有警可告。
