reqlog

package
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package reqlog is the live view of requests crossing a tunnel. Both ends see the same request and can report it: the hub with Middleware wrapped around its proxy, and the peer as it serves (dial.Options.RequestHook). Neither stores anything; a hook is called once per request, after the response.

Index

Constants

View Source
const (
	SkippedDisabled = "disabled"
	SkippedBinary   = "binary"
)

The reasons a body was not captured.

View Source
const DefaultRecent = 100

DefaultRecent is how many events a new watcher is replayed when the broker is built with 0. Enough to fill a console panel, small enough to forget.

View Source
const Redacted = "<redacted>"

Redacted replaces the value of a header that carries a credential. The header is still reported — knowing a request was authenticated is worth seeing — but the secret itself never reaches a viewer.

Variables

This section is empty.

Functions

func Headers added in v0.43.0

func Headers(h http.Header) map[string]string

Headers flattens headers for reporting, joining repeats the way they travel on the wire and redacting the ones that carry credentials.

func Middleware

func Middleware(hook Hook, next http.Handler, opts ...Option) http.Handler

Middleware wraps a handler so every request it serves is reported — metadata only by default; WithHeaders and WithBodyLimit add the payload, bounded.

Types

type Body added in v0.43.0

type Body struct {
	// Content is the captured prefix, empty when Skipped says why not.
	Content []byte
	// Size is how many bytes crossed in total, which is more than
	// len(Content) when Truncated.
	Size int64
	// Truncated reports that the body was longer than the limit.
	Truncated bool
	// Skipped names why nothing was captured: "disabled" (capture is
	// off), "binary" (a content type not worth showing as text), or
	// "" when Content is the answer.
	Skipped string
}

Body is what was captured of one side's payload. Capture is bounded and best-effort: a body is a prefix, a reason it was not taken, or nothing at all when there was no body to take.

type BodyCapture added in v0.43.0

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

BodyCapture accumulates up to limit bytes while counting everything that passes. The zero value captures nothing.

func CaptureRequestBody added in v0.43.0

func CaptureRequestBody(r *http.Request, limit int) *BodyCapture

CaptureRequestBody replaces r.Body with one that records up to limit bytes as the handler reads it, and returns the capture to read afterwards. A limit of 0 records nothing but still counts the size, so a report can say a body was there.

func (*BodyCapture) Body added in v0.43.0

func (c *BodyCapture) Body(contentType string) Body

Body renders what was captured, given the content type it was declared with. A nil capture is a body nobody looked at.

type Broker added in v0.37.0

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

Broker fans one Hook out to many watchers. Nothing is persisted: the last Recent events are kept in memory so a watcher arriving mid-traffic is not blank, and a watcher that falls behind loses events rather than slowing the request that produced them.

broker := reqlog.NewBroker(0)
handler := reqlog.Middleware(broker.Hook(), revproxy.New(reg))

for ev := range broker.Watch(ctx) { ... }

func NewBroker added in v0.37.0

func NewBroker(recent int) *Broker

NewBroker returns a broker replaying the last recent events to each new watcher. Pass 0 for DefaultRecent, or a negative number for no replay at all (a watcher then sees only what arrives after it).

func (*Broker) Hook added in v0.37.0

func (b *Broker) Hook() Hook

Hook returns the Hook that feeds the broker, for Middleware or dial.Options.RequestHook.

func (*Broker) Publish added in v0.37.0

func (b *Broker) Publish(ev Event)

Publish records an event and hands it to every watcher.

func (*Broker) Watch added in v0.37.0

func (b *Broker) Watch(ctx context.Context) <-chan Event

Watch streams events until ctx ends, starting with the recent ones the broker still holds (oldest first). The channel is closed when ctx does.

type Event

