zprof – zgo.at/zprof Index | Files | Directories

package zprof

import "zgo.at/zprof"

Package zprof serves runtime profiling data.



const (
	ReportBinary = "bin"
	ReportDebug  = "dbg"
	ReportDebug2 = "dbg2"
	ReportTop    = "top"
	ReportTraces = "traces"
	ReportSVG    = "svg"


var (
	// Prefix sets the prefix to the path you serve the profiles on, for example
	// "/profile".
	Prefix = func(prefix string) HandlerOpt {
		return func(h *Handler) { h.prefix = strings.TrimRight(prefix, "/") }

	// Auth adds HTTP Basic auth.
	Auth = func(user, passwd string) HandlerOpt {
		return func(h *Handler) { h.user, h.passwd = []byte(user), []byte(passwd) }

	// HTTPS enforces using https:// links; this can't always be reliably
	// detected.
	HTTPS = func(https bool) HandlerOpt {
		return func(h *Handler) { h.https = https }
var BlockRate *int64 = func() *int64 {
	var z int64
	return &z

BlockRate remembers the current block profile rate.

There is no way to query the block profile rate; so keep track of it here. If you call SetBlockProfileRate() in your program then you may want to set this value as well to ensure the UI is displayed correct.

Use atomic.StoreInt64() and atomic.LoadInt64() to set and get the value.


func Profile

func Profile(addr string, opts ...HandlerOpt)

Profile starts a new HTTP server on addr to profile this application.

Errors are considered non-fatal, but will be printed to stderr. "localhost:6060" will be used if addr is an empty string.

The auth can be used to set up HTTP Basic auth; the format is as "user:password". No auth is added if this is an empty string.


type Handler

type Handler struct {
	// contains filtered or unexported fields

func NewHandler

func NewHandler(opts ...HandlerOpt) Handler

NewHandler creates a new Handler instance.

func (Handler) Index

func (h Handler) Index(w http.ResponseWriter, r *http.Request) error

Index serves an overview of all profiles and metrics.

func (Handler) PProf

func (h Handler) PProf(name string) func(w http.ResponseWriter, r *http.Request) error

PProf serves pprof profiles based on the name.

Available profiles: goroutine, threadcreate, heap, allocs, block, mutex

func (Handler) Profile

func (h Handler) Profile(w http.ResponseWriter, r *http.Request) error

Profile creates a pprof formatted CPU profile.

Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified.

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (Handler) SetProfileRates

func (h Handler) SetProfileRates(w http.ResponseWriter, r *http.Request) error

SetProfileRates sets the block and mutex profile rates.

func (Handler) Symbol

func (h Handler) Symbol(w http.ResponseWriter, r *http.Request) error

Symbol looks up the program counters listed in the request, responding with a table mapping program counters to function names.

func (Handler) Trace

func (h Handler) Trace(w http.ResponseWriter, r *http.Request) error

Trace shows the execution trace in binary form.

Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified.

type HandlerOpt

type HandlerOpt func(*Handler)

Source Files



v0.0.0-20211217104121-c3c12596d8f0 (latest)
Dec 17, 2021
25 packages
Last checked
4 months ago

Tools for package owners.