菜单
开源

接收来自 Pyroscope SDK 的 Profiles

Alloy 中的 pyroscope.receive_http 组件接收来自使用 Pyroscope SDK 进行插桩的应用的 profiles。这种方法提供了以下几个好处:

  • 通过将 profiles 发送到本地 Alloy 实例而不是互联网上,降低延迟
  • 将基础设施关注点(认证、路由)与应用代码分离
  • 集中管理认证和元数据丰富

有关此组件的更多信息,请参阅 pyroscope.receive_http 组件文档。

注意

pyroscope.receive_http 组件目前处于公共预览阶段。要使用此组件,请将 --stability.level 标志设置为 public-preview。有关 Alloy 运行用法的更多信息,请参阅 run 命令文档

要设置 profile 接收,您需要:

  1. 配置 Alloy 组件
  2. 配置您的应用 SDK
  3. 启动 Alloy

配置 Alloy 组件

配置至少需要两个组件:

这是一个基本配置,设置了一个简单的 profile 收集管道。它创建一个接收器来收集来自您的应用的 profiles,并通过写入器组件将它们转发到 Pyroscope 后端:

alloy
// Receives profiles over HTTP
pyroscope.receive_http "default" {
   http {
       listen_address = "0.0.0.0"
       listen_port = 9999
   }
   forward_to = [pyroscope.write.backend.receiver]
}

// Forwards profiles to Pyroscope
pyroscope.write "backend" {
   endpoint {
       url = "http://pyroscope:4040"
   }
}

配置应用 SDK

更新您的应用 SDK 配置,将其指向 Alloy 的接收端点而不是直接指向 Pyroscope。例如,在 Go 中:

Go
config := pyroscope.Config{
    ApplicationName: "my.service.cpu",
    ServerAddress:   "https://:9999", // Alloy's receive endpoint
}

请查看您特定语言 SDK 的文档以了解确切的配置选项。

示例

本节中的示例提供了您可以作为自己配置起点的样本。

浏览 Pyroscope GitHub 仓库中的示例,了解如何配置 Grafana Alloy 以接收来自使用 Pyroscope 插桩的 Golang 应用的 profiles。

基本接收设置

此示例展示了一个基本设置,在端口 9090 接收 profiles 并将其转发到本地 Pyroscope 实例:

alloy
pyroscope.receive_http "default" {
    http {
        listen_address = "0.0.0.0"
        listen_port = 9090
    }
    forward_to = [pyroscope.write.production.receiver]
}

pyroscope.write "production" {
    endpoint {
        url = "https://:4040"
    }
}

认证

要将 profiles 发送到需要认证的 Pyroscope 端点:

alloy
pyroscope.write "production" {
    endpoint {
        url = "http://pyroscope:4040"
        basic_auth {
            username = env("PYROSCOPE_USERNAME")
            password = env("PYROSCOPE_PASSWORD")
        }
    }
}

添加外部标签

外部标签会添加到通过写入组件转发的所有 profiles。这对于添加基础设施元数据很有用:

alloy
pyroscope.receive_http "default" {
    http {
        listen_address = "0.0.0.0"
        listen_port = 9999
    }
    forward_to = [pyroscope.write.backend.receiver]
}

pyroscope.write "backend" {
    endpoint {
        url = "http://pyroscope:4040"
    }
    external_labels = {
        "env"      = "production",
        "region"   = "us-west-1",
        "instance" = env("HOSTNAME"),
        "cluster"  = "main",
    }
}

多个目标

将接收到的 profiles 转发到多个目标 - 这对于测试或迁移场景很有用:

alloy
pyroscope.receive_http "default" {
    http {
        listen_address = "0.0.0.0"
        listen_port = 9999
    }
    forward_to = [pyroscope.write.staging.receiver, pyroscope.write.production.receiver]
}

// Send profiles to staging
pyroscope.write "staging" {
    endpoint {
        url = "http://pyroscope-staging:4040"
    }
    external_labels = {
        "env" = "staging",
    }
}

// Send profiles to production
pyroscope.write "production" {
    endpoint {
        url = "http://pyroscope-production:4041"
    }
    external_labels = {
        "env" = "production",
    }
}

注意

此配置将复制接收到的 profiles,并将副本发送到每个配置的 pyroscope.write 组件。

另一种方法是配置多个接收器和多个目标:

alloy
pyroscope.receive_http "staging" {
    http {
        listen_address = "0.0.0.0"
        listen_port = 9998
    }
    forward_to = [pyroscope.write.staging.receiver]
}

pyroscope.receive_http "production" {
    http {
        listen_address = "0.0.0.0"
        listen_port = 9999
    }
    forward_to = [pyroscope.write.production.receiver]
}

// Send profiles to staging
pyroscope.write "staging" {
    endpoint {
        url = "http://pyroscope-staging:4040"
    }
    external_labels = {
        "env" = "staging",
    }
}

// Send profiles to production
pyroscope.write "production" {
    endpoint {
        url = "http://pyroscope-production:4041"
    }
    external_labels = {
        "env" = "production",
    }
}

有关组件配置选项的更多信息,请参阅: