agenttest

package
v2.11.0-dev.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package agenttest provides a mock APM agent for inspecting traces in tests.

The agent collects spans sent via its in-process HTTP transport and exposes them for assertions. No real networking is involved, allowing for flushes to be deterministic and immediate.

agent := agenttest.New()
agent.HandleTraces("/v0.4/traces", myHandler)
agent.Start(t)
// ... start tracer with agent.Addr() / agent.Transport() ...
// ... create spans, flush ...
span := agent.RequireSpan(t, agenttest.With().Operation("http.request"))

By design, this API does not expose span slices or iterators. Order-dependent assertions are a common source of test flakiness; any future iterator must randomize its traversal order.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Agent

type Agent interface {
	// Info returns the agent info configuration (e.g. sampling rates).
	Info() *Info
	// HandleTraces registers a handler that decodes traces arriving at the given
	// HTTP pattern (e.g. "/v0.4/traces") and stores the resulting spans.
	HandleTraces(string, TraceHandler)

	// Start initializes the agent. Call this before starting the tracer.
	Start(testing.TB) error
	// Addr returns the agent address to pass to the tracer via WithAgentAddr.
	Addr() string
	// Transport returns an optimized transport for interacting with the agent.
	Transport() http.RoundTripper

	// FindSpan returns the first collected span matching all provided conditions,
	// or nil if none is found.
	FindSpan(...*SpanMatch) *Span
	// RequireSpan returns the first collected span matching all provided conditions.
	// It fails the test immediately if no matching span is found.
	RequireSpan(testing.TB, ...*SpanMatch) *Span
	// CountSpans returns the total number of spans collected so far.
	CountSpans() int
}

Agent is a mock APM agent that collects spans in-process for test assertions.

func New

func New() Agent

New creates a new mock agent. Register trace handlers with HandleTraces before calling Start.

type Info

type Info struct {
	Rates map[string]float64 `json:"rate_by_service"`
}

Info holds agent configuration returned to the tracer on flush responses, such as per-service sampling rates.

func (*Info) RateByService

func (i *Info) RateByService(service, env string, rate float64)

RateByService sets the sampling rate for a given service/env pair. The tracer uses these rates when making sampling decisions.

type Span

type Span struct {
	SpanID uint64
	// TraceID holds the lower 64 bits of the trace ID. For 128-bit trace IDs
	// (which the tracer generates when DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED
	// is set) the upper 64 bits are available in Meta["_dd.p.tid"]. Assertions on
	// full 128-bit identity should use that tag rather than this field.
	TraceID   uint64
	ParentID  uint64
	Service   string
	Operation string
	Resource  string
	Type      string
	Start     int64 // unix nanoseconds
	Duration  int64 // nanoseconds
	Error     int32
	Meta      map[string]string
	Metrics   map[string]float64
	Tags      map[string]any // merged view: meta + metrics + top-level attrs
	Children  []*Span
}

Span holds the data of a single collected span. Meta and Metrics contain the raw string and numeric tags respectively; Tags is a merged view of both plus top-level attributes (name, service, resource, type) for convenience.

type SpanMatch

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

SpanMatch is a builder for span matching conditions. Create one with With and chain methods to add conditions. Pass the result to Agent.FindSpan or Agent.RequireSpan.

func With

func With() *SpanMatch

With returns a new empty SpanMatch builder. Chain methods like Service, Operation, Tag, etc. to add matching conditions.

func (*SpanMatch) Condition

func (m *SpanMatch) Condition(desc string, fn func(*Span) bool) *SpanMatch

Condition adds an arbitrary predicate condition with a human-readable description used in diagnostic output when the condition fails.

func (*SpanMatch) FailedConditions

func (m *SpanMatch) FailedConditions(s *Span) []string

FailedConditions returns human-readable descriptions of conditions that did not match the given span, for use in diagnostic output.

func (*SpanMatch) Matches

func (m *SpanMatch) Matches(s *Span) bool

Matches reports whether the span satisfies all conditions in this SpanMatch.

func (*SpanMatch) Operation

func (m *SpanMatch) Operation(operation string) *SpanMatch

Operation adds a condition that the span's operation name must equal the given value.

func (*SpanMatch) ParentOf

func (m *SpanMatch) ParentOf(parentID uint64) *SpanMatch

ParentOf adds a condition that the span's parent ID must equal the given value.

func (*SpanMatch) Resource

func (m *SpanMatch) Resource(resource string) *SpanMatch

Resource adds a condition that the span's resource must equal the given value.

func (*SpanMatch) Service

func (m *SpanMatch) Service(service string) *SpanMatch

Service adds a condition that the span's service must equal the given value.

func (*SpanMatch) Tag

func (m *SpanMatch) Tag(key string, value any) *SpanMatch

Tag adds a condition that the span's merged Tags map must contain the given key with the given value.

func (*SpanMatch) Type

func (m *SpanMatch) Type(spanType string) *SpanMatch

Type adds a condition that the span's type must equal the given value.

type TraceHandler

type TraceHandler func(io.Reader) []*Span

TraceHandler decodes trace data from an io.Reader and returns the decoded spans. Implementations handle a specific wire format (e.g. msgpack v0.4 or binary v1.0).

Jump to

Keyboard shortcuts

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