v0.3.0 新功能

监控与可观测性

全面的监控能力,包括 Prometheus 指标、查询追踪和系统健康端点。 全面洞察 MynaDB 的性能、资源使用和运维健康状况,确保您的时序数据基础设施平稳运行。

📈 完整的可观测性栈

MynaDB 暴露 Prometheus 兼容指标、结构化健康端点、慢查询追踪和自报告系统指标。配合 Grafana 即可获得完整的监控和可视化方案。

Prometheus 指标

MynaDB 暴露一个 /metrics 端点,使用 Prometheus 导出格式。 抓取此端点可收集关于写入、查询、缓存性能、存储和后台操作的详细指标。

端点

bash
# Fetch Prometheus metrics
curl http://localhost:7000/metrics

Prometheus 抓取配置

在 prometheus.yml 中将 MynaDB 添加为抓取目标:

yaml
scrape_configs:
  - job_name: 'mynadb'
    scrape_interval: 15s
    static_configs:
      - targets: ['localhost:7000']
        labels:
          instance: 'mynadb-prod-01'

  # For a cluster deployment, add all nodes
  - job_name: 'mynadb-cluster'
    scrape_interval: 15s
    static_configs:
      - targets:
          - 'node1:7000'
          - 'node2:7000'
          - 'node3:7000'

关键指标

指标 类型 说明
mynadb_writes_total Counter 接收的写入请求总数
mynadb_write_bytes_total Counter 写入的总字节数(Line Protocol 载荷大小)
mynadb_queries_total Counter 执行的 SQL 查询总数
mynadb_query_duration_seconds Histogram 查询执行耗时(秒)(桶:0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10)
mynadb_cache_hits_total Counter 缓存命中总数(元数据缓存 + 查询缓存)
mynadb_cache_misses_total Counter 缓存未命中总数
mynadb_storage_bytes Gauge 所有 catalog 当前总存储使用量(字节)
mynadb_compaction_runs_total Counter 已完成的压缩运行总数
mynadb_active_connections Gauge 当前活跃的客户端连接数(HTTP、Flight、MySQL)
mynadb_flush_duration_seconds Histogram 缓存刷写到 Parquet 的操作耗时(秒)

指标输出示例

text
# HELP mynadb_writes_total Total number of write requests
# TYPE mynadb_writes_total counter
mynadb_writes_total{catalog="mydb"} 158432

# HELP mynadb_query_duration_seconds Query execution duration
# TYPE mynadb_query_duration_seconds histogram
mynadb_query_duration_seconds_bucket{le="0.01"} 12045
mynadb_query_duration_seconds_bucket{le="0.05"} 34210
mynadb_query_duration_seconds_bucket{le="0.1"} 45123
mynadb_query_duration_seconds_bucket{le="0.25"} 48900
mynadb_query_duration_seconds_bucket{le="0.5"} 49800
mynadb_query_duration_seconds_bucket{le="1"} 49950
mynadb_query_duration_seconds_bucket{le="+Inf"} 50000
mynadb_query_duration_seconds_sum 1523.45
mynadb_query_duration_seconds_count 50000

# HELP mynadb_storage_bytes Current storage usage in bytes
# TYPE mynadb_storage_bytes gauge
mynadb_storage_bytes{catalog="mydb",schema="default"} 10737418240

健康检查端点

MynaDB 提供多个 HTTP 端点用于健康检查、负载均衡器集成和运维可视化。这些端点返回 JSON 响应,无需身份认证。

端点 方法 说明
GET /health GET 基本健康检查。服务器运行时返回 200 OK。用于负载均衡器探测。
GET /stats GET 写缓存统计:当前大小、条目数、刷写状态和缓冲区利用率。
GET /memory GET 内存使用明细:堆分配、RSS、缓存大小和缓冲池使用情况。
GET /cache/stats GET 元数据缓存和查询结果缓存的命中/未命中率。

健康检查

bash
# Basic health check
curl http://localhost:7000/health

# Response:
# {"status": "ok"}

写缓存统计

bash
curl http://localhost:7000/stats

