httpstats

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 8 Imported by: 1

README

go-httpstats

Usage:

go-httpstats is a Golang package to report HTTP statistics for each http.Handler. the library reports

  • HTTP request count
  • HTTP response time
  • HTTP request count for each HTTP status
  • HTTP response time for each HTTP status
  • 90, 95, 99 percentiles of HTTP response time
  • average HTTP response time

Example

_example/main.go

func main() {
	mw := stats.New()
	handler := mw.WrapHandleFunc(
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("hello world\n"))
		}))
	statsHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		d, err := mw.Data()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
		if err := json.NewEncoder(w).Encode(d); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}

	})
	http.Handle("/", handler)
	http.Handle("/stats", statsHandler)
	if err := http.ListenAndServe(":9999", nil); err != nil {
		fmt.Println(err)
	}

}

request to _example/main.go.

$ curl -s http://localhost:9999/stats | jq .
{
  "request": {
    "count": 641344,
    "status_count": {
      "200": 641344,
      "400": 0,
      "401": 0,
      "403": 0,
      "404": 0,
      "500": 0,
      "501": 0,
      "502": 0,
      "503": 0,
      "504": 0
    }
  },
  "response": {
    "max_time": 1.4884e-05,
    "min_time": 4.36e-07,
    "average_time": 7.35283e-07,
    "percentiled_time": {
      "90": 8.02e-07,
      "95": 8.94e-07,
      "99": 1.225e-06
    }
  }
}

License:

MIT License

Author:

Copyright Mercari, Inc.

Documentation

Index

Constants

View Source
const (
	// DefaultBufferSize is buf size sampling response time for percentile and average.
	DefaultBufferSize = 1000
	// DefaultSamplingFactor is factor to sample response time for percentile and average
	DefaultSamplingFactor = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Request  RequestData  `json:"request"`
	Response ResponseData `json:"response"`
}

Data is a metrics data.

type Metrics

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

Metrics is a HTTP metrics structure. a Metrics measures only one http.Handler. Metrics is needed to the number of http.Handler which you want to measure.

Metrics has buffer for measuring latest HTTP requests. default bufsize is DefaultBufferSize. if you want to modify, use NewCapa().

func New

func New() (*Metrics, error)

New returns a new statistics structure. buffer size of HTTP requests is allocated length of DefaultBufferSize.

func NewCapa

func NewCapa(bufsize, factor int) (*Metrics, error)

NewCapa returns a new statistics structure. buffer size of HTTP requests is allocated length of bufsize.

if bufsize is less than 2, NewCapa returns error.

func (*Metrics) Data

func (m *Metrics) Data() (*Data, error)

Data returns statistics Data for http.Handler you set.

func (*Metrics) WrapHandleFunc

func (m *Metrics) WrapHandleFunc(h http.Handler) http.Handler

WrapHandleFunc wraps http.Handler which you want to measure metrics.

type RequestData

type RequestData struct {
	// Count is sum of all HTTP request counts.
	Count int64 `json:"count"`

	// StatusCount is HTTP request count for each HTTP status.
	StatusCount statusCountMap `json:"status_count"`
}

RequestData is metrics of HTTP request.

type ResponseData

type ResponseData struct {
	// MaxTime is maximum response time in a period.
	MaxTime float64 `json:"max_time"`

	// MinTime is minimum response time in a period.
	MinTime float64 `json:"min_time"`

	// AverageTime is average HTTP response time of HTTP requests in buffer.
	AverageTime float64 `json:"average_time"`

	// PercentiledTime is HTTP response time percentiles of latest bufsize HTTP requests.
	PercentiledTime percentiledTimeMap `json:"percentiled_time"`
}

ResponseData is metrics of HTTP response.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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