type Event struct {
	// At is when the response completed.
	At time.Time
	// Peer is the tunnel the request went through, filled on the hub
	// via WithPeerHeader; a peer reporting its own requests leaves it
	// empty, since it knows only one.
	Peer string
	// Method and Path are the request's, as received.
	Method string
	Path   string
	// Query is the raw query string without "?", kept apart from Path
	// so a list stays readable.
	Query string
	// Host is the authority the request carried, which is what
	// subdomain routing reads.
	Host string
	// Proto is the client's protocol ("HTTP/1.1", "HTTP/2.0").
	Proto string
	// RemoteAddr is who the reporting end saw connecting: the client
	// for a hub behind nothing, otherwise whatever proxy fronts it.
	RemoteAddr string
	// UserAgent is the request's, empty when it sent none.
	UserAgent string
	// RequestBytes is the request's declared Content-Length, -1 when
	// it did not say (a streamed or chunked body).
	RequestBytes int64
	// ResponseBytes is what the handler actually wrote.
	ResponseBytes int64
	// RequestHeaders and ResponseHeaders are the headers as they
	// crossed, with credential-carrying values redacted (see Headers).
	// Nil when the reporting end does not collect them.
	RequestHeaders  map[string]string
	ResponseHeaders map[string]string
	// RequestBody and ResponseBody are bounded captures of the
	// payloads, empty unless the reporting end was built with a body
	// limit. See Body.
	RequestBody  Body
	ResponseBody Body
	// Status is the response code, 0 when the request never got one
	// (the connection died first).
	Status int
	// Duration is how long the response took at the reporting end,
	// so the hub's includes the tunnel hop and the peer's does not.
	Duration time.Duration
}

Event is one request, seen from whichever end reports it. It is metadata only: no header values beyond the few named here, and never a body, so reporting a request costs what reading these fields costs and nothing that would have to be redacted later.

func From added in v0.39.0

func From(r *http.Request) Event

From fills the request half of an Event from r. The reporting end adds what only it knows (the peer, the outcome, the timing).

type Hook

type Hook func(Event)

Hook receives one event per request. It runs on the request's goroutine after the response, so it should not block: print, count, or hand off to a channel.

type Option added in v0.43.0

type Option func(*config)

Option configures what a Middleware or Recorder collects.

func WithBodyLimit added in v0.43.0

func WithBodyLimit(limit int) Option

WithBodyLimit captures up to limit bytes of each body, request and response. 0 (the default) captures none, which is the only setting that costs nothing and the only one that cannot leak a payload into whatever reads the events.

func WithHeaders added in v0.43.0

func WithHeaders() Option

WithHeaders reports the request and response headers, with credential-carrying values redacted (see Headers).

func WithPeerHeader added in v0.45.0

func WithPeerHeader(name string) Option

WithPeerHeader fills Event.Peer from the named request header, read once the response is done. It is how a wrapped proxy names the tunnel a request went through: the proxy normalises the routed peer onto its routing header as part of serving, so by the time the event is reported the header holds the answer.

type Recorder

type Recorder struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

Recorder captures the status code of a response while forwarding everything else, so a handler behind it keeps streaming, flushing and hijacking as it would without one.

func NewRecorder

func NewRecorder(w http.ResponseWriter, opts ...Option) *Recorder

NewRecorder wraps w. Until the handler writes, the status reads as 200, which is what net/http sends for a handler that writes a body without calling WriteHeader. With WithBodyLimit it also keeps a bounded prefix of the response, readable afterwards with Body.

func (*Recorder) Body added in v0.43.0

func (r *Recorder) Body() Body

Body is what was captured of the response, judged against the content type the handler declared.

func (*Recorder) Flush

func (r *Recorder) Flush()

Flush keeps streaming responses streaming: without it the wrapper would hide the underlying Flusher and the proxy's immediate flush would buffer.

func (*Recorder) Status

func (r *Recorder) Status() int

Status is the code the handler sent.

func (*Recorder) Unwrap

func (r *Recorder) Unwrap() http.ResponseWriter

Unwrap lets http.ResponseController reach the real writer, which the standard library looks for (hijack, deadlines).

func (*Recorder) Write

func (r *Recorder) Write(b []byte) (int, error)

func (*Recorder) WriteHeader

func (r *Recorder) WriteHeader(code int)

func (*Recorder) Written added in v0.39.0

func (r *Recorder) Written() int64

Written is how many bytes of body the handler wrote.

Jump to

Keyboard shortcuts

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