使用 Prometheus 进行可观测性

您可以从 Metabase 中以 Prometheus 格式导出指标。

在本地运行 Metabase 和 Prometheus

为了让您了解 Metabase 和 Prometheus 在生产环境中的工作方式,我们将逐步介绍如何在本地设置 Metabase 和 Prometheus。

使用 MB_PROMETHEUS_SERVER_PORT 启动 Metabase

下载最新的 Metabase JAR,并使用环境变量运行 Metabase 以指定 Prometheus 服务器端口

MB_PROMETHEUS_SERVER_PORT=9191 java --add-opens java.base/java.nio=ALL-UNNAMED -jar metabase.jar

MB_PROMETHEUS_SERVER_PORT=9191 指定 Metabase 将使用哪个端口 (9191) 向 Prometheus 发送数据。为了澄清这里涉及的端口

  • 端口 3000 是 Metabase 用于提供 Metabase 应用的端口。您可以使用 MB_JETTY_PORT 设置另一个端口(例如,MB_JETTY_PORT=3001)。
  • 端口 9191(或您使用 MB_PROMETHEUS_SERVER_PORT 环境变量指定的任何端口)是 Prometheus 用于从 Metabase 抓取指标的端口。
  • 端口 9090 是 Prometheus 用于提供 Prometheus 应用程序的端口。

当您启动 Metabase 时,Metabase 日志将告诉您 Metabase 正在启动 prometheus metrics collectorprometheus metrics web-server

(truncated logs)
2022-09-01 17:47:38,808 INFO metabase.util :: Database setup took 3.4 s
2022-09-01 17:47:38,826 INFO metabase.core :: Setting up prometheus metrics
2022-09-01 17:47:38,827 INFO metabase.prometheus :: Starting prometheus metrics collector
2022-09-01 17:47:38,839 INFO metabase.prometheus :: Starting prometheus metrics web-server on port 9,191
(truncated logs)

您可以在 https://127.0.0.1:3000 查看本地运行的 Metabase。

下载并配置 Prometheus

下载 Prometheus,并解压文件。

更改到 Prometheus 目录,添加以下 YAML 文件来配置您的 Prometheus

Prometheus 配置文件示例

global:
  scrape_interval: 15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: "codelab-monitor"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s
    # use whatever port here that you set for MB_PROMETHEUS_SERVER_PORT
    static_configs:
      - targets: ["localhost:9191"]

您需要将“target”更改为 Metabase 所在的位置,对于此特定示例,Metabase 与 Prometheus 运行在同一主机(“localhost”)上。

在本地运行 Prometheus

在 Prometheus 目录的新终端进程中,运行

./prometheus --config.file=prometheus.yml

然后检查 https://127.0.0.1:9090。您应该看到 Prometheus 应用,并能够搜索 Metabase 发出的各种指标。

Prometheus page showing `jvm_thread_state` graph

指标输出示例

以下是 Metabase 的一些示例输出

'# HELP jvm_threads_current Current thread count of a JVM
'# TYPE jvm_threads_current gauge
jvm_threads_current 81.0
'# HELP jvm_threads_daemon Daemon thread count of a JVM
'# TYPE jvm_threads_daemon gauge
jvm_threads_daemon 36.0
'# HELP jvm_threads_peak Peak thread count of a JVM
'# TYPE jvm_threads_peak gauge
jvm_threads_peak 81.0
'# HELP jvm_threads_started_total Started thread count of a JVM
'# TYPE jvm_threads_started_total counter
jvm_threads_started_total 104.0
'# HELP jvm_threads_deadlocked Cycles of JVM-threads that are in deadlock waiting to acquire object monitors or ownable synchronizers
'# TYPE jvm_threads_deadlocked gauge
jvm_threads_deadlocked 0.0

导出的指标

Metabase 导出的指标包括

  • c3p0_max_pool_size
  • c3p0_min_pool_size
  • c3p0_num_busy_connections
  • c3p0_num_connections
  • c3p0_num_idle_connections
  • c3p0_num_threads_awaiting_checkout_default_user
  • jetty_async_dispatches_total
  • jetty_async_requests_total
  • jetty_async_requests_waiting
  • jetty_async_requests_waiting_max
  • jetty_dispatched_active
  • jetty_dispatched_active_max
  • jetty_dispatched_time_max
  • jetty_dispatched_time_seconds_total
  • jetty_dispatched_total
  • jetty_expires_total
  • jetty_request_time_max_seconds
  • jetty_request_time_seconds_total
  • jetty_requests_active
  • jetty_requests_active_max
  • jetty_requests_total
  • jetty_responses_bytes_total
  • jetty_responses_total
  • jetty_stats_seconds
  • jvm_gc_collection_seconds_count
  • jvm_gc_collection_seconds_sum
  • jvm_memory_bytes_committed
  • jvm_memory_bytes_init
  • jvm_memory_bytes_max
  • jvm_memory_bytes_used
  • jvm_memory_objects_pending_finalization
  • jvm_memory_pool_bytes_committed
  • jvm_memory_pool_bytes_init
  • jvm_memory_pool_bytes_max
  • jvm_memory_pool_bytes_used
  • jvm_memory_pool_collection_committed_bytes
  • jvm_memory_pool_collection_init_bytes
  • jvm_memory_pool_collection_max_bytes
  • jvm_memory_pool_collection_used_bytes
  • jvm_threads_current
  • jvm_threads_daemon
  • jvm_threads_deadlocked
  • jvm_threads_deadlocked_monitor
  • jvm_threads_peak
  • jvm_threads_started_total
  • jvm_threads_state
  • process_cpu_seconds_total
  • process_max_fds
  • process_open_fds
  • process_start_time_seconds
  • process_virtual_memory_bytes
  • metabase_email_messages_total
  • metabase_email_messages_created
  • metabase_email_message_errors_total
  • metabase_email_message_errors_created

延伸阅读

阅读其他Metabase 版本的文档。