autopprof

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2022 License: Apache-2.0 Imports: 10 Imported by: 1

README

autopprof

Automatically profile the Go applications when CPU or memory utilization crosses threshold levels.

Once you start the autopprof, the autopprof process will periodically check the CPU and memory utilization of the Go applications. If the resource utilization crosses the specified threshold for each type of resource, the autopprof will automatically profile the application (heap or cpu) and report the profiling report to the specific reporter (e.g. Slack).

profiling example

Currently, only Memory profiling is supported. We'll add CPU profiling support soon.

Installation

go get -u github.com/daangn/autopprof

Usage

If your application is running on non-linux systems, you should check the ErrUnsupportedPlatform error returned from autopprof.Start() and handle it properly.

package main

import (
	"errors"
	"log"

	"github.com/daangn/autopprof"
	"github.com/daangn/autopprof/report"
)

func main() {
	err := autopprof.Start(autopprof.Option{
		MemThreshold: 0.5,
		Reporter: report.NewSlackReporter(
			&report.SlackReporterOption{
				App:     "YOUR_APP_NAME",
				Token:   "YOUR_TOKEN_HERE",
				Channel: "#REPORT_CHANNEL",
			},
		),
	})
	if errors.Is(err, autopprof.ErrUnsupportedPlatform) {
		// You can just skip the autopprof.
		log.Println(err)
	} else if err != nil {
		log.Fatalln(err)
	}
	defer autopprof.Stop()

	// Your code here.
}

License

Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedPlatform = fmt.Errorf(
		"autopprof: unsupported platform (only Linux is supported)",
	)
	ErrCgroupsUnavailable  = fmt.Errorf("autopprof: cgroups is unavailable")
	ErrInvalidMemThreshold = fmt.Errorf(
		"autopprof: memory threshold value must be between 0 and 1",
	)
	ErrNilReporter = fmt.Errorf("autopprof: Reporter can't be nil")
)

Errors.

Functions

func Start

func Start(opt *Option) error

Start does not do anything on unsupported platforms.

Types

type Option

type Option struct {
	// MemThreshold is the memory usage threshold (between 0 and 1)
	//  to trigger the heap profiling.
	// Autopprof will start the heap profiling when the memory usage
	//  is higher than this threshold.
	MemThreshold float64

	// Reporter is the reporter to send the profiling report implementing
	//  the report.Reporter interface.
	Reporter report.Reporter
}

Option is the configuration for the autopprof.

Directories

Path Synopsis
examples module
Package report is a generated GoMock package.
Package report is a generated GoMock package.

Jump to

Keyboard shortcuts

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