基于pyroscope-go项目性能数据持续收集

背景

在日常开发过程中,遇到类似崩溃或者cpu飚高时刻没法获取到有效的pprof数据,从而去优化修复项目。所以我们需要引入持续性分析且能够允许我们随着时间的推移了解系统资源的使用情况,从而定位、调试和修复性能相关的问题。

使用场景

  1. 查找代码中的性能问题

  2. 解决高 CPU 利用率的问题

  3. 定位并修复内存泄漏

  4. 理解应用程序的调用树

  5. 跟踪指标随时间的变化

性能及原理

  1. 关于采集文件存储:在官方文档有说明:为避免丢失最新数据,当它检测到数据路径所在的卷接近磁盘不足,Pyroscope 将删除最旧的块。

  2. pyroscope是定时从runtime提供的几个函数获取系统指标数据,以及pprof上报是通过http上传到远程server中,只要上传和获取频率不是非常大,对业务本身影响并不大

部署

colobu.com/2022/01/27/...

为便于使用,目前采用的是Go Agent ,采用push模式,参考文档

dockerfile 复制代码
docker run -it  --name=pyroscope-go -p 4040:4040 pyroscope/pyroscope:latest server
go 复制代码
go get github.com/grafana/pyroscope-go

package main

import "github.com/grafana/pyroscope-go"

func main() {
  // These 2 lines are only required if you're using mutex or block profiling
  // Read the explanation below for how to set these rates:
  runtime.SetMutexProfileFraction(5)
  runtime.SetBlockProfileRate(5)

  pyroscope.Start(pyroscope.Config{
    ApplicationName: "simple.golang.app", //自定义应用名称

    // replace this with the address of pyroscope server
    ServerAddress:   "http://pyroscope-server:4040", //本地是http://127.0.0.1:4040

    // you can disable logging by setting this to nil
    Logger:          pyroscope.StandardLogger,

    // you can provide static tags via a map:
    Tags:            map[string]string{"hostname": os.Getenv("HOSTNAME")},

    ProfileTypes: []pyroscope.ProfileType{
      // these profile types are enabled by default:
      pyroscope.ProfileCPU,
      pyroscope.ProfileAllocObjects,
      pyroscope.ProfileAllocSpace,
      pyroscope.ProfileInuseObjects,
      pyroscope.ProfileInuseSpace,

      // these profile types are optional:
      pyroscope.ProfileGoroutines,
      pyroscope.ProfileMutexCount,
      pyroscope.ProfileMutexDuration,
      pyroscope.ProfileBlockCount,
      pyroscope.ProfileBlockDuration,
    },
  })

  // your code goes here
}

只要以上两步,访问127.0.0.1:4040你就可以看到布灵布灵的火焰图了

目前我本地只是做了pprof的收集分析,大佬们后续可以考虑线上k8s环境中借鉴七猫技术团队的方案的引入holmes,发现业务性能问题,和上报机制

本文参考文档如下:

github.com/grafana/pyr...

github.com/mosn/holmes...

相关推荐
lizhongxuan6 分钟前
Agent Sandbox: Firecracker 任务生命周期
后端
YYYing.19 分钟前
【设计模式系列 (七) 】桥接模式
c++·后端·设计模式·桥接模式
lizhongxuan1 小时前
Agent Sandbox: Firecracker 镜像、启动与 I/O
后端
打工仔折腾 AI2 小时前
飞牛OS上用Docker Compose部署ExerciseDiary运动记录并配置远程访问
运维·人工智能·后端·python·docker·容器·ai agent 实战
小蒜学长2 小时前
基于Python的动物救助站管理系统的设计与实现(代码+数据库+LW)
数据库·后端·python·django·动物救助
小小张说故事2 小时前
Python 中 __new__ 和 __init__ 到底谁才是构造函数?一文讲透区别与 5 个坑
后端·python
用户847172102842 小时前
.shot 文件格式规范
前端·后端
wang_shu_mo_ran3 小时前
Spring IoC和DI概念篇
java·后端·spring
Cheney Pan3 小时前
第4篇 Spring Boot 应用监控:Micrometer 与 JVM 指标
jvm·spring boot·后端·prometheus
阿钱真强道4 小时前
30 嵌入式操作系统 | 上位机界面:Flask 网页显示 + 控制闭环
后端·python·flask