serverstats

package
v6.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

README

Server Stats

Server Stats provides a structure and Echo handler for tracking server statistics, such as when the server started, number of requests, and the count of various status codes. Here is an example of using this in an Echo application.

httpServer = echo.New()

/*
 * Server stats middleware
 */
serverStats := serverstats.NewServerStats(nil)
httpServer.Use(serverStats.Middleware)

httpServer.GET("/serverstats", serverStats.Handler)

The ServerStats object satifies the IServerStats interface, allowing developers to implement their own statistics. It also provides the ability to provide a callback method to add custom stats per application. This is done by passing a method to the NewServerStats constructor method. Inside this method you can work with a map called CustomStats. This is a map of string keys with interfaces for values.

httpServer = echo.New()

/*
 * Server stats middleware
 */
serverStats := serverstats.NewServerStats(func(ctx echo.Context, serverStats *ServerStats) {
	var ok bool
	var value int

	if _, ok = serverStats.CustomStats["someCounter"]; !ok {
		serverStats.CustomStats["someCounter"] = 0;
	}

	value = serverStats.CustomStats["someCounter"].(int)
	value++

	serverStats.CustomStats["someCounter"] = value
})
httpServer.Use(serverStats.Middleware)

httpServer.GET("/serverstats", serverStats.Handler)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IServerStats

type IServerStats interface {
	GetAverageResponseTimeGraph(precision ResponseTimePrecision) ResponseTimeGraphCollection
	Handler(ctx echo.Context) error
	Middleware(next echo.HandlerFunc) echo.HandlerFunc
}

IServerStats defines an interface for capturing and retrieving server statistics

type ResponseTime

type ResponseTime struct {
	ExecutionTime time.Duration
	Time          time.Time
}

ResponseTime is used to track how much time a request took to execute, and what time (of day) it happened

type ResponseTimeGraph

type ResponseTimeGraph struct {
	AverageResponseTimeInNanoseconds  int64  `json:"averageResponseTimeInNanoseconds"`
	AverageResponseTimeInMicroseconds int64  `json:"averageResponseTimeInMicroseconds"`
	AverageExecutionTimeMilliseconds  int64  `json:"averageExecutionTimeMilliseconds"`
	Time                              string `json:"time"`
}

ResponseTimeGraph reports average response times for a given date/time

type ResponseTimeGraphCollection

type ResponseTimeGraphCollection []*ResponseTimeGraph

ResponseTimeGraphCollection is a collection of ResponseTimeGraph structs

type ResponseTimePrecision

type ResponseTimePrecision int

ResponseTimePrecision describes how granular to report response times

const (
	// PrecisionHour reports responses times averaged by hour
	PrecisionHour ResponseTimePrecision = 1

	// PrecisionDay reports respose times averaged by day
	PrecisionDay ResponseTimePrecision = 2

	// PrecisionMonth reports response times averaged by month
	PrecisionMonth ResponseTimePrecision = 3
)

type ServerStats

type ServerStats struct {
	AverageFreeSystemMemory *ring.Ring
	AverageMemoryUsage      *ring.Ring
	CustomStats             map[string]interface{} `json:"customStats"`
	Uptime                  time.Time              `json:"uptime"`
	RequestCount            uint64                 `json:"requestCount"`
	ResponseTimes           *ring.Ring
	StatsByDayCollection    StatsByDayCollection
	Statuses                map[string]int `json:"statuses"`

	sync.RWMutex
	// contains filtered or unexported fields
}

ServerStats tracks general server statistics. This includes information about uptime, response times and counts, and requests counts broken down by HTTP status code. ServerStats is thread-safe due to a write lock on requests, and a read lock on reads

func NewServerStats

func NewServerStats(customMiddleware func(ctx echo.Context, serverStats *ServerStats)) *ServerStats

NewServerStats creates a new ServerStats object

func NewServerStatsWithOptions

func NewServerStatsWithOptions(options ServerStatsOptions, customMiddleware func(ctx echo.Context, serverStats *ServerStats)) *ServerStats

func (*ServerStats) GetAverageResponseTimeGraph

func (s *ServerStats) GetAverageResponseTimeGraph(precision ResponseTimePrecision) ResponseTimeGraphCollection

GetAverageResponseTimeGraph returns an array of response time objects. The precision specifies at what interval you wish to get data for. For example, passing Hour gets you response times averaged by hour. Passing Day gets you response times averaged by day

func (*ServerStats) Handler

func (s *ServerStats) Handler(ctx echo.Context) error

Handler is an endpoint handler you can plug into your application to return stat data

func (*ServerStats) Middleware

func (s *ServerStats) Middleware(next echo.HandlerFunc) echo.HandlerFunc

Middleware is used to capture request and response stats. This is designed to be used with the Echo framework

func (*ServerStats) NewMiddlewareWithTimeTracking

func (s *ServerStats) NewMiddlewareWithTimeTracking() echo.MiddlewareFunc

NewMiddlewareWithTimeTracking returns a middleware that tracks stats by day and hour. You provide it a pointer to a StatsByDayCollection and this will update stats grouped by day (starting at midnight) and hour.

TODO: Perhaps keep the statsByDayCollection in ServerStats locally. cause otherwise this ain't working

type ServerStatsOptions

type ServerStatsOptions struct {
	NumMemStatsToKeep      int
	NumResponseTimesToKeep int
}

type StatsByDay

type StatsByDay struct {
	Date        time.Time             `json:"date"`
	HourlyStats StatsByHourCollection `json:"hourlyStats"`
}

func NewStatsByDay

func NewStatsByDay(date time.Time) *StatsByDay

type StatsByDayCollection

type StatsByDayCollection []*StatsByDay

type StatsByHour

type StatsByHour struct {
	Hour                              int                    `json:"hour"`
	AverageFreeMemory                 uint64                 `json:"averageFreeMemory"`
	AverageFreeMemoryPretty           string                 `json:"averageFreeMemoryPretty"`
	AverageMemoryUsage                uint64                 `json:"averageMemoryUsage"`
	AverageMemoryUsagePretty          string                 `json:"averageMemoryUsagePretty"`
	AverageResponseTimeInNanoseconds  int64                  `json:"averageResponseTimeInNanoseconds"`
	AverageResponseTimeInMicroseconds int64                  `json:"averageResponseTimeInMicroseconds"`
	AverageResponseTimeInMilliseconds int64                  `json:"averageResponseTimeInMilliseconds"`
	CustomStats                       map[string]interface{} `json:"customStats"`
	RequestCount                      uint64                 `json:"requestCount"`
	Statuses                          map[string]int         `json:"statuses"`

	sync.RWMutex `json:"-"`
}

func NewStatsByHour

func NewStatsByHour(hour int) *StatsByHour

func (*StatsByHour) Calculate

func (sbh *StatsByHour) Calculate(s *ServerStats)

type StatsByHourCollection

type StatsByHourCollection []*StatsByHour

Jump to

Keyboard shortcuts

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