apptrace

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package apptrace keeps a bounded, in-memory trace of every AWS API call that passes through the router, plus child spans for cross-service deliveries (SNS to SQS, EventBridge targets, ...) linked to the request that triggered them.

Index

Constants

View Source
const (
	KindRequest  = "request"
	KindInternal = "internal"
)

Span kinds.

View Source
const (
	DefaultCapacity = 2000
	DefaultMaxBody  = 4096
)

Defaults for Config fields left zero.

View Source
const Redacted = "[REDACTED]"

Redacted replaces every secret value.

Variables

This section is empty.

Functions

func Middleware

func Middleware(t *Tracer) echo.MiddlewareFunc

Middleware traces every API request. Metadata is always cheap; headers and bodies are captured only while Tracer.DetailEnabled.

func WithSpan

func WithSpan(ctx context.Context, s *Span) context.Context

WithSpan returns ctx carrying s as the current span.

Types

type ChildInfo

type ChildInfo struct {
	Service   string
	Operation string
	Resource  string
	Region    string
	Account   string
}

ChildInfo describes an internal delivery to trace.

type Config

type Config struct {
	// Capacity is the ring size in spans.
	Capacity int
	// MaxBody caps each captured body in bytes.
	MaxBody int
	// Enabled turns metadata recording on.
	Enabled bool
	// Detail additionally captures redacted headers and bodies.
	Detail bool
}

Config configures a Tracer.

type Detail

type Detail struct {
	RequestHeaders  map[string]string `json:"requestHeaders,omitempty"`
	ResponseHeaders map[string]string `json:"responseHeaders,omitempty"`
	Query           string            `json:"query,omitempty"`
	RequestBody     string            `json:"requestBody,omitempty"`
	ResponseBody    string            `json:"responseBody,omitempty"`
	RequestTrunc    bool              `json:"requestBodyTruncated,omitempty"`
	ResponseTrunc   bool              `json:"responseBodyTruncated,omitempty"`
}

Detail is the opt-in capture of headers and truncated, redacted bodies.

type Filter

type Filter struct {
	Service    string
	Operation  string
	Region     string
	Account    string
	Principal  string
	ErrorCode  string
	TraceID    string
	ParentID   string
	Kind       string
	Query      string
	AfterSeq   uint64
	Limit      int
	Offset     int
	MinStatus  int
	MaxStatus  int
	ErrorsOnly bool
}

Filter selects spans for Tracer.List. Zero fields match everything.

type ListResult

type ListResult struct {
	Items    []Span `json:"items"`
	Total    int    `json:"total"`
	Capacity int    `json:"capacity"`
	Recorded uint64 `json:"recorded"`
}

ListResult is a page of spans, newest first.

type Span

type Span struct {
	Start  time.Time `json:"time"`
	Detail *Detail   `json:"detail,omitempty"`

	ID            string  `json:"id"`
	TraceID       string  `json:"traceId"`
	ParentID      string  `json:"parentId,omitempty"`
	Kind          string  `json:"kind"`
	Service       string  `json:"service"`
	Operation     string  `json:"operation"`
	Resource      string  `json:"resource,omitempty"`
	Region        string  `json:"region,omitempty"`
	Account       string  `json:"account,omitempty"`
	Principal     string  `json:"principal,omitempty"`
	AccessKeyID   string  `json:"accessKeyId,omitempty"`
	Method        string  `json:"method,omitempty"`
	Path          string  `json:"path,omitempty"`
	ErrorCode     string  `json:"errorCode,omitempty"`
	LatencyMs     float64 `json:"latencyMs"`
	RequestBytes  int64   `json:"requestBytes"`
	ResponseBytes int64   `json:"responseBytes"`
	Seq           uint64  `json:"seq"`
	Status        int     `json:"status"`
	HasDetail     bool    `json:"hasDetail,omitempty"`
	// contains filtered or unexported fields
}

Span is one recorded API call or internal delivery. It is immutable once handed to Tracer.Record.

func FromContext

func FromContext(ctx context.Context) *Span

FromContext returns the current span, or nil when the request is not traced.

func StartChild

func StartChild(ctx context.Context, info ChildInfo) (context.Context, *Span)

StartChild opens a child span under the span in ctx. It returns ctx unchanged and a nil span when ctx is untraced; Span.Finish is nil-safe.

func (*Span) Finish

func (s *Span) Finish(errCode string)

Finish commits a child span. errCode is empty on success. Safe on nil.

func (*Span) SetOperation

func (s *Span) SetOperation(service, operation, resource string)

SetOperation records the service, operation and resource the service layer resolved for this request. Safe on a nil span.

type Tracer

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

Tracer is a fixed-capacity, lock-free ring of committed spans.

func New

func New(cfg Config) *Tracer

New builds a Tracer; zero Capacity and MaxBody take their defaults.

func (*Tracer) Clear

func (t *Tracer) Clear()

Clear drops every retained span.

func (*Tracer) Config

func (t *Tracer) Config() Config

Config returns the current settings.

func (*Tracer) DetailEnabled

func (t *Tracer) DetailEnabled() bool

DetailEnabled reports whether headers and bodies are captured.

func (*Tracer) Enabled

func (t *Tracer) Enabled() bool

Enabled reports whether spans are being recorded.

func (*Tracer) Get

func (t *Tracer) Get(id string) (Span, bool)

Get returns the newest span with the given id, including its Detail.

func (*Tracer) List

func (t *Tracer) List(f Filter) ListResult

List returns spans matching f, newest first, without their Detail.

func (*Tracer) Record

func (t *Tracer) Record(s *Span)

Record commits s to the ring, overwriting the oldest span when full.

func (*Tracer) Recorded

func (t *Tracer) Recorded() uint64

Recorded is the total number of spans ever committed, including evicted ones.

func (*Tracer) SetDetail

func (t *Tracer) SetDetail(v bool)

SetDetail toggles header and body capture at runtime.

func (*Tracer) SetEnabled

func (t *Tracer) SetEnabled(v bool)

SetEnabled toggles recording at runtime.

func (*Tracer) Trace

func (t *Tracer) Trace(traceID string) []Span

Trace returns every retained span of a trace, oldest first, without Detail.

Jump to

Keyboard shortcuts

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