monitor

package module
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: MIT Imports: 4 Imported by: 1

README

golang-ci

go mod version GoDoc goreportcard

GitHub license GitHub latest SemVer tag) GitHub release)

for what

  • this project used to gin api server status monitor

support check

  • health
  • Hardware
    • disk
    • cpu
    • ram
  • debug
    • run vars
    • pprof

env

  • minimum go version: go 1.20
  • change go 1.20, ^1.20, 1.20.14 to new go version
libs
lib url version
gin https://github.com/gin-gonic/gin v1.9.1
gopsutil https://github.com/shirou/gopsutil/ v3.23.5

Contributing

Contributor Covenant GitHub contributors

We welcome community contributions to this project.

Please read Contributor Guide for more information on how to get started.

请阅读有关 贡献者指南 以获取更多如何入门的信息

demo

make init
make dep
# ensure right then
make exampleDebug
# and open url
# health http://127.0.0.1:38000/status/health
# pprof http://127.0.0.1:38000/debug/pprof/

make exampleStatus
# status http://127.0.0.1:38000/status/hardware/disk
# status http://127.0.0.1:38000/status/hardware/ram
# status http://127.0.0.1:38000/status/hardware/cpu
# status http://127.0.0.1:38000/status/hardware/cpu_info

make examplePprof
# pprof http://127.0.0.1:38000/debug/vars
# pprof http://127.0.0.1:38000/debug/pprof/

use middleware lib

import

# go get
go get -v github.com/bar-counter/monitor/v2

# go mod find out version
go list -mod readonly -m -versions github.com/bar-counter/monitor/v2
# all use awk to get script
echo "go mod edit -require=$(go list -m -versions github.com/bar-counter/monitor | awk '{print $1 "@" $NF}')"
# then use your want version like v2.1.0
go mod edit -require=github.com/bar-counter/monitor/v2@v2.1.0
go mod download -x

gin server status

package main

import (
	"fmt"

	"github.com/bar-counter/monitor/v2"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	monitorCfg := &monitor.Cfg{
		Status: true,
		//StatusPrefix: "/status",
		StatusHardware: true,
		//StatusHardwarePrefix: "/hardware",
	}
	err := monitor.Register(r, monitorCfg)
	if err != nil {
		fmt.Printf("monitor register err %v\n", err)
		return
	}
	err = r.Run(":38000")
	if err != nil {
		fmt.Printf("run err %v\n", err)
		return
	}
}

and you can use to get status of server or run make exampleStatus

curl 'http://127.0.0.1:38000/status/health' \
  -X GET

curl 'http://127.0.0.1:38000/status/hardware/disk' \
  -X GET

curl 'http://127.0.0.1:38000/status/hardware/ram' \
  -X GET

curl 'http://127.0.0.1:38000/status/hardware/cpu' \
  -X GET

curl 'http://127.0.0.1:38000/status/hardware/cpu_info' \
  -X GET

StatusPrefix default is /status you can change by your self StatusHardwarePrefix default is /hardware

gin server debug

vars
package main

import (
	"fmt"

	"github.com/bar-counter/monitor/v2"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	monitorCfg := &monitor.Cfg{
		Status: true,
		//StatusPrefix: "/status",
		StatusHardware: true,
		//StatusHardwarePrefix: "/hardware",
		Debug: true,
		//DebugPrefix: "/debug",
		DebugMiddleware: gin.BasicAuth(gin.Accounts{
			"admin": "admin",
			"user":  "user",
		}),
	}
	err := monitor.Register(r, monitorCfg)
	if err != nil {
		fmt.Printf("monitor register err %v\n", err)
		return
	}

	err = r.Run(":38000")
	if err != nil {
		fmt.Printf("run err %v\n", err)
		return
	}
}

or run make exampleDebug because use gin.BasicAuth must add --user user:user

curl 'http://127.0.0.1:38000/debug/vars' \
 --user user:user \
 -X GET

DebugPrefix default is /debug

{
  "cgo": 6,
  "cmdline": [
    "/var/folders/79/dw7nb8rx7kgcqty_9qq2nv640000gn/T/go-build2348802398/b001/exe/debugdemo"
  ],
  "gc_pause": 0,
  "go_version": "go1.18.2",
  "goroutine": 5,
  "memstats": {
  },
  "os": "darwin",
  "os_cores": 8,
  "run_time": "8.211807521s"
}
item doc desc
cgo go doc runtime.NumCgoCall
cmdline server run cmd
cores go doc runtime.NumCPU
gc_pause count last gc time
goroutine go doc runtime.NumGoroutine
memstats go doc runtime.MemStats
os go doc runtime.GOOS
os_cores go doc runtime.NumCPU
run_time count server run time

more info see go doc expvar

DebugMiddleware can use BasicAuth or other Middleware

pprof
package main

import (
	"fmt"

	"github.com/bar-counter/monitor/v2"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	monitorCfg := &monitor.Cfg{
		Status: true,
		//StatusPrefix: "/status",
		StatusHardware: true,
		//StatusHardwarePrefix: "/hardware",
		Debug: true,
		//DebugPrefix: "/debug",
		//DebugMiddleware: gin.BasicAuth(gin.Accounts{
		//	"admin": "admin",
		//	"user":  "user",
		//}),
		PProf: true,
		//PProfPrefix: "/pprof",
	}
	err := monitor.Register(r, monitorCfg)
	if err != nil {
		fmt.Printf("monitor register err %v\n", err)
		return
	}

	err = r.Run(":38000")
	if err != nil {
		fmt.Printf("run err %v\n", err)
		return
	}
}

or run make examplePprof

then see at http://127.0.0.1:38000/debug/pprof/ or use cli to

# cpu
go tool pprof http://localhost:38000/debug/pprof/profile
# mem
go tool pprof http://localhost:38000/debug/pprof/heap
# block
go tool pprof http://localhost:38000/debug/pprof/block

License

FOSSA Status

Documentation

Index

Constants

View Source
const (
	DefaultStatusPrefix = "/status"
	// DefaultVarsPrefix url prefix of /status/hardware
	DefaultStatusHardwarePrefix = "/hardware"
	DefaultDebugPrefix          = "/debug"
	// DefaultVarsPrefix url prefix of /debug/vars
	DefaultVarsPrefix = "/vars"
	// DefaultPrefix url prefix of /debug/pprof
	DefaultPPROFPrefix = "/pprof"
)

Variables

This section is empty.

Functions

func Register

func Register(r *gin.Engine, cfg *Cfg) error

Types

type Cfg

type Cfg struct {
	APIBase              string
	Status               bool
	StatusMiddleware     gin.HandlerFunc
	StatusPrefix         string
	StatusHardware       bool
	StatusHardwarePrefix string
	Debug                bool
	DebugPrefix          string
	DebugMiddleware      gin.HandlerFunc
	VarsPrefix           string
	PProf                bool
	PProfPrefix          string
}
var DefaultCfg *Cfg

DefaultCfg config for monitor

Directories

Path Synopsis
cmd
example

Jump to

Keyboard shortcuts

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