# Response:
{
  "cache_size_bytes": 52428800,
  "cache_entries": 1250,
  "max_size_bytes": 4294967296,
  "utilization_pct": 1.22,
  "last_flush": "2026-02-13T10:30:00Z",
  "flush_interval_secs": 60
}

内存使用

bash
curl http://localhost:7000/memory

# Response:
{
  "rss_bytes": 536870912,
  "heap_allocated_bytes": 268435456,
  "write_cache_bytes": 52428800,
  "metadata_cache_bytes": 16777216,
  "query_cache_bytes": 33554432
}

缓存命中率

bash
curl http://localhost:7000/cache/stats

# Response:
{
  "metadata_cache": {
    "hits": 45230,
    "misses": 1280,
    "hit_rate": 0.972,
    "size_bytes": 16777216,
    "entries": 342
  },
  "query_cache": {
    "hits": 12450,
    "misses": 8320,
    "hit_rate": 0.599,
    "size_bytes": 33554432,
    "entries": 89,
    "ttl_secs": 300
  }
}

查询追踪

MynaDB 支持慢查询日志和 Trace ID 传播,用于调试查询性能问题。慢查询会被自动记录并附带执行详情,帮助您识别和优化有问题的查询。

慢查询日志配置

toml
[logging]
level = "info"
log_dir = "/data0/mynadb/logs"

# Slow query threshold (milliseconds)
# Queries exceeding this duration are logged at WARN level
slow_query_threshold_ms = 500

# Include execution plan in slow query logs
slow_query_log_plan = true

慢查询日志输出

text
WARN  query: Slow query detected
  catalog=mydb
  sql="SELECT * FROM cpu WHERE time > now() - interval '1 hour' ORDER BY time DESC"
  duration_ms=1523
  rows_returned=48200
  partitions_scanned=3
  files_scanned=12
  trace_id=abc123def456

Trace ID 传播

通过 X-Trace-Id 请求头传递 Trace ID,以便在您的可观测性栈中关联查询。Trace ID 会包含在日志条目、指标标签和错误响应中,实现端到端的追踪。

bash
# Pass a trace ID with your query
curl -H "X-Trace-Id: req-abc123" \
  "http://localhost:7000/query/mydb?sql=SELECT+count(*)+FROM+cpu"

# The trace ID appears in logs, allowing correlation
# with upstream services and distributed tracing systems
🔗 OpenTelemetry 集成

MynaDB 还支持通过 traceparent 请求头(W3C Trace Context)进行 OpenTelemetry (OTLP) 追踪上下文传播。这使得与 Jaeger、Zipkin 和 Grafana Tempo 等分布式追踪系统的无缝集成成为可能。

系统指标采集

MynaDB 自报告关键的系统级指标,涵盖 CPU、内存、磁盘和网络。这些指标包含在 /metrics 端点中,无需单独的 Node Exporter 即可提供对宿主环境的可视化。

⚡

CPU

进程 CPU 使用率百分比、使用的核心数和系统负载均值。 指标:mynadb_process_cpu_usage

💻

内存

驻留集大小(RSS)、堆分配和虚拟内存。 指标:mynadb_process_memory_bytes

💾

磁盘

数据目录磁盘使用量、可用空间和 I/O 吞吐量。 指标:mynadb_disk_usage_bytes

🌐

网络

发送/接收字节数,各协议活跃连接数(HTTP、Flight、MySQL)。 指标:mynadb_network_bytes_total

系统指标表

指标 类型 标签 说明
mynadb_process_cpu_usage Gauge - 进程 CPU 使用率百分比(0-100)
mynadb_process_memory_bytes Gauge type (rss, heap, virtual) 进程内存使用量(字节)
mynadb_disk_usage_bytes Gauge state (used, available) 数据目录磁盘使用情况
mynadb_disk_io_bytes_total Counter direction (read, write) 累计磁盘 I/O(字节)
mynadb_network_bytes_total Counter direction (rx, tx) 累计网络传输字节数
mynadb_open_file_descriptors Gauge - 打开的文件描述符数量

告警规则

使用 Prometheus 告警规则,在潜在问题影响工作负载之前获得通知。以下示例涵盖了 MynaDB 部署的常见场景。

