菜单
开源

在 Linux 上设置 eBPF profiling

要在 Linux 上使用 Grafana Alloy 设置 eBPF profiling,您需要:

  • 验证您的系统满足要求。
  • 安装 Alloy
  • 创建 Alloy 配置文件。有关更多信息,请参阅 配置参考
  • 运行 Alloy。
  • 最后,验证是否收到了性能剖析数据。

开始之前

开始之前,您需要:

注意

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

验证系统是否满足要求

eBPF profiler 需要 Linux 内核版本 >= 4.9 (由于 BPF_PROG_TYPE_PERF_EVENT 的原因)。

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

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

shell
uname -r

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

安装 Alloy

按照安装说明下载并为您当前的 Linux 发行版安装 Alloy。

配置 Alloy

要配置 Alloy eBPF profiler 以分析本地进程,请使用 discovery.process 组件 并在 pyroscope.ebpf 组件中添加一个默认目标。所有进程都将被分析并归入默认目标下。

创建一个名为 alloy.config 的文件,内容如下:

alloy
discovery.process "all" {

}

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

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

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

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

有关配置 Alloy 的信息,请参阅Kubernetes 上的 Grafana Alloy

有关所用特定块的信息,请参阅Grafana Alloy 参考discovery.process

将占位符 <URL> 替换为适当的服务器 URL。这可以是 Grafana Cloud URL 或您自己的自定义 Pyroscope 服务器 URL。

如果您需要向 Grafana Cloud 发送数据,则必须配置 HTTP Basic 身份验证。将 <User> 替换为您的 Grafana Cloud stack 用户,将 <Password> 替换为您的 Grafana Cloud API 密钥。

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

注意

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

启动 Alloy

注意

eBPF profiler 需要 root 权限。

要启动 Alloy,运行:

shell
alloy run alloy.config

如果您看到以下错误:

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 权限运行 Alloy,eBPF profiler 需要这些权限才能工作。

验证是否收到了性能剖析数据

要验证 Pyroscope 服务器是否收到了性能剖析数据,请前往 Pyroscope UI 或Grafana Pyroscope 数据源。从下拉菜单中选择一个性能剖析类型和服务。