pprofutil

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Unlicense Imports: 3 Imported by: 2

Documentation

Overview

Package pprofutil contains utilities for pprof HTTP handlers.

Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"net/url"
	"time"

	"github.com/AdguardTeam/golibs/pprofutil"
)

// must is a helper for tests.
func must[T any](v T, err error) (res T) {
	if err != nil {
		panic(err)
	}

	return v
}

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

	srv := httptest.NewServer(mux)
	defer srv.Close()

	u := must(url.Parse(srv.URL)).JoinPath(pprofutil.BasePath)
	req := must(http.NewRequest(http.MethodGet, u.String(), nil))

	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()

	req = req.WithContext(ctx)
	resp := must(http.DefaultClient.Do(req))
	respBody := must(io.ReadAll(resp.Body))

	lines := bytes.Split(respBody, []byte("\n"))
	for i := range 15 {
		fmt.Printf("%s\n", lines[i])
	}

	fmt.Println("…")

}
Output:

<html>
<head>
<title>/debug/pprof/</title>
<style>
.profile-name{
	display:inline-block;
	width:6rem;
}
</style>
</head>
<body>
/debug/pprof/
<br>
<p>Set debug=1 as a query parameter to export in legacy text format</p>
<br>
…

Index

Examples

Constants

View Source
const BasePath = "/debug/pprof/"

BasePath is the default base path used by RoutePprof.

TODO(a.garipov): Consider adding the ability to configure the base path.

Variables

This section is empty.

Functions

func RoutePprof

func RoutePprof(r Router)

RoutePprof adds all pprof handlers to r under the paths within BasePath.

Types

type Router

type Router interface {
	Handle(pattern string, h http.Handler)
}

Router is the interface for HTTP routers, such as *http.ServeMux.

type RouterFunc

type RouterFunc func(pattern string, h http.Handler)

RouterFunc is a functional implementation of the Router interface.

func (RouterFunc) Handle

func (f RouterFunc) Handle(pattern string, h http.Handler)

Handle implements the Router interface for RouterFunc.

Jump to

Keyboard shortcuts

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