菜单
文档breadcrumb arrow Grafana Alloybreadcrumb arrow 收集和转发数据breadcrumb arrow 收集 Alloy 遥测数据
开源

设置元监控以收集 Alloy 遥测数据

你可以配置 Alloy 收集其自身的遥测数据并将其转发到你选择的后端。

本主题介绍如何收集和转发 Alloy 的指标、日志和追踪数据。

本主题中使用的组件和配置块

开始之前

  • 确定 Alloy 的遥测数据发送到何处。
  • 熟悉 Alloy 中的组件概念。

元监控指标

Alloy 使用 Prometheus 暴露格式公开其内部指标。

指标通过 --server.http.listen-addr 命令行标志配置的地址上的 /metrics 端点公开。例如,127.0.0.1:12345/metrics。但是,也可以使用 prometheus.exporter.self 组件访问相同的指标。

在本任务中,你将使用 prometheus.exporter.selfprometheus.scrape 组件抓取 Alloy 的内部指标并将其转发到兼容的 Alloy 组件。

  1. 将以下 prometheus.exporter.self 组件添加到你的配置中。该组件不接受任何参数。

    alloy
    prometheus.exporter.self "<SELF_LABEL>" {
    }

    替换以下内容

    • <SELF_LABEL>:组件的标签,例如 defaultmetamonitoring。标签在同一配置文件中的所有 prometheus.exporter.self 组件中必须唯一。
  2. 将以下 prometheus.scrape 组件添加到你的配置文件中。

    alloy
    prometheus.scrape "<SCRAPE_LABEL>" {
      targets    = prometheus.exporter.self.<SELF_LABEL>.targets
      forward_to = [<METRICS_RECEIVER_LIST>]
    }

    替换以下内容

    • <SCRAPE_LABEL>:抓取组件的标签,例如 default。标签在同一配置文件中的所有 prometheus.scrape 组件中必须唯一。
    • <METRICS_RECEIVER_LIST>:要转发指标的组件接收器列表,用逗号分隔。例如,要发送到远程写入组件,使用 prometheus.remote_write.WRITE_LABEL.receiver。类似地,要将数据发送到重新打标签组件,使用 prometheus.relabel.PROCESS_LABEL.receiver。要在 OTLP 格式中使用数据,你可以将数据发送到转换器组件,例如 otelcol.receiver.prometheus.OTEL.receiver

以下示例演示了配置可能的一系列组件。

alloy
prometheus.exporter.self "default" {
}

prometheus.scrape "metamonitoring" {
  targets    = prometheus.exporter.self.default.targets
  forward_to = [prometheus.remote_write.default.receiver]
}

prometheus.remote_write "default" {
  endpoint {
    url = "http://mimir:9009/api/v1/push"
  }
}

元监控日志

logging 块定义了 Alloy 的日志行为。

在本任务中,你使用 logging 块将 Alloy 的日志转发到兼容组件。该块没有标签,每个配置文件只能提供一次。

  1. 将以下 logging 配置块添加到你的配置文件的顶层。

    alloy
    logging {
      level    = "<LOG_LEVEL>"
      format   = "<LOG_FORMAT>"
      write_to = [<LOGS_RECEIVER_LIST>]
    }

    替换以下内容

    • <LOG_LEVEL>:Alloy 日志使用的日志级别。如果未设置此属性,默认为 info
    • <LOG_FORMAT>:Alloy 日志使用的日志格式。如果未设置此属性,默认为 logfmt
    • <LOGS_RECEIVER_LIST>:要转发日志的组件接收器列表,用逗号分隔。例如,要发送到处理组件,使用 loki.process.PROCESS_LABEL.receiver。类似地,要将数据发送到重新打标签组件,使用 loki.relabel.PROCESS_LABEL.receiver。要在 OTLP 格式中使用数据,你可以将数据发送到转换器组件,例如 otelcol.receiver.loki.OTEL.receiver

以下示例演示了配置 logging 块并将其发送到兼容组件。

alloy
logging {
  level    = "warn"
  format   = "json"
  write_to = [loki.write.default.receiver]
}

loki.write "default" {
  endpoint {
    url = "http://loki:3100/loki/api/v1/push"
  }
}

元监控追踪

tracing 块定义了 Alloy 的追踪行为。

在本任务中,你使用 tracing 块将 Alloy 内部追踪转发到兼容组件。该块没有标签,每个配置文件只能提供一次。

  1. 将以下 tracing 配置块添加到你的配置文件的顶层。

    alloy
    tracing {
      sampling_fraction = <SAMPLING_FRACTION>
      write_to          = [<TRACES_RECEIVER_LIST>]
    }

    替换以下内容

    • <SAMPLING_FRACTION>:要保留的追踪数据的比例。如果未设置此属性,默认为 0.1
    • <TRACES_RECEIVER_LIST>:要转发追踪数据的组件接收器列表,用逗号分隔。例如,要发送到 OpenTelemetry exporter 组件,使用 otelcol.exporter.otlp.EXPORT_LABEL.input

以下示例演示了配置 tracing 块并将其发送到兼容组件。

alloy
tracing {
  sampling_fraction = 0.1
  write_to          = [otelcol.exporter.otlp.default.input]
}

otelcol.exporter.otlp "default" {
    client {
        endpoint = "tempo:4317"
    }
}