yaml
groups:
  - name: mynadb_alerts
    rules:
      # Alert when query latency p99 exceeds 5 seconds
      - alert: MynaDBHighQueryLatency
        expr: |
          histogram_quantile(0.99,
            rate(mynadb_query_duration_seconds_bucket[5m])
          ) > 5
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: "High query latency on {{ $labels.instance }}"
          description: "p99 query latency is {{ $value | humanizeDuration }} (threshold: 5s)"

      # Alert when cache hit rate drops below 80%
      - alert: MynaDBLowCacheHitRate
        expr: |
          rate(mynadb_cache_hits_total[5m])
          / (rate(mynadb_cache_hits_total[5m]) + rate(mynadb_cache_misses_total[5m]))
          < 0.8
        for: 15m
        labels:
          severity: warning
        annotations:
          summary: "Low cache hit rate on {{ $labels.instance }}"
          description: "Cache hit rate is {{ $value | humanizePercentage }} (threshold: 80%)"

      # Alert when disk usage exceeds 85%
      - alert: MynaDBDiskUsageHigh
        expr: |
          mynadb_disk_usage_bytes{state="used"}
          / (mynadb_disk_usage_bytes{state="used"} + mynadb_disk_usage_bytes{state="available"})
          > 0.85
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Disk usage critical on {{ $labels.instance }}"
          description: "Disk usage is {{ $value | humanizePercentage }}. Consider adjusting retention or adding storage."

      # Alert when no writes are received for 5 minutes
      - alert: MynaDBNoWrites
        expr: |
          rate(mynadb_writes_total[5m]) == 0
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "No writes on {{ $labels.instance }}"
          description: "MynaDB has not received any write requests in the last 5 minutes."

      # Alert when compaction is backed up
      - alert: MynaDBCompactionBacklog
        expr: |
          mynadb_storage_bytes > 50e9
          and rate(mynadb_compaction_runs_total[1h]) == 0
        for: 30m
        labels:
          severity: warning
        annotations:
          summary: "Compaction stalled on {{ $labels.instance }}"
          description: "No compaction runs in the last 30 minutes with >50GB storage."

      # Alert when instance is down
      - alert: MynaDBInstanceDown
        expr: up{job="mynadb"} == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "MynaDB instance {{ $labels.instance }} is down"
          description: "The MynaDB instance has been unreachable for more than 1 minute."

预计算规则

将常用表达式预计算为 Recording Rules,以提升仪表板性能:

yaml
groups:
  - name: mynadb_recording_rules
    rules:
      # Write rate per second
      - record: mynadb:writes_per_second
        expr: rate(mynadb_writes_total[5m])

      # Query rate per second
      - record: mynadb:queries_per_second
        expr: rate(mynadb_queries_total[5m])

      # Cache hit rate
      - record: mynadb:cache_hit_rate
        expr: |
          rate(mynadb_cache_hits_total[5m])
          / (rate(mynadb_cache_hits_total[5m]) + rate(mynadb_cache_misses_total[5m]))

      # p50 query latency
      - record: mynadb:query_latency_p50
        expr: histogram_quantile(0.5, rate(mynadb_query_duration_seconds_bucket[5m]))

      # p99 query latency
      - record: mynadb:query_latency_p99
        expr: histogram_quantile(0.99, rate(mynadb_query_duration_seconds_bucket[5m]))

Grafana 仪表板

使用预构建的 Grafana 仪表板可视化所有这些指标。MynaDB 提供了一个全面的仪表板模板,包含写入/查询吞吐量、延迟分布、缓存性能、存储使用和系统资源利用率面板。

✅ 开箱即用的仪表板

请参阅 Grafana 集成指南,了解设置数据源、导入 MynaDB 仪表板和在 Grafana 中配置告警的详细步骤。

📊

写入吞吐量

每秒请求数和每秒字节数的写入速率与趋势线

⏱

查询延迟

p50、p95、p99 延迟直方图和百分位分布

🚀

缓存性能

命中/未命中率、淘汰计数和内存利用率

🗃

存储概览

磁盘使用趋势、压缩进度和分区计数

🛈 相关指南

Grafana 集成 - 仪表板设置与可视化
配置 - 完整配置参考
故障排除 - 常见问题诊断
管理控制台 - 内置 Web 管理界面