mcpField
A
alloy

alloy

github.com/grafana云与 DevOps2955.1k 次安装1 天前更新
代码仓库

关于 alloy

Grafana Alloy 是用于配置和运行 OpenTelemetry 兼容采集管道的技能,面向采集指标、日志、追踪和性能剖析数据的运维人员。内容涵盖配置校验、数据转发及故障排查。

主要功能

  1. 01格式化并校验配置
  2. 02采集并转发指标
  3. 03采集并转发日志
  4. 04接收并转发 OTLP 追踪
  5. 05提供采集管道参考模式

适用场景

  1. 01本地检查 Alloy 配置
  2. 02将应用日志发送到 Loki
  3. 03将 OTLP 追踪发送到 Tempo

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/grafana/skills --skill alloy

安装后在 Claude Code 中输入/alloy确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
collection-patterns.md
components.md
config-syntax.md

详细介绍

Grafana Alloy

文档:https://grafana.com/docs/alloy/latest/

兼容 OpenTelemetry 的采集器——一个二进制文件即可处理指标、日志、链路和性能剖析数据。

前置条件

  • 已安装 alloy CLI(brew install grafana/grafana/alloy、apt install alloy,或使用 grafana/alloy Docker 镜像)
  • 用于发送数据的端点(Grafana Cloud、Prometheus、Loki、Tempo、Pyroscope)
  • 该端点的 API 密钥和用户名,并导出为 GRAFANA_API_KEY 等环境变量

常见工作流

1. 在本地编写并验证配置
// config.alloy — metrics → Grafana Cloud
prometheus.scrape "app" {
  targets = [{"__address__" = "localhost:9090"}]
  forward_to = [prometheus.remote_write.cloud.receiver]
  scrape_interval = "30s"
}

prometheus.remote_write "cloud" {
  endpoint {
    url = sys.env("PROMETHEUS_URL")
    basic_auth {
      username = sys.env("PROM_USER")
      password = sys.env("GRAFANA_API_KEY")
    }
  }
}
# 1. Format + syntax-check (catches typos before run)
alloy fmt config.alloy
alloy validate config.alloy

# 2. Run it
alloy run config.alloy
# or as a service: systemctl restart alloy

# 3. Verify all components are healthy via the UI on port 12345
curl -s http://localhost:12345/api/v0/web/components \
  | jq '.[] | select(.health.state != "healthy") | {id, state:.health.state, msg:.health.message}'
# Expect: empty (nothing unhealthy). Otherwise the row shows the failing component + reason.

# 4. Verify samples are flowing
curl -s http://localhost:12345/metrics \
  | grep -E '^prometheus_remote_storage_(samples_total|enqueue_retries_total)' | head
# samples_total should be > 0 and rising; retries should be 0.
2. 添加日志发送(文件 → Loki)
loki.source.file "app_logs" {
  targets    = [{ __path__ = "/var/log/app/*.log", job = "app" }]
  forward_to = [loki.write.cloud.receiver]
}

loki.write "cloud" {
  endpoint {
    url = sys.env("LOKI_URL")
    basic_auth {
      username = sys.env("LOKI_USER")
      password = sys.env("GRAFANA_API_KEY")
    }
  }
}
# Verify in Grafana → Explore → Loki:
#   {job="app"}
# Expect lines streaming. If empty:
#   - check /var/log/app/*.log actually exists + readable by the alloy user
#   - http://localhost:12345 → loki.source.file.app_logs → "Targets" tab
3. 接收 OTLP 链路并发送到 Tempo
otelcol.receiver.otlp "default" {
  grpc { endpoint = "0.0.0.0:4317" }
  http { endpoint = "0.0.0.0:4318" }
  output { traces = [otelcol.exporter.otlp.tempo.input] }
}

otelcol.exporter.otlp "tempo" {
  client {
    endpoint = "tempo-xxx.grafana.net/tempo:443"
    auth     = otelcol.auth.basic.grafana_cloud.handler
  }
}

otelcol.auth.basic "grafana_cloud" {
  username = sys.env("TEMPO_USER")
  password = sys.env("GRAFANA_API_KEY")
}
# Verify in Grafana → Explore → Tempo → service.name = your-service
# At the Alloy level, watch the receiver:
curl -s http://localhost:12345/metrics | grep otelcol_receiver_accepted_spans

完整模式集(Kubernetes 发现与重新标记,以及包含全部 4 种信号的完整 Cloud 管道):references/collection-patterns.md。

参考资料

故障排查

  • alloy validate → “component not found” → 版本过旧;运行 alloy --version 并升级
  • UI 显示组件 unhealthy → 在 http://localhost:12345 中点击该组件,查看实时错误
  • prometheus_remote_storage_samples_dropped_total 持续增长 → 检查 enqueue_retries_total、remote_write 端点 URL 和凭据
  • 没有链路到达 Tempo → 检查 otelcol_receiver_refused_spans,以及导出器的 otelcol_exporter_sent_spans / otelcol_exporter_send_failed_spans

资源

评论

支持 Markdown