菜单
开源

在 Linux 上设置 eBPF 性能分析

要在 Linux 上使用 Grafana Agent 设置 eBPF 性能分析,您需要

  • 验证您的系统是否满足要求。
  • 安装 Grafana Agent 流模式。
  • 创建 Grafana Agent 配置文件。有关更多信息,请参阅配置参考
  • 运行 Grafana Agent 或 Grafana Alloy。
  • 最后,验证是否已收到配置文件。

注意

Grafana Alloy 是我们对 OTel 收集器分发的最新名称。Grafana Agent 已被弃用,并且在 2025 年 10 月 31 日之前处于长期支持 (LTS) 状态。Grafana Agent 将于 2025 年 11 月 1 日达到使用寿命 (EOL)。阅读更多关于我们建议迁移到Grafana Alloy的原因。

先决条件

在您开始之前,您需要

注意

如果您没有 Grafana 和/或 Pyroscope 服务器,您可以使用 [Grafana Cloud][gcloud] 免费计划开始使用。

验证系统是否满足要求

eBPF 性能分析器需要 Linux 内核版本 >= 4.9(由于BPF_PROG_TYPE_PERF_EVENT)。

BPF_PROG_TYPE_PERF_EVENT 是一种 eBPF 程序类型,可以附加到硬件或软件事件,例如 Linux 内核中的性能监控计数器或跟踪点。

要打印机器的内核版本,请运行

shell
uname -r

确保您的内核版本 >= 4.9。

安装 Grafana Agent

按照 安装说明 下载并安装适合您当前 Linux 发行版的 Grafana Agent。

通过运行以下命令验证代理是否已正确安装

shell
grafana-agent-flow --version

配置 Grafana Agent

要配置 Grafana Agent eBPF 分析器以分析本地进程,您需要将 targets_only 标志设置为 false 并在 pyroscope.ebpf 组件中添加一个默认目标。所有进程都将被分析并在默认目标下分组。

注意

我们正在 开发更灵活的配置,允许您指定要分析的进程。

创建一个名为 agent.river 的文件,其内容如下

river
discovery.process "all" {

}

discovery.relabel "agent" {
    targets = discovery.process.all.targets
    // Filter needed processes
    rule {
        source_labels = ["__meta_process_exe"]
        regex = ".*/grafana-agent"
        action = "keep"
    }
}

pyroscope.ebpf "instance" {
 forward_to     = [pyroscope.write.endpoint.receiver]
 targets = discovery.relabel.agent.output
}

pyroscope.scrape "local" {
  forward_to     = [pyroscope.write.endpoint.receiver]
  targets    = [
    {"__address__" = "localhost:12345", "service_name"="grafana/agent"},
  ]
}

pyroscope.write "endpoint" {
 endpoint {
  basic_auth {
   password = "<PASSWORD>"
   username = "<USERNAME>"
  }
  url = "<URL>"
 }
 external_labels = {
  "env"      = "prod",
  "instance" = env("HOSTNAME"),
 }
}

<URL> 占位符替换为相应的服务器 URL。这可能是 Grafana Cloud URL 或您自己的自定义 Pyroscope 服务器 URL。

如果需要将数据发送到 Grafana Cloud,您需要配置 HTTP 基本身份验证。将 <User> 替换为您的 Grafana Cloud 堆栈用户,将 <Password> 替换为您的 Grafana Cloud API 密钥。

注意

如果您使用的是自己的 Pyroscope 服务器,则可以完全删除 basic_auth 部分。

有关更多信息,请参阅 配置 Grafana Pyroscope 数据源文档

启动 Grafana Agent

要启动 Grafana Agent,请运行以下命令

shell
grafana-agent-flow run agent.river

如果您看到以下错误

shell
level=error msg="component exited with error" component=pyroscope.ebpf.local_pods err="ebpf profiling session start: load bpf objects: field DisassociateCtty: program disassociate_ctty: map events: map create: operation not permitted (MEMLOCK may be too low, consider rlimit.RemoveMemlock)"

确保您以 root 权限运行代理,这是 eBPF 分析器正常工作所需的权限。

验证是否已收到分析结果

要验证 Pyroscope 服务器是否已收到分析结果,请转到 Pyroscope UI 或 Grafana Pyroscope 数据源。从下拉菜单中选择一个分析类型和一个服务。

discovery.process