servertiming

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

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

Go to latest
Published: Mar 16, 2020 License: Apache-2.0 Imports: 9 Imported by: 3

README

HTTP Server-Timing for Gin (a HTTP web framework written in Golang)

Godoc

This is a project for myself to know more about API server in Golang and Thank you for repository go-server-timing by mitchellh. It is my first time to play with Golang and this is quite cool.

Description

This is a library to build a middleware of HTTP Server-Timing for Gin, which this header allow a server to send timing metrics from the backend to show processing times on the browser as below:

Server Timing Example

Usage

Example usage is shown below.

func main() {
    // Build router from gin-gonic/gin
	router := gin.Default()

	// Wrap handler with timing middleware
	router.Use(servertiming.Middleware())

	// Build a testing example of routing
	router.GET("/", Handler)

	// Run gin application
	router.Run()
}


func Handler(c *gin.Context) {
	// Create a wait group to wait for the response of HTTP fetches
	var wg sync.WaitGroup

	// Get timing header builder from the context
	timing := servertiming.FromContext(c)

	// Samples for testing
	for i := 0; i < 5; i++ {
		// Increment the WaitGroup counter.
		wg.Add(1)

		// Launch a goroutine to fetch the URL.
		name := fmt.Sprintf("service-%d", i)
		go func(name string) {
			// Imagine handler performing tasks in a goroutine
			defer timing.NewMetric(name).Start().Stop()
			time.Sleep(random(25, 75))

			// Decrement the counter when the goroutine completes.
			wg.Done()
		}(name)
	}

	// Imagine blocking code in handler
	m := timing.NewMetric("sql").WithDesc("SQL query").Start()
	time.Sleep(random(20, 50))
	m.Stop()

	// Wait for all HTTP fetches to complete.
	wg.Wait()

	// Write header to the response adter all fetches done
	servertiming.WriteHeader(c)

	// Create response of gin
	c.String(http.StatusOK, "Done. Check your browser inspector timing details.")
}

func random(min, max int) time.Duration {
	return (time.Duration(rand.Intn(max-min) + min)) * time.Millisecond
}

Documentation

Index

Constants

View Source
const HeaderKey = "Server-Timing"

Variables

This section is empty.

Functions

func Middleware

func Middleware() gin.HandlerFunc

func NewContext

func NewContext(c *gin.Context, h *Header)

func WriteHeader

func WriteHeader(c *gin.Context)

Types

type Header struct {
	Metrics []*Metric
	sync.Mutex
}

func FromContext

func FromContext(c *gin.Context) *Header

func ParseHeader

func ParseHeader(input string) (*Header, error)

func (*Header) Add

func (h *Header) Add(m *Metric) *Metric

func (*Header) NewMetric

func (h *Header) NewMetric(name string) *Metric

func (*Header) String

func (h *Header) String() string

type Metric

type Metric struct {
	Name     string
	Duration time.Duration
	Desc     string
	Extra    map[string]string
	// contains filtered or unexported fields
}

func (*Metric) GoString

func (m *Metric) GoString() string

func (*Metric) Start

func (m *Metric) Start() *Metric

func (*Metric) Stop

func (m *Metric) Stop() *Metric

func (*Metric) String

func (m *Metric) String() string

func (*Metric) WithDesc

func (m *Metric) WithDesc(desc string) *Metric

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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