:chart_with_upwards_trend: Monitors Go MemStats + System stats such as Memory, Swap and CPU and sends via UDP anywhere you want for logging etc...

Package stats

Project status Build Status GoDoc

Package stats allows for gathering of statistics regarding your Go application and system it is running on and sent them via UDP to a server where you can do whatever you wish to the stats; display, store in database or send off to a logging service.

We currently gather the following Go related information:
  • of Garbabage collects

  • Last Garbage Collection
  • Last Garbage Collection Pause Duration
  • Memory Allocated
  • Memory Heap Allocated
  • Memory Heap System Allocated
  • Go version
  • Number of goroutines
  • HTTP request logging; when implemented via middleware
And the following System Information:
  • Host Information; hostname, OS....
  • CPU Information; type, model, # of cores...
  • Total CPU Timings
  • Per Core CPU Timings
  • Memory + Swap Information

Installation

Use go get.

go get gopkg.in/go-playground/stats.v1

or to update

go get -u gopkg.in/go-playground/stats.v1

Then import the validator package into your own code.

import "gopkg.in/go-playground/stats.v1"

Example

Server

package main

import (
	"fmt"

	"gopkg.in/go-playground/stats.v1"
)

func main() {

	config := &stats.ServerConfig{
		Domain: "",
		Port:   3008,
		Debug:  false,
	}

	server, err := stats.NewServer(config)
	if err != nil {
		panic(err)
	}

	for stat := range server.Run() {

		// calculate CPU times
		// totalCPUTimes := stat.CalculateTotalCPUTimes()
		// perCoreCPUTimes := stat.CalculateCPUTimes()

		// Do whatever you want with the data
		// * Save to database
		// * Stream elsewhere
		// * Print to console
		//

		fmt.Println(stat)
	}
}

Client

package main

import (
	"fmt"
	"net/http"
	"runtime"

	"gopkg.in/go-playground/stats.v1"
)

var statsClient *stats.ClientStats

func main() {

	serverConfig := &stats.ServerConfig{
		Domain: "remoteserver",
		Port:   3008,
		Debug:  false,
	}

	clientConfig := &stats.ClientConfig{
		Domain:           "",
		Port:             3009,
		PollInterval:     1000,
		Debug:            false,
		LogHostInfo:      true,
		LogCPUInfo:       true,
		LogTotalCPUTimes: true,
		LogPerCPUTimes:   true,
		LogMemory:        true,
		LogGoMemory:      true,
	}

	client, err := stats.NewClient(clientConfig, serverConfig)
	if err != nil {
		panic(err)
	}

	go client.Run()

	// if you want to capture HTTP requests in a middleware you'll have
	// to have access to the client.
	// see below for middleware example
	statsClient = client
}

// LoggingRecoveryHandler ...
//
//
// Middleware example for capturing HTTP Request info
// NOTE: this is standard go middlware, but could be adapted to other types/styles easily
//
func LoggingRecoveryHandler(next http.Handler) http.Handler {

	fn := func(w http.ResponseWriter, r *http.Request) {

		// log incoming request
		logReq := statsClient.NewHTTPRequest(w, r)

		defer func() {
			if err := recover(); err != nil {
				trace := make([]byte, 1<<16)
				n := runtime.Stack(trace, true)

				// log failure
				logReq.Failure(fmt.Sprintf("%s\n%s", err, trace[:n]))

				http.Error(w, "Friendly error message", 500)
				return
			}
		}()

		next.ServeHTTP(logReq.Writer(), r)

		// log completion
		logReq.Complete()
	}

	return http.HandlerFunc(fn)
}

License

  • MIT License, Copyright (c) 2015 Dean Karn
  • BSD License
    • Copyright (c) 2014, WAKAYAMA Shirou
    • Copyright (c) 2009 The Go Authors. All rights reserved.
  • BSD License, Copyright (c) 2010-2012 The w32 Authors. All rights reserved.
Owner
Go Playgound
multiple packages, libraries and programs to further the advancement of Go!
Go Playgound
Similar Resources

Best-effort CPU-local sharded values for Go

Best-effort CPU-local sharded values for Go

percpu Percpu is a Go package to support best-effort CPU-local sharded values. This package is something of an experiment. See Go issue #18802 for dis

Nov 9, 2022

Script that sets your nzxt kraken temps based on cpu temps on linux

liquidctl-cpu-temp Script that monitors cpu temps and sets cpu cooler temps according to entered fan/pump curves. Only tested on NZXT kraken z63 requi

Nov 16, 2021

Github-org-stats - Returns the star and fork count of repositories in an organisation

github-org-stats Utility to get github star and fork count of repositories in an

Sep 27, 2022

