perf

package
v0.11.4 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSlowSpanThreshold = 1500 * time.Millisecond

DefaultSlowSpanThreshold is how long a root span must take before its timing tree is logged at WARN instead of DEBUG.

Root spans carry the only per-step breakdown of a hook, and they used to log exclusively at DEBUG. Real sessions run at INFO, so a hook that took seconds left behind no record of *which* step took them — the one case anybody needs the breakdown for. Escalating slow spans means a real session captures its own diagnosis without the user reproducing anything or turning on DEBUG.

1.5s sits above what a healthy hook costs and below the observed slow cluster, so fast turns stay silent instead of logging a WARN every turn.

View Source
const SlowSpanEnvVar = "ENTIRE_PERF_SLOW_MS"

SlowSpanEnvVar overrides the slow-root-span threshold, in milliseconds. A value of 0 (or a negative one) disables level escalation entirely, so every root span logs at DEBUG as it did before.

Variables

This section is empty.

Functions

This section is empty.

Types

type LoopSpan added in v0.5.1

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

LoopSpan wraps a Span that groups loop iterations. Each call to Iteration creates a child span representing one pass through the loop.

Usage:

ctx, loop := perf.StartLoop(ctx, "process_sessions")
for _, item := range items {
    iterCtx, iterSpan := loop.Iteration(ctx)
    doWork(iterCtx, item)
    iterSpan.End()
}
loop.End()

func StartLoop added in v0.5.1

func StartLoop(ctx context.Context, name string, attrs ...slog.Attr) (context.Context, *LoopSpan)

StartLoop begins a new loop span. The returned context contains the loop span and should be passed to Iteration. Call loop.End() after the loop completes.

func (*LoopSpan) End added in v0.5.1

func (l *LoopSpan) End()

End completes the loop span, auto-ending any unended iteration children first so their durations are captured at loop-end time rather than deferring to the root span's End() (which may run much later).

func (*LoopSpan) Iteration added in v0.5.1

func (l *LoopSpan) Iteration(ctx context.Context) (context.Context, *Span)

Iteration creates a child span for a single loop iteration. The caller must call End() on the returned span when the iteration completes.

type Span

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

Span tracks timing for an operation and its substeps. A Span is not safe for concurrent use from multiple goroutines.

func Start

func Start(ctx context.Context, name string, attrs ...slog.Attr) (context.Context, *Span)

Start begins a new span. If ctx already has a span, the new one becomes a child. Returns the updated context and the span. Call span.End() when the operation completes.

func (*Span) End

func (s *Span) End()

End completes the span. For root spans, emits a single log line with the full timing tree -- at DEBUG normally, or at WARN with slow=true when the span exceeded slowSpanThreshold so the breakdown survives a default-level session. For child spans, records the duration only. Safe to call multiple times -- subsequent calls are no-ops.

func (*Span) RecordError

func (s *Span) RecordError(err error)

RecordError marks the span as errored. Only the first non-nil error is stored; subsequent calls are no-ops. Call this before End() on error paths.

Jump to

Keyboard shortcuts

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