accessprof

package module
v0.0.0-...-b1c67f2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2017 License: MIT Imports: 17 Imported by: 0

README

accessprof: Wrap http.Handler to analyze accesses

Build Status GoDoc

go get github.com/agatan/accessprof

Example

See _example/.

var prof accessprof.AccessProf
http.ListenAndServe(prof.Wrap(yourHandler, "/accessprof"))

Documentation

Overview

Example
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"regexp"
	"strings"
	"time"

	"github.com/agatan/accessprof"
	"github.com/agatan/timejump"
)

func main() {
	// Use timejump package to mock `time.Now`.
	timejump.Activate()
	defer timejump.Deactivate()
	timejump.Stop()
	timejump.Jump(time.Date(2017, 12, 2, 0, 0, 0, 0, time.UTC))

	var a accessprof.AccessProf
	exampleHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(fmt.Sprintf("Path: %s\n", r.URL.Path)))
		io.Copy(w, r.Body)
		r.Body.Close()
	})

	handler := a.Wrap(exampleHandler, "")
	server := httptest.NewServer(handler)
	defer server.Close()

	http.Get(server.URL)
	http.Get(server.URL + "/test/123")
	http.Get(server.URL + "/test/456")
	http.Post(server.URL+"/test/123", "application/json", strings.NewReader("{}"))
	http.Post(server.URL+"/test/789", "application/json", strings.NewReader(`{"key": "value"}`))

	report := handler.Report([]*regexp.Regexp{
		regexp.MustCompile(`/test/\d+`),
	})
	fmt.Print(report.String())

}
Output:

+--------+--------+-----------+-------+-----+-----+-----+-----+-----------+-----------+-----------+-----------+
| STATUS | METHOD |   PATH    | COUNT | MIN | MAX | SUM | AVG | MIN(BODY) | MAX(BODY) | SUM(BODY) | AVG(BODY) |
+--------+--------+-----------+-------+-----+-----+-----+-----+-----------+-----------+-----------+-----------+
|    200 | GET    | /         |     1 | 0s  | 0s  | 0s  | 0s  |         8 |         8 |         8 |     8.000 |
|    200 | GET    | /test/\d+ |     2 | 0s  | 0s  | 0s  | 0s  |        16 |        16 |        32 |    16.000 |
|    200 | POST   | /test/\d+ |     2 | 0s  | 0s  | 0s  | 0s  |        18 |        32 |        50 |    25.000 |
+--------+--------+-----------+-------+-----+-----+-----+-----+-----------+-----------+-----------+-----------+

Index

Examples

Constants

View Source
const (
	DefaultFlushThreshold = 1000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessLog

type AccessLog struct {
	Method           string
	Path             string
	RequestBodySize  int64
	Status           int
	ResponseBodySize int
	ResponseTime     time.Duration
	AccessedAt       time.Time
}

type AccessProf

type AccessProf struct {

	// LogFile is a filepath of the log file. (if empty, accessprof holds all logs on memory)
	LogFile        string
	FlushThreshold int
	// contains filtered or unexported fields
}

func (*AccessProf) Count

func (a *AccessProf) Count() int

func (*AccessProf) LoadAccessLogs

func (a *AccessProf) LoadAccessLogs() ([]*AccessLog, error)

func (*AccessProf) Report

func (a *AccessProf) Report(aggregates []*regexp.Regexp) *Report

func (*AccessProf) Reset

func (a *AccessProf) Reset()

func (*AccessProf) Wrap

func (a *AccessProf) Wrap(h http.Handler, reportPath string) *Handler

type Handler

type Handler struct {
	// Handler is the base handler to wrap
	Handler http.Handler
	// ReportPath is a path of HTML reporting endpoint (ignored if empty)
	ReportPath string
	*AccessProf
}

func (*Handler) ServeHTTP

func (a *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Report

type Report struct {
	Segments   []*ReportSegment
	Aggregates []*regexp.Regexp
	Since      time.Time
}

func (*Report) RenderHTML

func (r *Report) RenderHTML(w io.Writer, reportPath string) error

func (*Report) RequestCount

func (r *Report) RequestCount() int

func (*Report) String

func (r *Report) String() string

type ReportSegment

type ReportSegment struct {
	Method     string
	Path       string
	PathRegexp *regexp.Regexp
	Status     int
	AccessLogs []*AccessLog
}

func (*ReportSegment) AggregationPath

func (seg *ReportSegment) AggregationPath() string

func (*ReportSegment) AvgBody

func (seg *ReportSegment) AvgBody() float64

func (*ReportSegment) AvgResponseTime

func (seg *ReportSegment) AvgResponseTime() time.Duration

func (*ReportSegment) Count

func (seg *ReportSegment) Count() int

func (*ReportSegment) MaxBody

func (seg *ReportSegment) MaxBody() int

func (*ReportSegment) MaxResponseTime

func (seg *ReportSegment) MaxResponseTime() time.Duration

func (*ReportSegment) MinBody

func (seg *ReportSegment) MinBody() int

func (*ReportSegment) MinResponseTime

func (seg *ReportSegment) MinResponseTime() time.Duration

func (*ReportSegment) SumBody

func (seg *ReportSegment) SumBody() int

func (*ReportSegment) SumResponseTime

func (seg *ReportSegment) SumResponseTime() time.Duration

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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