Skip to content

Prometheus

Host metrics 是指从运行应用程序的主机的操作系统收集的指标,这些指标包括 CPU、内存、磁盘和网络使用情况。 了解主机指标至关重要,因为它可以帮助你识别可能影响应用程序整体性能的潜在问题或瓶颈。 在本教程中,我们将向你展示如何收集 Host metrics,将它们发送到 GreptimeDB 并进行数据可视化。

创建服务

为了充分体验 GreptimeCloud 的强大功能,你需要创建一个包含身份验证数据库的服务。打开 GreptimeCloud 控制台,注册并登录。然后单击 New Service 按钮并配置以下内容:

  • Service Name: 服务名称。
  • Description: 有关该服务的更多信息。
  • Region: 选择数据库所在的区域。
  • Plan: 选择要使用的定价计划。

现在创建服务,准备向其写入一些数据。

写入数据

准备

示例

我们将使用 node exporter 来监控系统,并通过 Prometheus 将指标发送到 GreptimeDB。

首先,创建一个名为 quick-start-prometheus 的新目录来托管我们的项目,创建名为 compose.yml 的 docker-compose 文件,并添加以下内容:

yaml
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    depends_on:
      - node_exporter
    ports:
      - 9090:9090
    volumes:
      - ./prometheus-greptimedb.yml:/etc/prometheus/prometheus.yml:ro

  node_exporter:
    image: quay.io/prometheus/node-exporter:latest
    container_name: node_exporter
    ports:
      - 9100:9100
    command:
      - '--path.rootfs=/'
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    depends_on:
      - node_exporter
    ports:
      - 9090:9090
    volumes:
      - ./prometheus-greptimedb.yml:/etc/prometheus/prometheus.yml:ro

  node_exporter:
    image: quay.io/prometheus/node-exporter:latest
    container_name: node_exporter
    ports:
      - 9100:9100
    command:
      - '--path.rootfs=/'

以上的配置文件将启动 Prometheus 和 node exporter。接下来,创建一个名为 prometheus-greptimedb.yml 的新文件,并添加以下内容:

yaml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# 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: 'node'
    static_configs:
      - targets: ['node_exporter:9100']

remote_write:
  - url: https://<host>/v1/prometheus/write?db=<dbname>
    basic_auth:
      username: <username>
      password: <password>
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# 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: 'node'
    static_configs:
      - targets: ['node_exporter:9100']

remote_write:
  - url: https://<host>/v1/prometheus/write?db=<dbname>
    basic_auth:
      username: <username>
      password: <password>

通过上面的配置文件,Prometheus 从 node exporter 中抓取指标并将其发送到 GreptimeDB。有关 <host>, <dbname>, <username><password> 的信息,请参考 GreptimeDBGreptimeCloud 中的 Prometheus 文档。

最后启动 Docker 容器:

bash
docker-compose up
docker-compose up

连接信息可以在 GreptimeCloud 控制台中找到。

数据可视化

在开发人员的日常工作中,可视化面板中的数据和监控指标非常重要。在 GreptimeCloud 控制台中,单击 Open Prometheus Workbench,然后单击 + New RulesetAdd Group,将组命名为 host-monitor 并添加面板。

要为关注的所有 Table 添加面板,请选择相应的 Table 并逐个单击 Add Panel。添加完所有必要的面板后,单击 save 按钮保存它们。之后你可以在日常工作中通过查看面板来监控指标。此外你可以为 Panel 设置警报规则,以便在指标超过阈值时收到通知。