pprofhijackmiddleware

package module
v0.0.0-...-cd0b1c7 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2021 License: MIT Imports: 6 Imported by: 0

README

go-pprof-hijack-middleware

pprof-hijack-middleware is a net/http middleware that gathers pprof cpu/mem profiles for the lifecycle of an HTTP request.

It is mostly intended to be used for development purposes and is useful to limit the pprof sample to the exact lifecycle of an HTTP request handler.

It hijacks the original http.ResponseWriter and returns the pprof profile data (gzipped compressed) as the HTTP response payload.

The HTTP request must generate enough load/allocs for the pprof profiling rate to pick up.

A simple "hello world" handler will, most likely, not output any useful profiling data.

There are no dependencies besides the standard lib.

Install with go get:
go get github.com/miguelcnf/go-pprof-hijack-middleware
Use as any regular net/http middleware:
mux := http.NewServeMux()

cpuProfileHijackHandler := pprofhijackmiddleware.CPUProfile(http.HandlerFunc(customHandler))
mux.Handle("/custom", cpuProfileHijackHandler)

err := http.ListenAndServe(":8080", mux)
if err != http.ErrServerClosed {
    log.Fatalf("server closed unexpectedly: %v", err)
}

See the CPU Profile Example for a running example.

Documentation

Overview

pprofhijackmiddleware provides pprof cpu/mem profile gathering as a net/http middleware.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CPUProfile

func CPUProfile(h http.Handler) http.Handler

CPUProfile gathers a pprof CPU profile during the lifecycle of the passed HTTP request handler. It hijacks the http.ResponseWriter to return a pprof compatible gzipped file as the HTTP response body.

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	pprofhijackmiddleware "github.com/miguelcnf/go-pprof-hijack-middleware"
)

var (
	customHandler = func(w http.ResponseWriter, r *http.Request) {}
	helloHandler  = func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprintf(w, "hello")
	}
)

func main() {
	mux := http.NewServeMux()

	// regular handler, not hijacked
	mux.Handle("/hello", http.HandlerFunc(helloHandler))

	// hijack a single handler
	cpuProfileHijackHandler := pprofhijackmiddleware.CPUProfile(http.HandlerFunc(customHandler))
	mux.Handle("/custom", cpuProfileHijackHandler)

	err := http.ListenAndServe(":8080", mux)
	if err != http.ErrServerClosed {
		log.Fatalf("server closed unexpectedly: %v", err)
	}
}
Output:

func MemProfile

func MemProfile(h http.Handler) http.Handler

MemProfile gathers a pprof Heap profile after the HTTP request handler is executed and a forced GC is executed. It hijacks the http.ResponseWriter to return a pprof compatible gzipped file as the HTTP response body.

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	pprofhijackmiddleware "github.com/miguelcnf/go-pprof-hijack-middleware"
)

var (
	customHandler = func(w http.ResponseWriter, r *http.Request) {}
	helloHandler  = func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprintf(w, "hello")
	}
)

func main() {
	mux := http.NewServeMux()
	mux.Handle("/hello", http.HandlerFunc(helloHandler))
	mux.Handle("/custom", http.HandlerFunc(customHandler))

	// hijack all registered handlers
	err := http.ListenAndServe(":8080", pprofhijackmiddleware.MemProfile(mux))
	if err != http.ErrServerClosed {
		log.Fatalf("server closed unexpectedly: %v", err)
	}
}
Output:

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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