gosh

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: MIT Imports: 7 Imported by: 0

README

Go Statistics Handler

GitHub Actions codecov Go Report Card GoDoc GitHub license

About

  • The gosh is an abbreviation for Go Statistics Handler.
  • This Repository is provided following functions.
    • Go runtime statistics struct.
    • Go runtime statistics API handler.
    • Go runtime measure method.
  • You can specify the favorite JSON encoder.

Install

$ go get github.com/osamingo/gosh@latest

Usage

Example
package main

import (
	"encoding/json"
	"io"
	"log"
	"net/http"

	"github.com/osamingo/gosh"
)

func main() {

	h, err := gosh.NewStatisticsHandler(func(w io.Writer) gosh.JSONEncoder {
		return json.NewEncoder(w)
	})
	if err != nil {
		log.Fatalln(err)
	}

	mux := http.NewServeMux()
	mux.Handle("/healthz", h)

	if err := http.ListenAndServe(":8080", mux); err != nil {
		log.Fatalln(err)
	}
}
Output
$ curl "localhost:8080/healthz" | jq .
{
  "timestamp": 1527317620,
  "go_version": "go1.10.2",
  "go_os": "darwin",
  "go_arch": "amd64",
  "cpu_num": 8,
  "goroutine_num": 6,
  "gomaxprocs": 8,
  "cgo_call_num": 1,
  "memory_alloc": 422272,
  "memory_total_alloc": 422272,
  "memory_sys": 3084288,
  "memory_lookups": 6,
  "memory_mallocs": 4720,
  "memory_frees": 71,
  "stack_inuse": 491520,
  "heap_alloc": 422272,
  "heap_sys": 1605632,
  "heap_idle": 401408,
  "heap_inuse": 1204224,
  "heap_released": 0,
  "heap_objects": 4649,
  "gc_next": 4473924,
  "gc_last": 0,
  "gc_num": 0,
  "gc_per_second": 0,
  "gc_pause_per_second": 0,
  "gc_pause": []
}

License

Released under the MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewStatisticsHandler

func NewStatisticsHandler(fn func(w io.Writer) JSONEncoder) (http.Handler, error)

NewStatisticsHandler returns new StatisticsHandler.

Example
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/osamingo/gosh"
)

func newJSONEncoder(w io.Writer) gosh.JSONEncoder {
	return json.NewEncoder(w)
}

func main() {
	sh, err := gosh.NewStatisticsHandler(newJSONEncoder)
	if err != nil {
		fmt.Println(err)

		return
	}

	srv := httptest.NewServer(sh)

	defer srv.Close()

	req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL, nil)
	if err != nil {
		fmt.Println(err)

		return
	}

	resp, err := srv.Client().Do(req)
	if err != nil {
		fmt.Println(err)

		return
	}

	defer func() {
		if err := resp.Body.Close(); err != nil {
			fmt.Println(err)

			return
		}
	}()

	if resp.StatusCode != http.StatusOK {
		fmt.Println("unexpect status code:", resp.StatusCode)

		return
	}

	var stats gosh.Statistics
	if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
		fmt.Println(err)

		return
	}

	fmt.Printf("status_code: %d, has_gorutine: %t", resp.StatusCode, stats.GoroutineNum > 0)

}
Output:

status_code: 200, has_gorutine: true

Types

type JSONEncoder added in v1.1.0

type JSONEncoder interface {
	Encode(v interface{}) error
}

A JSONEncoder provides Encode method. The Encode method writes the JSON encoding of v to the stream, followed by a newline character.

type Statistics

type Statistics struct {
	Timestamp        int64     `json:"timestamp"`
	GoVersion        string    `json:"go_version"`
	GoOS             string    `json:"go_os"`
	GoArch           string    `json:"go_arch"`
	CPUNum           int       `json:"cpu_num"`
	GoroutineNum     int       `json:"goroutine_num"`
	Gomaxprocs       int       `json:"gomaxprocs"`
	CgoCallNum       int64     `json:"cgo_call_num"`
	MemoryAlloc      uint64    `json:"memory_alloc"`
	MemoryTotalAlloc uint64    `json:"memory_total_alloc"`
	MemorySys        uint64    `json:"memory_sys"`
	MemoryLookups    uint64    `json:"memory_lookups"`
	MemoryMallocs    uint64    `json:"memory_mallocs"`
	MemoryFrees      uint64    `json:"memory_frees"`
	StackInuse       uint64    `json:"stack_inuse"`
	HeapAlloc        uint64    `json:"heap_alloc"`
	HeapSys          uint64    `json:"heap_sys"`
	HeapIdle         uint64    `json:"heap_idle"`
	HeapInuse        uint64    `json:"heap_inuse"`
	HeapReleased     uint64    `json:"heap_released"`
	HeapObjects      uint64    `json:"heap_objects"`
	GCNext           uint64    `json:"gc_next"`
	GCLast           uint64    `json:"gc_last"`
	GCNum            uint32    `json:"gc_num"`
	GCPerSecond      float64   `json:"gc_per_second"`
	GCPausePerSecond float64   `json:"gc_pause_per_second"`
	GCPause          []float64 `json:"gc_pause"`
}

A Statistics has runtime information.

type StatisticsHandler

type StatisticsHandler struct {
	NewJSONEncoder func(w io.Writer) JSONEncoder
	// contains filtered or unexported fields
}

A StatisticsHandler provides runtime information handler.

func (*StatisticsHandler) MeasureRuntime

func (sh *StatisticsHandler) MeasureRuntime() Statistics

MeasureRuntime accesses runtime information.

func (*StatisticsHandler) ServeHTTP

func (sh *StatisticsHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request)

ServeHTTP implements http.Handler interface.

Jump to

Keyboard shortcuts

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