events

package module
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2024 License: MIT Imports: 14 Imported by: 0

README

events

HTTP event logger and middleware.

Before:

http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("Hello world"))
})

http.ListenAndServe("127.0.0.1:8080", nil)

After:

import "github.com/bancek/events/v2"

http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("Hello world"))
})

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

handler := events.NewHTTPMiddleware(http.DefaultServeMux, logger)

http.ListenAndServe("127.0.0.1:8080", handler)

Output:

{"time":"2024-06-11T14:45:19.695142+02:00","level":"INFO","msg":"Event",
"httpSourceIp":"127.0.0.1","httpUserAgent":"curl/8.4.0","httpRespLen":11,
"requestId":"73152abd-a9fa-484f-bb75-659287254484","protocol":"http",
"httpMethod":"GET","httpUri":"/","httpRemoteAddr":"127.0.0.1:57309",
"duration":8000,"httpRespStatus":200}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AttrDuration       = "duration"
	AttrError          = "error"
	AttrErrorCause     = "errorCause"
	AttrErrorStack     = "errorStack"
	AttrHTTPMethod     = "httpMethod"
	AttrHTTPRemoteAddr = "httpRemoteAddr"
	AttrHTTPRespLen    = "httpRespLen"
	AttrHTTPRespStatus = "httpRespStatus"
	AttrHTTPSourceIP   = "httpSourceIp"
	AttrHTTPURI        = "httpUri"
	AttrHTTPUserAgent  = "httpUserAgent"
	AttrProtocol       = "protocol"
	AttrRequestID      = "requestId"
)

Functions

func GetCause

func GetCause(err error) (error, bool)

GetCause unwraps the error and returns the original error if it is not the same as the passed error.

func NewEventContext

func NewEventContext(ctx context.Context, event *Event) context.Context

func RequestIP

func RequestIP(r *http.Request, trustForwardedFor bool) string

func UnwrapAll

func UnwrapAll(err error) error

Types

type CaptureResponseWriter

type CaptureResponseWriter struct {
	http.ResponseWriter
	StatusCode     int
	ResponseLength int64
	Start          time.Time
	WriteStart     time.Time
	Hijacked       bool
	HeaderWritten  bool
}

func NewCaptureResponseWriter

func NewCaptureResponseWriter(w http.ResponseWriter) *CaptureResponseWriter

func (*CaptureResponseWriter) Duration

func (w *CaptureResponseWriter) Duration() time.Duration

func (*CaptureResponseWriter) Flush

func (w *CaptureResponseWriter) Flush()

func (*CaptureResponseWriter) Hijack

func (*CaptureResponseWriter) SetWriteDeadline added in v2.0.3

func (w *CaptureResponseWriter) SetWriteDeadline(t time.Time) error

func (*CaptureResponseWriter) Unwrap added in v2.0.2

func (*CaptureResponseWriter) Write

func (w *CaptureResponseWriter) Write(buf []byte) (int, error)

func (*CaptureResponseWriter) WriteDuration

func (w *CaptureResponseWriter) WriteDuration() time.Duration

func (*CaptureResponseWriter) WriteHeader

func (w *CaptureResponseWriter) WriteHeader(statusCode int)

type Event

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

func EventFromContext

func EventFromContext(ctx context.Context) *Event

func NewEvent

func NewEvent(logger *slog.Logger) *Event

func TryEventFromContext

func TryEventFromContext(ctx context.Context) (*Event, bool)

func (*Event) Clone added in v2.0.5

func (e *Event) Clone() *Event

func (*Event) GetAttr

func (e *Event) GetAttr(key string) any

func (*Event) Logger

func (e *Event) Logger() *slog.Logger

func (*Event) SetAttr

func (e *Event) SetAttr(key string, value any)

func (*Event) SetError

func (e *Event) SetError(err error)

type HTTPMiddleware

type HTTPMiddleware struct {
	BuildEvent         func(r *http.Request, e *Event)
	TrustForwardedFor  bool
	RequestIDHeaderKey string
	RequestLogMessage  string
	EventLogMessage    string
	LogRequest         func(e *Event)
	LogEvent           func(e *Event)
	// contains filtered or unexported fields
}

func NewHTTPMiddleware

func NewHTTPMiddleware(next http.Handler, logger *slog.Logger) *HTTPMiddleware

func (*HTTPMiddleware) ServeHTTP

func (m *HTTPMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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