Skip to content

saisrikark/pproftoggle

Repository files navigation

pproftoggle

run pprof without restarting your application

Table of Contents

Description

pprof is a tool to view resources used by go applications.
To use it we must host a http server.

Often to switch it on, users are forced to restart their application.
Resource consumption scenarios are hard to replicate due to this restart.
We would prefer not to host an extra server if not required.

This package helps us toggle the pprof server without restarting.
Some preexisting rules are provided which are implemented behind interface.

Features

Provide your own *http.Server
We will use it to host a http server while overwritting the handler

Toggle using

  • Environment Variable
    • Based on the key value pair match of an environment variable.
  • Simple yaml file
    • Yaml file key value pair match only for elements present at the root.
  • Implement your own rule
    • Rules are behind an interface so you can try your own implementation.

Installation

To use this package run the below commands

go get -d github.com/saisrikark/pproftoggle
go mod vendor

Usage

Below is an example using a simple yaml rule.
It will read a yaml file at the specified path and check if the key value pair match.
On a match pprof will be server via http.
When unmatched pprof will no longer be served.
Ensure the yaml file is created beforehand.

package main

import (
	"context"
	"log"
	"net/http"
	"sync"
	"time"

	"github.com/saisrikark/pproftoggle"
	"github.com/saisrikark/pproftoggle/rules"
)

func main() {
	var wg sync.WaitGroup
	ctx, cancel := context.WithCancel(context.Background())
	toggler, err := pproftoggle.NewToggler(
		pproftoggle.Config{
			PollInterval: time.Second * 1,
			HttpServer: &http.Server{
				Addr: ":8080",
			},
			Rules: []pproftoggle.Rule{
				rules.SimpleYamlRule{
					Key:   "enablepprof",
					Value: "true",
					Path:  "example.yaml",
				},
			},
		})
	if err != nil {
		log.Println("received error while trying to create new toggler", err)
		return
	}

	wg.Add(1)
	go func() {
		defer wg.Done()
		if err := toggler.Serve(ctx); err != nil {
			log.Println("received error while trying to serve using toggler", err)
		}
	}()
	time.Sleep(time.Minute * 10)

	cancel()
	wg.Wait()
}

Attempt to call the server.

curl https://localhost:8080/debug/pprof/heap

Refer to the examples folder for better examples.
NOTE: main.go under examples must be run from the directory it is present in.

Contributing

Just me for now 🙂

License

The code in this repository is licensed under the terms of the Apache License 2.0.

About

run pprof without restarting your application

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published