zhuge

package module
v0.0.0-...-6953a06 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 11 Imported by: 0

README

诸葛

救命用的profile。

在程序CPU、内存等异常的情况下,自动生成Profile并提交到Profiler以便分析。

安装

首先,你需要部署一个 pyroscope server

安装方式很简单,官方提供各种操作系统的安装命令,并且你还可以通过Docker安装,或者部署在Kubernates环境中。

建议你配置身份认证功能,这样就避免别人恶意往你的服务器中灌数据。并且设置数据保留的期限,节省成功。

我通过rpm安装在Linux操作系统中,所以修改配置/etc/pyroscope/server.yml 如下:

retention: 24h

auth:
  # Specifies which role will be granted to a newly signed up user.
  # Supported roles: Admin, ReadOnly. Defaults to ReadOnly.
  # The parameter affects all authentication providers.
  signup-default-role: ReadOnly
  # Internal authentication provider configuration.
  internal:
    enabled: true
    signup-enabled: false
在你的应用中发送Profile

官方的pyroscope agent会定时每10秒发送profile到pyroscope server。

正如官方所说,采集profile会带来2%~5%性能的损耗,不止于此,每10秒的采样数据也会带来磁盘的占用,所以我们想在必要、紧急的情况下才上传profile。

这正是这个项目的目的。

你可以通过下面的initPyroscope方法,在你的应用程序中配置Pyroscope,注意修改你的Pyroscope server地址和token。

package main

import (
	"math/rand"
	"os"
	"time"

	"github.com/smallnest/zhuge"
	"github.com/smallnest/zhuge/examples/btree"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

var profiler *zhuge.SampleProfiler

func main() {
	initPyroscope()

	tick := time.NewTicker(10 * time.Second)

	for range tick.C {
		btree.Run(16)
	}
}

func initPyroscope() {
	sc := zhuge.Config{
		ApplicationName: "zhuge.test_client",
		ServerAddress:   "http://rpcx.io:4040",
		Tags:            map[string]string{"host": "192.168.3.1"},

		Logger: zhuge.StandardLogger,

		AuthToken: os.Getenv("PYROSCOPE_AUTH_TOKEN"),

		ProfileTypes: []zhuge.ProfileType{
			zhuge.ProfileCPU,
			zhuge.ProfileAllocObjects,
			zhuge.ProfileAllocSpace,
			zhuge.ProfileInuseObjects,
			zhuge.ProfileInuseSpace,
		},
	}
    var err error
	profiler, err = zhuge.NewSampleProfiler(sc)
	if err != nil {
		panic(err)
	}
}

然后你可以在想采样的时候执行下面的语句就可以自动采样一次了:

go profiler.SampleNow()

TODO

下一步计划是参考mosn/holmes处理逻辑,由用户设定采样的触发条件,比如CPU占用高或者内存占用高的时候自动采样上传。

欢迎一起开发它的功能。

Credits

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Labels = pprof.Labels
View Source
var StandardLogger = &standardLoggerImpl{}

Functions

func TagWrapper

func TagWrapper(ctx context.Context, labels LabelSet, cb func(context.Context))

Types

type Config

type Config struct {
	ApplicationName string // e.g backend.purchases
	Tags            map[string]string
	ServerAddress   string // e.g http://pyroscope.services.internal:4040
	AuthToken       string // specify this token when using pyroscope cloud
	SampleRate      uint32
	Logger          Logger
	ProfileTypes    []ProfileType
	DisableGCRuns   bool // this will disable automatic runtime.GC runs between getting the heap profiles
}

type LabelSet

type LabelSet = pprof.LabelSet

type Logger

type Logger interface {
	Infof(_ string, _ ...interface{})
	Debugf(_ string, _ ...interface{})
	Errorf(_ string, _ ...interface{})
}

Logger is an interface that library users can use It is based on logrus, but much smaller — That's because we don't want library users to have to implement all of the logrus's methods

type ProfileType

type ProfileType string
const (
	ProfileCPU          ProfileType = "cpu"
	ProfileInuseObjects ProfileType = "inuse_objects"
	ProfileAllocObjects ProfileType = "alloc_objects"
	ProfileInuseSpace   ProfileType = "inuse_space"
	ProfileAllocSpace   ProfileType = "alloc_space"
	DefaultSampleRate               = 100
)

type Profiler

type Profiler struct {
	// contains filtered or unexported fields
}

func Start

func Start(cfg Config) (*Profiler, error)

Start starts continuously profiling go code

func (*Profiler) Stop

func (p *Profiler) Stop() error

Stop stops continious profiling session and uploads the remaining profiling data

type SampleProfiler

type SampleProfiler struct {
	// contains filtered or unexported fields
}

func NewSampleProfiler

func NewSampleProfiler(cfg Config) (*SampleProfiler, error)

SampleProfiler creates a priofiler profiling go code

func (*SampleProfiler) SampleNow

func (sp *SampleProfiler) SampleNow()

type SampleSession

type SampleSession Session

func NewSampleSession

func NewSampleSession(c SessionConfig) (*SampleSession, error)

func (*SampleSession) SampleNow

func (ps *SampleSession) SampleNow()

type Session

type Session struct {
	// contains filtered or unexported fields
}

func NewSession

func NewSession(c SessionConfig) (*Session, error)

func (*Session) Start

func (ps *Session) Start() error

func (*Session) Stop

func (ps *Session) Stop()

type SessionConfig

type SessionConfig struct {
	Upstream       upstream.Upstream
	Logger         Logger
	AppName        string
	Tags           map[string]string
	ProfilingTypes []ProfileType
	DisableGCRuns  bool
	SampleRate     uint32
	UploadRate     time.Duration
}

Directories

Path Synopsis
examples
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL