autopprof

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: Apache-2.0 Imports: 18 Imported by: 1

README

autopprof

Run tests Release

Automatically profile the Go applications when CPU or memory utilization crosses specific threshold levels against the Linux container CPU quota and memory limit.

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).

CPU Profile Report Example Memory Profile Report Example
profiling example cpu profiling example mem

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{
		CPUThreshold: 0.8, // Default: 0.75.
		MemThreshold: 0.8, // Default: 0.75.
		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.
}

You can create a custom reporter by implementing the report.Reporter interface.

Benchmark

Benchmark the overhead of watching the CPU and memory utilization. The overhead is very small, so we don't have to worry about the performance degradation.

You can run the benchmark test with this command.

./benchmark.sh
BenchmarkLightJob-5                    	49444164	       245.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkLightJobWithWatchCPUUsage-5   	48884026	       250.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkLightJobWithWatchMemUsage-5   	49036617	       246.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkHeavyJob-5                    	   59010	      203759 ns/op	       0 B/op	       0 allocs/op
BenchmarkHeavyJobWithWatchCPUUsage-5   	   58915	      204054 ns/op	       2 B/op	       0 allocs/op
BenchmarkHeavyJobWithWatchMemUsage-5   	   58850	      204764 ns/op	       2 B/op	       0 allocs/op

License

Apache 2.0

Documentation

Overview

Package autopprof is a generated GoMock package.

Package autopprof is a generated GoMock package.

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")
	ErrInvalidCPUThreshold = fmt.Errorf(
		"autopprof: cpu threshold value must be between 0 and 1",
	)
	ErrInvalidMemThreshold = fmt.Errorf(
		"autopprof: memory threshold value must be between 0 and 1",
	)
	ErrNilReporter         = fmt.Errorf("autopprof: Reporter can't be nil")
	ErrDisableAllProfiling = fmt.Errorf("autopprof: all profiling is disabled")
	ErrV2CPUQuotaUndefined = fmt.Errorf("autopprof: v2 cpu quota is undefined")
	ErrV2CPUMaxEmpty       = fmt.Errorf("autopprof: v2 cpu.max is empty")
	ErrV1CPUSubsystemEmpty = fmt.Errorf("autopprof: v1 cpu subsystem is empty")
)

Errors.

Functions

func Start

func Start(opt Option) error

Start configures and runs the autopprof process.

func Stop

func Stop()

Stop stops the global autopprof process.

Types

type Mockprofiler added in v0.2.0

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

Mockprofiler is a mock of profiler interface.

func NewMockprofiler added in v0.2.0

func NewMockprofiler(ctrl *gomock.Controller) *Mockprofiler

NewMockprofiler creates a new mock instance.

func (*Mockprofiler) EXPECT added in v0.2.0

EXPECT returns an object that allows the caller to indicate expected use.

type MockprofilerMockRecorder added in v0.2.0

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

MockprofilerMockRecorder is the mock recorder for Mockprofiler.

type Mockqueryer added in v0.2.1

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

Mockqueryer is a mock of queryer interface.

func NewMockqueryer added in v0.2.1

func NewMockqueryer(ctrl *gomock.Controller) *Mockqueryer

NewMockqueryer creates a new mock instance.

func (*Mockqueryer) EXPECT added in v0.2.1

func (m *Mockqueryer) EXPECT() *MockqueryerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

type MockqueryerMockRecorder added in v0.2.1

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

MockqueryerMockRecorder is the mock recorder for Mockqueryer.

type Option

type Option struct {
	// DisableCPUProf disables the CPU profiling.
	DisableCPUProf bool
	// DisableMemProf disables the memory profiling.
	DisableMemProf bool

	// CPUThreshold is the cpu usage threshold (between 0 and 1)
	//  to trigger the cpu profiling.
	// Autopprof will start the cpu profiling when the cpu usage
	//  is higher than this threshold.
	CPUThreshold float64

	// 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

	// ReportBoth sets whether to trigger reports for both CPU and memory when either threshold is exceeded.
	// If some profiling is disabled, exclude it.
	ReportBoth bool

	// 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