flightrecordermw

package module
v0.0.0-...-1f772fa Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

Flight Recorder middleware

This repo contains a middleware that uses the FlightRecorder from x/exp/trace to record trace data from a running Go application.

Usage

To setup the middleware, you need to provide a directory where the trace data will be stored, a FlightRecorder instance and a function that will be called for each request to decide if the trace data should be recorded.

fr := trace.NewFlightRecorder()
fr.Start()

frmw, err := flightrecordermw.New("/tmp", fr, func(r *http.Request, stats flightrecordermw.Stats) bool {
    return stats.Elapsed > 100*time.Millisecond
})
if err != nil {
    return err
}

To use the middleware, you need to wrap your handler with it.

mux.Handle("/hello", frmw.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello, World!"))
})))

That's it!

Handler

The middleware also provides a handler that can be used to serve the trace data.

mux.Handle("/traces/", http.StripPrefix("/traces/", frmw))

You will find a listing of all the available traces in the directory provided when creating the middleware.

Example

You can find a complete example in the example directory.

License (Apache 2)

This project is licensed under the Apache 2.0 License - see the LICENSE file for details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(dstDir string, fr *trace.FlightRecorder, shouldRecord ShouldRecord) (*frmw, error)

New returns a new FlightRecorder middleware and handler.

Types

type ShouldRecord

type ShouldRecord func(r *http.Request, stats Stats) bool

ShouldRecord is a function that returns true if the request should be recorded.

type Stats

type Stats struct {
	StatusCode int
	Elapsed    time.Duration
}

Stats contains the statistics of a request.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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