traceimport

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package traceimport infers a motel topology from recorded trace data. The pipeline parses spans, reconstructs trace trees, computes per-operation statistics, and serialises the result as a topology YAML file.

YAML serialisation of inferred config using the map-based form matching the synth DSL Produces output compatible with synth.LoadConfig for round-trip validation

Recording export: serialise reconstructed trace trees to a replay sidecar.

Normalised span type and format-specific parsers for trace inference Handles stdouttrace, OTLP protobuf JSON, and Jaeger JSON formats

Per-operation statistical accumulators for duration, error rate, and call patterns Analyses trace trees to collect distributions needed for config generation

Trace tree reconstruction from flat span lists Groups spans by trace ID and links children to parents via span IDs

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatErrorRate

func FormatErrorRate(errors, total int) string

FormatErrorRate returns a percentage string like "0.10%" or empty if zero.

func MarshalConfig

func MarshalConfig(collector *StatsCollector, serviceAttrs map[string]map[string]string, traceCount int, spanCount int, windowSecs float64) ([]byte, error)

MarshalConfig produces YAML bytes from the collected statistics.

func WriteRecording added in v0.9.2

func WriteRecording(trees []*TraceTree, w io.Writer) error

WriteRecording streams trace trees to w as a newline-delimited replay recording. Each trace becomes one line, so large captures are written incrementally without buffering the whole recording in memory.

Types

type CallStats

type CallStats struct {
	Count       int
	Occurrences int
}

CallStats separates how often a call happens from how many times it happens.

Count is the number of parent invocations that made the call at least once — the probability numerator. Occurrences is the total number of call occurrences across those invocations, so the typical per-invocation repetition (the emitted count:) is round(Occurrences / Count). Keeping these apart prevents a call made several times within one invocation from being mistaken for a call made across several invocations.

type CallStyleVote

type CallStyleVote struct {
	Parallel   int
	Sequential int
}

CallStyleVote tracks parallel vs sequential votes across traces.

type Format

type Format string

Format identifies the input trace format.

const (
	FormatAuto        Format = "auto"         // Detects the format from the input.
	FormatStdouttrace Format = "stdouttrace"  // Line-delimited JSON from the OTel stdout exporter.
	FormatOTLP        Format = "otlp"         // OTLP protobuf JSON.
	FormatJaeger      Format = "jaeger"       // Jaeger JSON export format (also used by Grafana Tempo).
	FormatMetaSummary Format = "meta-summary" // Meta ATC 2023 parent-data.csv summary rows.
)

Supported trace input formats.

type OpStats

type OpStats struct {
	DurationCount int
	DurationMean  float64
	DurationM2    float64
	ErrorCount    int
	TotalCount    int
	Calls         map[string]*CallStats // key: "targetService.targetOp"
}

OpStats accumulates statistics for a single (service, operation) pair.

func (*OpStats) RecordDuration added in v0.9.1

func (o *OpStats) RecordDuration(d time.Duration, weight int)

type Options

type Options struct {
	Format           Format
	MinTraces        int
	Warnings         io.Writer // defaults to os.Stderr
	MetaProfile      string
	MetaIncludeEmpty bool
	// RecordTo, when non-nil, receives a newline-delimited replay recording of
	// the source traces alongside the inferred topology. Not supported for
	// Meta summary imports, which carry no per-trace span data.
	RecordTo io.Writer
}

Options controls import behaviour.

type Result added in v0.9.2

type Result struct {
	// YAML is the inferred synth topology.
	YAML []byte
	// TraceCount is the number of source traces. For Meta summary imports it is
	// the total weighted parent-sample count rather than a literal trace count.
	TraceCount int
	// SpanCount is the number of source spans. For Meta summary imports it is an
	// estimate derived from the weighted call counts rather than a literal span
	// count.
	SpanCount int
}

Result contains the inferred topology and source counts from an import.

For span-based imports (OTLP, stdouttrace, auto) the counts are literal. For Meta summary imports they are weighted estimates rather than observed values; see the field comments.

func Import

func Import(r io.Reader, opts Options) (Result, error)

Import reads trace spans, analyses them, and produces a synth YAML topology.

type ServiceStats

type ServiceStats struct {
	Ops        map[string]*OpStats       // operation name -> stats
	CallStyles map[string]*CallStyleVote // operation name -> voting
}

ServiceStats holds per-operation stats and call style votes for one service.

type Span

type Span struct {
	TraceID    string
	SpanID     string
	ParentID   string // empty for root spans
	Service    string
	Operation  string
	StartTime  time.Time
	EndTime    time.Time
	IsError    bool
	Attributes map[string]string
}

Span is the format-independent representation of a trace span.

func ParseSpans

func ParseSpans(r io.Reader, format Format) ([]Span, error)

ParseSpans reads spans from the given reader in the specified format. FormatAuto inspects the first JSON object to determine the format. Whole-document JSON inputs are limited to 256 MB to prevent OOM on large trace exports. Explicit line-delimited stdouttrace inputs are scanned incrementally.

type SpanNode

type SpanNode struct {
	Span     Span
	Children []*SpanNode
}

SpanNode wraps a Span with its children in the trace tree.

type StatsCollector

type StatsCollector struct {
	Services map[string]*ServiceStats // service name -> stats
}

StatsCollector accumulates statistics across all traces.

func NewStatsCollector

func NewStatsCollector() *StatsCollector

NewStatsCollector creates an empty collector.

func (*StatsCollector) CollectFromTrees

func (c *StatsCollector) CollectFromTrees(trees []*TraceTree)

CollectFromTrees walks all trace trees, accumulating per-operation statistics.

type TraceTree

type TraceTree struct {
	TraceID  string
	Roots    []*SpanNode
	AllNodes []*SpanNode
}

TraceTree holds spans grouped by trace with parent-child links.

func BuildTrees

func BuildTrees(spans []Span, w io.Writer) []*TraceTree

BuildTrees reconstructs trace trees from a flat list of spans. Spans with broken parent references (parent not in dataset) become additional roots. Warnings about orphans are written to w (may be nil).

Jump to

Keyboard shortcuts

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