GitHub Review Stats

gh-review-stats - GitHub Review Stats A command line tool for examining review statistics for GitHub repositories. Installing Download a pre-built bin

Jul 21, 2022

A small web dashboard with stats for all pipelines of Buildkite organization.

A small web dashboard with stats for all pipelines of Buildkite organization.

Buildkite Stats A small Buildkite dashboard useful to prioritize which pipelines a Buildkite organization is waiting the most on. Noteworthy details:

Apr 25, 2022

Helps exercise your memory by giving you random tokens and poems to memorize.

memory-enhancer Helps exercise your memory by giving you random tokens and poems to memorize. Using Every day when you first open your terminal you wi

Nov 9, 2021

The package manager for macOS you didn’t know you missed. Simple, functional, and fast.

The package manager for macOS you didn’t know you missed. Simple, functional, and fast.

Stew The package manager for macOS you didn’t know you missed. Built with simplicity, functionality, and most importantly, speed in mind. Installation

Mar 30, 2022

Assembly syntax that makes you feel like you're writing code in a high-level language.

shasm Assembly syntax that makes you feel like you're writing code in a high-level language. Shasm is not an Assembler. Shasm simply compiles Shasm sy

Jun 5, 2021

A Go parser for the /etc/passwd file

godoc github.com/willdonnelly/passwd PACKAGE package passwd import "github.com/willdonnelly/passwd" FUNCTIONS func Parse() (map[string]Entr

Oct 20, 2022
Comments
  • undefined: cpu.CPUInfoStat

    undefined: cpu.CPUInfoStat

    $ go get gopkg.in/go-playground/stats.v1
    # gopkg.in/go-playground/stats.v1
    ../Go/src/gopkg.in/go-playground/stats.v1/stats.go:60: undefined: cpu.CPUInfoStat
    ../Go/src/gopkg.in/go-playground/stats.v1/stats.go:61: undefined: cpu.CPUTimesStat
    ../Go/src/gopkg.in/go-playground/stats.v1/stats.go:62: undefined: cpu.CPUTimesStat
    ../Go/src/gopkg.in/go-playground/stats.v1/stats.go:63: undefined: cpu.CPUTimesStat
    ../Go/src/gopkg.in/go-playground/stats.v1/stats.go:64: undefined: cpu.CPUTimesStat
    ../Go/src/gopkg.in/go-playground/stats.v1/stats.go:69: undefined: host.HostInfoStat
    
Related tags
Allows you to swap usernames at https://solo.to, I swapped @/city and @/lose with this program :)
Allows you to swap usernames at https://solo.to, I swapped @/city and @/lose with this program :)

Solo.To-Username-Swapper added the exe version for people who do not have Go installed, how ever I reccomend installing it at https://golang.org Getti

Sep 29, 2021
If you accept that 1 day is 24 hours in some situations, you might want to parse it in Go too.

relaxduration If you accept that 1 day is 24 hours in some situations, you might want to parse it in Go too. This package tries to handle situations w

Dec 7, 2022
Proc-peepin - Capture process cpu and memory and send it off to influx

proc-peepin Capture process cpu and memory and send it off to influx Running loc

Feb 13, 2022
A profiling tool to peek and profile the memory or cpu usage of a process
A profiling tool to peek and profile the memory or cpu usage of a process

Peekprof Get the CPU and Memory usage of a single process, monitor it live, and extract it in CSV and HTML. Get the best out of your optimizations. Us

Jan 9, 2023
Get all the swap details within the block range

go-swap-statistics get all the swap details within the block range get started git clone https://github.com/huahuayu/go-swap-statistics.git cd go-swap

Jul 22, 2022
Build for all Go-supported platforms by default, disable those which you don't want.

bagop Build for all Go-supported platforms by default, disable those which you don't want. Overview bagop is a simple build tool for Go which tries to

Jul 29, 2022
Operating system cpu scheduler algorithms

features first-come-first-serve algorithm. shortest-job-first algorithm. round-r

Dec 23, 2022
Crossjoin joins together your data from anywhere.

crossjoin Crossjoin joins together your data from anywhere. Supports PostgreSQL, Redshift, CSV data sources Zero dependency CLI, or a single Docker co

Feb 23, 2022
vtysock is a vtysh replacement that directly sends commands to the vty sockets of the daemons

vtysock vtysock is a vtysh replacement that directly sends commands to the vty sockets of the daemons. By skipping the parsing and validation checks d

Dec 18, 2022
Year-end-review - enables those want to look back on PRs at the end of year to review PRs and the comments as single Markdown file.

year-end-review year-end-review enables those want to look back on PRs at the end of year to review PRs and the comments as single Markdown file. HOW

Dec 31, 2021