菜单
文档breadcrumb arrow Grafana Alloybreadcrumb arrow 设置breadcrumb arrow 迁移breadcrumb arrow 从 Prometheus 迁移
开源

从 Prometheus 迁移到 Grafana Alloy

内置的 Alloy convert 命令可以将您的 Prometheus 配置迁移到 Alloy 配置。

本主题介绍如何

  • 将 Prometheus 配置转换为 Alloy 配置。
  • 使用 Alloy 原生运行 Prometheus 配置。

本主题中使用的组件

开始之前

  • 您必须拥有一个 Prometheus 配置。
  • 您必须有一组 Prometheus 应用准备好将遥测数据推送到 Alloy。
  • 您必须熟悉 Alloy 中的组件概念。

转换 Prometheus 配置

要将配置完全从 Prometheus 迁移到 Alloy,您必须将 Prometheus 配置转换为 Alloy 配置。此转换使您可以充分利用 Alloy 中提供的许多额外功能。

在此任务中,您可以使用 convert CLI 命令从 Prometheus 配置输出 Alloy 配置。

  1. 打开终端窗口并运行以下命令。

    shell
    alloy convert --source-format=prometheus --output=<OUTPUT_CONFIG_PATH> <INPUT_CONFIG_PATH>

    替换以下内容

    • <INPUT_CONFIG_PATH>:Prometheus 配置的完整路径。
    • <OUTPUT_CONFIG_PATH>:输出 Alloy 配置的完整路径。
  2. 使用 <OUTPUT_CONFIG_PATH> 中的新 Alloy 配置运行 Alloy

调试

  1. 如果 convert 命令无法转换 Prometheus 配置,诊断信息将发送到 stderr
    您可以通过包含 --bypass-errors 标志来绕过任何非关键问题,并尽力转换输出 Alloy 配置。

    注意

    如果您绕过错误,转换后的配置行为可能与原始 Prometheus 配置不符。请确保在使用转换后的配置之前充分测试它。

    shell
    alloy convert --source-format=prometheus --bypass-errors --output=<OUTPUT_CONFIG_PATH> <INPUT_CONFIG_PATH>

    替换以下内容

    • <INPUT_CONFIG_PATH>:Prometheus 配置的完整路径。
    • <OUTPUT_CONFIG_PATH>:输出 Alloy 配置的完整路径。
  2. 您还可以通过包含 --report 标志来输出诊断报告。

    shell
    alloy convert --source-format=prometheus --report=<OUTPUT_REPORT_PATH> --output=<OUTPUT_CONFIG_PATH> <INPUT_CONFIG_PATH>

    替换以下内容

    • <INPUT_CONFIG_PATH>:Prometheus 配置的完整路径。
    • <OUTPUT_CONFIG_PATH>:输出 Alloy 配置的完整路径。
    • <OUTPUT_REPORT_PATH>:报告的输出路径。

    使用下面的示例 Prometheus 配置,诊断报告提供了以下信息

    plaintext
    (Info) Converted scrape_configs job_name "prometheus" into...
      A prometheus.scrape.prometheus component
    (Info) Converted 1 remote_write[s] "grafana-cloud" into...
      A prometheus.remote_write.default component

运行 Prometheus 配置

如果您尚未准备好完全切换到 Alloy 配置,您可以使用 Prometheus 配置来运行 Alloy。--config.format=prometheus 标志会指示 Alloy 将您的 Prometheus 配置转换为 Alloy 配置并直接加载,而无需保存新配置。这使您可以在不修改 Prometheus 配置基础设施的情况下尝试 Alloy。

在此任务中,您可以使用 run CLI 命令使用 Prometheus 配置运行 Alloy。

运行 Alloy 并包含命令行标志 --config.format=prometheus。您的配置文件必须是有效的 Prometheus 配置文件,而不是 Alloy 配置文件。

调试

  1. 您可以按照 convert CLI 命令的调试说明生成诊断报告。

  2. 有关正在运行的 Alloy 的更多信息,请参阅调试 Alloy

  3. 如果您的 Prometheus 配置无法直接转换并加载到 Alloy 中,诊断信息将发送到 stderr。您可以通过在 --config.format=prometheus 之外包含 --config.bypass-conversion-errors 标志来绕过任何非关键问题并启动 Alloy。

    注意

    如果您绕过错误,转换后的配置行为可能与原始 Prometheus 配置不符。请勿在生产环境中使用此标志。

示例

本示例演示如何将 Prometheus 配置文件转换为 Alloy 配置文件。

以下 Prometheus 配置文件作为转换的输入。

yaml
global:
  scrape_timeout:    45s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:12345"]

remote_write:
  - name: "grafana-cloud"
    url: "https://prometheus-us-central1.grafana.net/api/prom/push"
    basic_auth:
      username: <USERNAME>
      password: <PASSWORD>

convert 命令接收 YAML 文件作为输入,并输出一个 Alloy 配置]文件。

shell
alloy convert --source-format=prometheus --output=<OUTPUT_CONFIG_PATH> <INPUT_CONFIG_PATH>

替换以下内容

  • <INPUT_CONFIG_PATH>:Prometheus 配置的完整路径。
  • <OUTPUT_CONFIG_PATH>:输出 Alloy 配置的完整路径。

新的 Alloy 配置文件如下所示

alloy
prometheus.scrape "prometheus" {
  targets = [{
    __address__ = "localhost:12345",
  }]
  forward_to     = [prometheus.remote_write.default.receiver]
  job_name       = "prometheus"
  scrape_timeout = "45s"
}

prometheus.remote_write "default" {
  endpoint {
    name = "grafana-cloud"
    url  = "https://prometheus-us-central1.grafana.net/api/prom/push"

    basic_auth {
      username = "USERNAME"
      password = "PASSWORD"
    }

    queue_config {
      capacity             = 2500
      max_shards           = 200
      max_samples_per_send = 500
    }

    metadata_config {
      max_samples_per_send = 500
    }
  }
}

限制

配置转换是尽力而为的。如果转换无法执行,Alloy 会发出警告或错误。

转换配置后,请检查生成的 Alloy 配置文件,并在生产环境中使用之前验证其正确性。

以下列表仅适用于 convert 命令,不适用于 Alloy

  • 以下配置无法转换为 Alloy:rule_files, alerting, remote_read, storage, 和 tracing。任何额外的非支持功能在转换过程中将作为错误返回。
  • 检查您是否在使用 Prometheus 时使用了配置文件中不存在的额外命令行参数。例如,--web.listen-address
  • Alloy 暴露的元监控指标通常与 Prometheus 元监控指标匹配,但名称不同。请确保您使用新的指标名称,例如,在您的告警和仪表盘查询中。
  • Alloy 生成的日志与 Prometheus 生成的日志不同。
  • Alloy 暴露 Alloy UI