profiler

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2025 License: MIT Imports: 12 Imported by: 1

README

Go Report Card GoDoc Build Coverage

profiler

Usage

Add the following line to your Go code:

// create and start the profiler handler
profiler.New().Start()

// ... or with custom values
profiler.New(
    profiler.WithSignal(syscall.SIGUSR1),
    profiler.WithAddress(":8080"),
    profiler.WithTimeout(15 * time.Minute),
)

Defaults

Parameter Default
Signal SIGUSR1
Listen :6666
Timeout 30m
Start the pprof endpoint
pkill -SIGUSR1 <your Go program>

After timeout the endpoint will shutdown.

Collect pprof data
go tool pprof -http $(hostname):8080 http://localhost:6666/debug/pprof/profile

... or ...

go tool pprof -http localhost:7007 http://localhost:8080/debug/pprof/profile

Kubernetes

Start the pprof endpoint
kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
...

kubectl exec -ti <your pod> sh
/ # pkill -SIGUSR1 <your Go program>
/ #

After timeout the endpoint will shutdown.

Check log
kubectl logs <your pod> -f | grep 'start debug endpoint'
Port-forward
kubectl port-forward <your pod> 8080:6666
Forwarding from 127.0.0.1:8080 -> 6666
Forwarding from [::1]:8080 -> 6666
Handling connection for 8080
Collect pprof data
go tool pprof -http $(hostname):8888 http://localhost:8080/debug/pprof/profile

... or ...

go tool pprof -http localhost:7007 http://localhost:8080/debug/pprof/profile
Use statsviz

Open: http://localhost:8080/debug/statsviz

Documentation

Overview

Package profiler implements functions to start a handler

Package profiler implements functions to start a handler

Index

Constants

View Source
const (
	InfoEvent = iota
	ErrorEvent
)

Event types

Variables

This section is empty.

Functions

This section is empty.

Types

type EventHandler added in v0.2.0

type EventHandler func(t EventType, v string, args ...any)

EventHandler function to handle log events

func DefaultEventHandler added in v0.2.0

func DefaultEventHandler() EventHandler

type EventType added in v0.2.0

type EventType int

EventType represents the event type

type Hooker

type Hooker interface {
	// PreStart will be executed after the signal was received but before the debug endpoint starts
	PreStart()
	// PostShutdown will be executed after the debug endpoint is shutdown or the start has failed
	PostShutdown()
}

Hooker represents the interface for Profiler hooks

type Option added in v0.2.0

type Option func(*Profiler)

Option is a Profiler functional option

func WithAddress

func WithAddress(address string) Option

WithAddress sets the listen address of the pprof handler

func WithEventHandler added in v0.2.0

func WithEventHandler(evt EventHandler) Option

WithEventHandler registers a custom event handler

func WithHooks

func WithHooks(hooks ...Hooker) Option

WithHooks registers the Profiler hooks

func WithSignal

func WithSignal(s os.Signal) Option

WithSignal sets the signal to activate the pprof handler

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout after the pprof handler will be shutdown

type Profiler

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

Profiler represents the Profiler

func New

func New(options ...Option) *Profiler

New returns a new profiler Defaults: - Signal : syscall.SIGUSR1 - Address: ":6666" - Timeout: 30m

func (*Profiler) Address

func (p *Profiler) Address() string

Address returns the listen address for the debug endpoint

func (*Profiler) Start

func (p *Profiler) Start(ctx context.Context)

Jump to

Keyboard shortcuts

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