什么是 Pyroscope?

Pyroscope是一个开源的持续分析系统,使用Go语言实现。服务端使用web页面查看,提供丰富的分析的功能,客户端提供Go、Java、Python、Ruby、PHP、.NET等多种语言的支持,并且支持PUSH、PULL两种采集方式。

Pyroscope 是一个开源的持续性能剖析平台。它能够帮你:

  • 找出源代码中的性能问题和瓶颈
  • 解决 CPU 利用率高的问题
  • 理解应用程序的调用树(call tree)
  • 追踪随一段时间内变化的情况

特性:

  • 可以存储来自多个应用程序的多年剖析数据
  • 你可以一次查看多年的数据或单独查看特定的事件
  • 较低的 CPU 开销
  • 数据压缩效率高,磁盘空间要求低
  • 快捷的 UI 界面

部署架构图:

img.png

Pyroscope server安装

Pyroscope server端可以通过docker安装:

docker run -it -p 4040:4040 pyroscope/pyroscope:latest server

也可以在各中操作系统中直接安装。

比如Mac:

1
2
brew install pyroscope-io/brew/pyroscope
brew services start pyroscope-server

各Liunx发行版也方便安装

比如ubuntu:

1
2
wget https://dl.pyroscope.io/release/pyroscope_0.10.2_amd64.deb
sudo apt-get install ./pyroscope_0.10.2_amd64.deb

安装完成后,就可以通过web界面访问了: http://localhost:4040, 你可以通过配置文件更改监听端口以及其它一些配置项。

Pyroscope 客户端实践

Pyroscope 提供了多种客户端,比如Go、Java、Python、Ruby、PHP、.NET等多种语言的支持,并且支持PUSH、PULL两种采集方式。这里介绍 GO 语言的客户端实例,采用 PUSH 的方式.

GO 客户端的 PUSH 方式通过使用标准runtime/pprof包来收集分析数据。

1 安装 go 客户端

go get github.com/pyroscope-io/client/pyroscope

2 在你的项目的 main.go 文件中,添加如下代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    pyroscope.Start(pyroscope.Config{
        ApplicationName: "simple.golang.app",
        
        ServerAddress: "http://pyroscope-server:4040",
        
        Logger: pyroscope.StandardLogger,
        
        ProfileTypes: []pyroscope.ProfileType{
            pyroscope.ProfileCPU,
            pyroscope.ProfileAllocObjects,
            pyroscope.ProfileAllocSpace,
            pyroscope.ProfileInuseObjects,
            pyroscope.ProfileInuseSpace,
        },
	})

3 完整代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

import (
	"github.com/pyroscope-io/client/pyroscope"
	"net/http"
)

func main() {
	pyroscope.Start(pyroscope.Config{
		ApplicationName: "simple.golang.app",

		ServerAddress: "http://xx.xx.xx.xx:4040",

		Logger: pyroscope.StandardLogger,

		ProfileTypes: []pyroscope.ProfileType{
			pyroscope.ProfileCPU,
			pyroscope.ProfileAllocObjects,
			pyroscope.ProfileAllocSpace,
			pyroscope.ProfileInuseObjects,
			pyroscope.ProfileInuseSpace,
		},
	})
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello, world!"))
	})
	http.ListenAndServe(":8080", nil)
}

4 浏览器打开相应的 dashboard 界面,就可以看到相应的监控数据了 img_1.png

参考