Skip to content

Neo4j Prometheus 指标映射

集成配置

启用 Prometheus 指标导出

在 Neo4j 4.x 及以上版本中,Prometheus 指标导出默认启用。可以通过以下配置进行调整:

txt
# 启用 Prometheus 指标导出
dbms.metrics.prometheus.enabled=true

# 设置 Prometheus 指标导出端口
dbms.metrics.prometheus.port=2004

# 设置监听地址
dbms.metrics.prometheus.host=0.0.0.0

# 配置指标过滤器,仅导出特定指标
dbms.metrics.filter=neo4j.*

Prometheus 配置

在 Prometheus 配置文件中添加 Neo4j 目标:

yaml
scrape_configs:
  - job_name: 'neo4j'
    static_configs:
      - targets: ['neo4j-server:2004']
    scrape_interval: 15s
    scrape_timeout: 10s

验证指标导出

使用 curl 命令验证 Prometheus 指标是否正常导出:

bash
curl http://neo4j-server:2004/metrics

核心指标映射

数据库核心指标

Neo4j 指标Prometheus 指标描述标签
dbms.neo4j.versionneo4j_versionNeo4j 版本version, edition
dbms.modeneo4j_mode数据库运行模式mode
dbms.neo4j.start_timeneo4j_start_time_seconds数据库启动时间
dbms.transaction.active_readneo4j_transaction_active_total活跃读事务数type="read"
dbms.transaction.active_writeneo4j_transaction_active_total活跃写事务数type="write"
dbms.transaction.committed_readneo4j_transaction_committed_total已提交读事务数type="read"
dbms.transaction.committed_writeneo4j_transaction_committed_total已提交写事务数type="write"
dbms.transaction.rollbacks_readneo4j_transaction_rollbacks_total已回滚读事务数type="read"
dbms.transaction.rollbacks_writeneo4j_transaction_rollbacks_total已回滚写事务数type="write"

查询执行指标

Neo4j 指标Prometheus 指标描述标签
dbms.query.activeneo4j_query_active_total活跃查询数
dbms.query.execution_time_medianneo4j_query_execution_time_seconds查询执行时间中位数
dbms.query.execution_time_95th_percentileneo4j_query_execution_time_seconds95% 查询执行时间
dbms.query.execution_time_99th_percentileneo4j_query_execution_time_seconds99% 查询执行时间
dbms.query.rejectedneo4j_query_rejected_total被拒绝的查询数

存储指标

Neo4j 指标Prometheus 指标描述标签
store.sizeneo4j_store_size_bytes存储大小
store.nodesneo4j_store_nodes_total节点数量
store.relationshipsneo4j_store_relationships_total关系数量
store.propertiesneo4j_store_properties_total属性数量
transaction_logs.total_sizeneo4j_transaction_logs_size_bytes事务日志总大小
page_cache.hit_rationeo4j_page_cache_hit_ratio页面缓存命中率
page_cache.missesneo4j_page_cache_misses_total页面缓存未命中数
page_cache.hitsneo4j_page_cache_hits_total页面缓存命中数

JVM 指标

Neo4j 指标Prometheus 指标描述标签
jvm.memory.heap.usedjvm_memory_used_bytesJVM 堆内存使用量area="heap"
jvm.memory.heap.committedjvm_memory_committed_bytesJVM 堆内存提交量area="heap"
jvm.memory.heap.maxjvm_memory_max_bytesJVM 堆内存最大值area="heap"
jvm.memory.nonheap.usedjvm_memory_used_bytesJVM 非堆内存使用量area="nonheap"
jvm.gc.pausejvm_gc_pause_secondsGC 暂停时间action, cause
jvm.gc.collection_countjvm_gc_collection_count_totalGC 收集次数collector
jvm.gc.collection_timejvm_gc_collection_seconds_totalGC 收集总时间collector
jvm.threads.countjvm_threads_countJVM 线程数state

网络指标

Neo4j 指标Prometheus 指标描述标签
network.connections.activeneo4j_network_connections_active_total活跃网络连接数protocol
network.connections.createdneo4j_network_connections_created_total已创建网络连接数protocol
network.connections.closedneo4j_network_connections_closed_total已关闭网络连接数protocol
network.received_bytesneo4j_network_bytes_total接收字节数direction="received"
network.sent_bytesneo4j_network_bytes_total发送字节数direction="sent"

集群指标

Neo4j 指标Prometheus 指标描述标签
causal_clustering.members.healthyneo4j_causal_clustering_members_healthy_total健康集群成员数role
causal_clustering.members.totalneo4j_causal_clustering_members_total集群成员总数
causal_clustering.replication.lagneo4j_causal_clustering_replication_lag_seconds复制延迟
causal_clustering.leader_electionsneo4j_causal_clustering_leader_elections_total领导选举次数

指标查询示例

数据库状态查询

txt
# 检查 Neo4j 版本和运行模式
neo4j_version
neo4j_mode

# 数据库启动时间(转换为可读格式)
time() - neo4j_start_time_seconds

事务性能查询

txt
# 活跃事务数
sum(neo4j_transaction_active_total)

# 每秒提交的写事务数
rate(neo4j_transaction_committed_total{type="write"}[5m])

# 事务回滚率
rate(neo4j_transaction_rollbacks_total[5m]) / rate(neo4j_transaction_committed_total[5m])

存储性能查询

txt
# 页面缓存命中率
neo4j_page_cache_hit_ratio

# 页面缓存未命中率
1 - neo4j_page_cache_hit_ratio

# 存储大小增长趋势
rate(neo4j_store_size_bytes[24h])

JVM 性能查询

txt
# JVM 堆内存使用率
(neo4j_jvm_memory_used_bytes{area="heap"} / neo4j_jvm_memory_max_bytes{area="heap"}) * 100

# GC 暂停时间总和
rate(jvm_gc_pause_seconds_sum[5m])

# GC 暂停频率
rate(jvm_gc_collection_count_total[5m])

查询性能查询

txt
# 活跃查询数
sum(neo4j_query_active_total)

# 慢查询率(执行时间超过 1 秒的查询)
rate(neo4j_query_slow_total[5m]) / rate(neo4j_query_committed_total[5m])

# 查询执行时间趋势
rate(neo4j_query_execution_time_seconds{quantile="0.95"}[5m])

Grafana 仪表盘配置

导入官方仪表盘

Neo4j 提供了官方 Grafana 仪表盘,可以从 Grafana 仪表盘库 导入:

  1. 打开 Grafana 界面,点击左侧菜单的 "+" 按钮,选择 "Import"
  2. 输入仪表盘 ID:6753(Neo4j Overview)或 6754(Neo4j Cluster Overview)
  3. 选择 Prometheus 数据源
  4. 点击 "Import" 完成导入

自定义仪表盘

创建自定义 Grafana 仪表盘时,建议包含以下面板:

  1. 数据库概览:显示数据库版本、运行模式、启动时间等
  2. 事务监控:显示活跃事务数、每秒事务数、事务回滚率等
  3. 查询性能:显示活跃查询数、查询执行时间、慢查询率等
  4. 存储使用:显示存储大小、页面缓存命中率、事务日志大小等
  5. JVM 监控:显示堆内存使用率、GC 暂停时间、线程数等
  6. 网络监控:显示网络连接数、吞吐量等
  7. 集群状态:显示集群成员数、复制延迟、领导选举次数等

告警配置

在 Grafana 中配置告警规则,例如:

  • 当 JVM 堆内存使用率超过 85% 时触发警告
  • 当页面缓存命中率低于 90% 时触发警告
  • 当活跃查询数超过 100 时触发警告
  • 当复制延迟超过 5 秒时触发警告

最佳实践

指标采集频率

  • 对于生产环境,建议采集频率设置为 15-30 秒
  • 对于开发环境,可以降低采集频率以减少资源消耗

指标存储策略

  • 根据监控需求配置合适的指标保留时间
  • 使用 Prometheus 联邦集群处理大规模监控数据
  • 考虑使用 Thanos 或 Cortex 进行长期指标存储

指标查询优化

  • 避免使用高基数标签,减少指标 cardinality
  • 使用 PromQL 聚合函数减少返回数据量
  • 为常用查询创建 Prometheus 记录规则

告警策略

  • 定义清晰的告警级别(警告、错误、紧急)
  • 设置合理的告警阈值,避免误报
  • 配置告警通知渠道(邮件、Slack、PagerDuty 等)
  • 定期审查和调整告警规则

常见问题(FAQ)

Q1: 如何启用特定的 Neo4j 指标导出?

A1: 使用 dbms.metrics.filter 配置项过滤要导出的指标。例如,仅导出存储相关指标:

txt
dbms.metrics.filter=store.*,page_cache.*

Q2: Neo4j 4.x 和 5.x 的 Prometheus 指标映射有什么区别?

A2: Neo4j 5.x 对 Prometheus 指标进行了优化,主要区别包括:

  • 指标名称更加符合 Prometheus 命名规范
  • 增加了更多细粒度的标签
  • 调整了部分指标的计算方式
  • 新增了一些 5.x 特有的指标

Q3: 如何监控多个 Neo4j 实例?

A3: 在 Prometheus 配置文件中添加多个目标,并使用标签区分不同实例:

yaml
scrape_configs:
  - job_name: 'neo4j'
    static_configs:
      - targets: ['neo4j-server-1:2004']
        labels:
          instance: 'neo4j-1'
      - targets: ['neo4j-server-2:2004']
        labels:
          instance: 'neo4j-2'

Q4: Prometheus 指标导出会影响 Neo4j 性能吗?

A4: 指标导出对性能的影响很小,但在高负载环境下,建议:

  • 调整采集频率,降低采集频率
  • 仅导出必要的指标
  • 监控指标导出本身的性能开销

Q5: 如何调试 Prometheus 指标导出问题?

A5: 调试方法包括:

  • 检查 Neo4j 日志中的指标相关信息
  • 使用 curl 命令直接访问指标端点
  • 检查 Prometheus 日志中的抓取错误
  • 确保防火墙允许 Prometheus 访问 Neo4j 指标端口

Q6: 如何自定义 Prometheus 指标名称?

A6: Neo4j 不支持直接自定义指标名称,但可以在 Prometheus 中使用记录规则重命名指标:

yaml
groups:
- name: neo4j.rules
  rules:
  - record: custom_neo4j_transaction_count
    expr: sum(neo4j_transaction_active_total) by (instance)

Q7: 如何监控 Neo4j 企业版特有的指标?

A7: 确保使用 Neo4j 企业版,并在指标过滤器中包含企业版特有的指标前缀,如 causal_clustering.*enterprise.* 等。

Q8: 如何导出 Neo4j 慢查询指标到 Prometheus?

A8: 配置慢查询日志,并使用 Prometheus 日志抓取工具(如 Promtail + Loki)分析慢查询日志,或将慢查询指标通过自定义 exporter 导出。

Q9: 如何监控 Neo4j 备份和恢复操作?

A9: 备份和恢复操作的指标可以通过以下方式监控:

  • 监控备份过程中的 CPU、内存和磁盘使用率
  • 检查备份日志中的关键指标
  • 使用自定义脚本将备份状态导出到 Prometheus

Q10: 如何使用 Prometheus 监控 Neo4j 集群的健康状态?

A10: 监控集群健康状态的关键指标包括:

  • 健康集群成员数(neo4j_causal_clustering_members_healthy_total
  • 复制延迟(neo4j_causal_clustering_replication_lag_seconds
  • 领导选举次数(neo4j_causal_clustering_leader_elections_total
  • 集群成员状态变化

可以使用这些指标创建 Grafana 仪表盘和告警规则,监控集群健康状态。