pprofsnapshot

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2022 License: MIT Imports: 12 Imported by: 0

README

pprofsnapshot

pprofsnapshot makes it easy to download profiling data to local.

Usages

pprofsnapshot packages profiles as *.tar.gz archived, it offers the std http.Handle which can be embedded in your code easily.

package main

import (
	"net/http"

	"github.com/chenjiandongx/pprofsnapshot"
)

func main() {
	// http handler provides some query params similar to the `http/pprof`
	// * seconds
	// * debug
	// * profiles: goroutine/threadcreate/heap/allocs/block/mutex/cpu, split by comma
	http.Handle("/debug/pprof/snapshot", pprofsnapshot.HandlerFor())
    // 
	// http.HandleFunc("/debug/pprof/snapshot", pprofsnapshot.HandlerFuncFor())
	http.ListenAndServe(":8080", nil)
}

/*
for example:

> $ curl http://localhost:8080/debug/pprof/snapshot?debug=1&seconds=15&profiles=heap,cpu,goroutine -o profiles.tar.gz
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100  3143    0  3143    0     0    209      0 --:--:--  0:00:15 --:--:--   658

> $ tar -zxvf profiles.tar.gz
x profiles/heap.pprof
x profiles/cpu.pprof
x profiles/goroutine.pprof
*/

Or just leverage the Collector to capture profiling data anywhere like this.

// 1) Collect API
collector := pprofsnapshot.NewCollector()
b, err := collector.Collect(context.Background())
if err != nil {
	// handles error
}
f, err := os.Create("profiles.tar.gz")
if err != nil {
	// handles error
}
f.Write(b)

// 2) Write API
collector := pprofsnapshot.NewCollector()
f, err := os.Create("profiles.tar.gz")
if err != nil {
	// handles error
}
collector.Write(context.Background(), f)
Options
// WithDebugLevel sets profiles debug level
WithDebugLevel(level int) Option

// WithEnabledProfiles sets collected profiles.
// supports goroutine/threadcreate/heap/allocs/block/mutex/cpu
WithEnabledProfiles(profiles []string) Option

// WithSamplingSeconds sets sampling duration in seconds.
WithSamplingSeconds(n int) Option
License

MIT ©chenjiandongx

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandlerFor added in v0.2.1

func HandlerFor(opts ...Option) http.Handler

HandlerFor returns the http.Handler for the profiles snapshots collected.

func HandlerFuncFor added in v0.2.1

func HandlerFuncFor(opts ...Option) http.HandlerFunc

HandlerFuncFor returns the http.HandlerFunc for the profiles snapshots collected.

Types

type Collector

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

func NewCollector

func NewCollector(opts ...Option) *Collector

func (*Collector) Collect

func (c *Collector) Collect(ctx context.Context) ([]byte, error)

Collect returns the compressed tarball data in bytes

func (*Collector) Write

func (c *Collector) Write(ctx context.Context, w io.Writer) (int, error)

Write writes collected data to the io.Writer.

type Option

type Option func(o *option)

func WithDebugLevel

func WithDebugLevel(level int) Option

WithDebugLevel sets profiles debug level

func WithEnabledProfiles

func WithEnabledProfiles(profiles []string) Option

WithEnabledProfiles sets collected profiles. supported: goroutine/threadcreate/heap/allocs/block/mutex/cpu

func WithSamplingSeconds

func WithSamplingSeconds(n int) Option

WithSamplingSeconds sets sampling duration in seconds.

Jump to

Keyboard shortcuts

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