traces

package module
v0.1.55 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 21 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 traces runs trace plugins: kinds of capture that each declare a params form and a record schema, and emit records a record store keeps.

Index

Examples

Constants

View Source
const ProfilePrefix = "traces/"

ProfilePrefix names every trace session's profile, traces/<kind>, which keys its authorization and restart.

View Source
const TruncatedColumn = "truncated"

TruncatedColumn is the column WithTruncation lists the cut values' paths in.

Variables

View Source
var (
	ErrUnknownKind   = errors.New("unknown trace kind")
	ErrInvalidParams = errors.New("invalid trace params")
)

Functions

func StreamOf added in v0.1.54

func StreamOf(ctx context.Context) string

StreamOf is the stream a capture's records are written to, from the context its Prepare and Handle run under. A kind that writes companion streams, such as bodies kept apart from its records, names them after it.

Types

type Capabilities

type Capabilities struct {
	Live       bool `json:"live,omitempty"`
	Historical bool `json:"historical,omitempty"`
	Follow     bool `json:"follow,omitempty"`
}

Capabilities say how a kind captures: Live captures what happens while a session runs, Historical reads what already happened and ends by itself, Follow goes on to capture what happens next.

type Checkpointer added in v0.1.54

type Checkpointer[P, R any] interface {
	Checkpoint(ctx dbcontext.Context, params P, records Emitter[R]) error
}

Checkpointer is a TraceHandler that takes a reading at each checkpoint its host marks in a running capture, such as the end of each step a span covers: the value of an expression, the state of a cache. Its records are committed before the checkpoint returns.

type Emitter

type Emitter[R any] interface {
	// Emit waits while the session's buffer is full, and returns ctx's error
	// once it is done.
	Emit(ctx context.Context, record R) error
	// TryEmit never waits: a record a full buffer cannot take is dropped and
	// counted, so a handler called on someone else's goroutine never stalls it.
	TryEmit(record R) bool
	// Report replaces the kind's own summary of its capture, what it counts
	// beyond the records it emits, such as events its source lost. The
	// session shows the latest report with its progress and keeps the last.
	Report(summary any)
	// Warn adds a warning the session ends with.
	Warn(message string)
	// Describe names the session by what the capture set up on its source,
	// with the statements or settings that set it up. A session records them
	// once, as it starts running, so only a Preparer's Prepare describes it.
	Describe(handle string, metadata ...query.SessionMetadata)
}

Emitter takes a handler's records.

type Handler

type Handler[P, R any] struct {
	// contains filtered or unexported fields
}

Handler is a TraceHandler as a TracePlugin, with the processing its records get before they are stored.

func NewHandler

func NewHandler[P, R any](handler TraceHandler[P, R], capabilities Capabilities) *Handler[P, R]

NewHandler makes handler a plugin with capabilities.

Example
// A minimal trace kind written outside this package: its params, its record
// schema, and a capture that emits until stopped, registered on a catalog.

package main

import (
	"fmt"
	"time"

	dbcontext "github.com/flanksource/commons-db/context"
	"github.com/flanksource/commons-db/recordstore/recordresults"

	"github.com/flanksource/commons-db/tracing/traces"
)

// HeartbeatParams is the form a client fills to start a heartbeat capture.
type HeartbeatParams struct {
	Every string `json:"every,omitempty" clicky:"title=Every"`
}

// Heartbeat is one record a heartbeat capture stores.
type Heartbeat struct {
	At   time.Time `json:"at" pretty:"label=At"`
	Beat int       `json:"beat"`
}

type heartbeats struct{}

func (heartbeats) Params() HeartbeatParams { return HeartbeatParams{Every: "1s"} }

func (heartbeats) Schema() recordresults.ResultType[Heartbeat] {
	return recordresults.ResultType[Heartbeat]{Title: "Heartbeats", TimeColumn: "at"}
}

func (heartbeats) Handle(ctx dbcontext.Context, params HeartbeatParams, records traces.Emitter[Heartbeat], _ traces.Records[Heartbeat]) error {
	every, err := time.ParseDuration(params.Every)
	if err != nil {
		return err
	}
	for beat := 1; ; beat++ {
		select {
		case <-ctx.Done():
			return nil
		case <-time.After(every):
		}
		if err := records.Emit(ctx, Heartbeat{At: time.Now().UTC(), Beat: beat}); err != nil {
			return err
		}
	}
}

func main() {
	kinds := traces.NewKinds()
	if err := kinds.RegisterKind("heartbeat", traces.NewHandler[HeartbeatParams, Heartbeat](heartbeats{}, traces.Capabilities{Live: true})); err != nil {
		panic(err)
	}
	infos, err := kinds.List()
	if err != nil {
		panic(err)
	}
	for _, info := range infos {
		fmt.Println(info.Name, info.Title, info.Params.Properties["every"].Default, len(info.Columns))
	}
}
Output:
heartbeat Heartbeats 1s 2

func (*Handler[P, R]) Capabilities

func (h *Handler[P, R]) Capabilities() Capabilities

func (*Handler[P, R]) Params

func (h *Handler[P, R]) Params() (*rpc.OpenAPISchema, error)

func (*Handler[P, R]) Schema

func (h *Handler[P, R]) Schema(kind string) (recordstore.KindSchema, error)

func (*Handler[P, R]) Title

func (h *Handler[P, R]) Title() string

func (*Handler[P, R]) ValidateParams

func (h *Handler[P, R]) ValidateParams(params json.RawMessage) error

func (*Handler[P, R]) WithDeduplication

func (h *Handler[P, R]) WithDeduplication(key func(R) string, window func(P) time.Duration) *Handler[P, R]

WithDeduplication drops a record whose key a session already emitted within the window its params give; a window of zero or less keeps every record.

func (*Handler[P, R]) WithDefaults added in v0.1.54

func (h *Handler[P, R]) WithDefaults(defaults P) *Handler[P, R]

WithDefaults starts the kind's form and its captures from defaults rather than the handler's own Params, as a host that knows its connections does.

func (*Handler[P, R]) WithJSONProcessor

func (h *Handler[P, R]) WithJSONProcessor(paths ...string) *Handler[P, R]

WithJSONProcessor parses the text fields at paths, dotted JSON names such as "response.content.text", when they hold a JSON object or array, so their fields are stored, masked and filtered as structure. Records read back through Records hold that JSON as text again. Each path must name a string field of R.

func (*Handler[P, R]) WithSecretMasking

func (h *Handler[P, R]) WithSecretMasking(keep ...string) *Handler[P, R]

WithSecretMasking masks secrets in every record (maskSecrets), never masking the keys named in keep, at any depth.

func (*Handler[P, R]) WithTruncation

func (h *Handler[P, R]) WithTruncation(maxBytes int) *Handler[P, R]

WithTruncation cuts every string in a record longer than maxBytes, listing what it cut in the record's Truncation, which R must embed.

type KindInfo

type KindInfo struct {
	Name         string             `json:"name"`
	Title        string             `json:"title"`
	Capabilities Capabilities       `json:"capabilities"`
	Params       *rpc.OpenAPISchema `json:"params"`
	Columns      []query.ColumnDef  `json:"columns"`
}

KindInfo describes one registered kind.

type Kinds

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

Kinds is the trace plugins a host serves, by kind name.

func NewKinds

func NewKinds() *Kinds

func (*Kinds) Get

func (k *Kinds) Get(name string) (TracePlugin, bool)

Get finds the plugin registered as name.

func (*Kinds) List

func (k *Kinds) List() ([]KindInfo, error)

List describes every registered kind, by name.

func (*Kinds) RegisterKind

func (k *Kinds) RegisterKind(name string, plugin TracePlugin) error

RegisterKind serves plugin as kind name, refusing a plugin whose params or schema could not be described, and a name already taken.

func (*Kinds) RegisterResultTypes

func (k *Kinds) RegisterResultTypes(registry *recordresults.Registry) error

RegisterResultTypes declares every kind's result type into registry, which is how a results store opened with it (recordresults.OpenOptions.Register) knows the kinds' schemas and serves their <prefix>/<kind> profiles.

type Preparer

type Preparer[P, R any] interface {
	Prepare(ctx dbcontext.Context, params P, records Emitter[R]) (prepared dbcontext.Context, release func() error, err error)
}

Preparer is a TraceHandler whose capture must be set up before its session runs: a subscription that must not miss a record, or a server-side session whose failure should refuse the start. Prepare runs as the session starts, before it reports running; Handle then runs under the context Prepare returns, which can carry what it set up, and release runs once Handle has returned, or at once if the capture never started. A release that fails is the session's error, its records still committed and sealed.

type Records

type Records[R any] interface {
	Scan(ctx context.Context, afterSeq int64, fn func(seq int64, record R) error) error
}

Records reads back the records a session has committed.

type Runtime

type Runtime struct {
	Kinds    *Kinds
	Sessions *query.SessionRegistry
	Probes   *probe.Manager
	// Results is the store the captures' records are committed to, opened
	// with Kinds.RegisterResultTypes.
	Results *recordresults.Results

	// PollEvery is how often a running capture's records are committed.
	// Zero is a second.
	PollEvery time.Duration
	// BufferRows caps the records a capture holds before they are committed.
	// Zero is 10,000; it is never more than one sample of the probe commits.
	BufferRows int
	// BufferBytes caps the bytes of records a capture holds before they are
	// committed, measured by their keys and text. Zero is 64MiB.
	BufferBytes int
	// contains filtered or unexported fields
}

Runtime starts captures of the kinds it serves.

func (*Runtime) Checkpoint added in v0.1.54

func (r *Runtime) Checkpoint(ctx context.Context, managed *query.ManagedSession) error

Checkpoint marks a checkpoint in a running capture: a kind that is a Checkpointer takes its reading, and the session is then sampled, so the reading and anything the capture emitted before it are committed when Checkpoint returns. The session's background polls are not checkpoints.

func (*Runtime) Restarter

func (r *Runtime) Restarter(base func() dbcontext.Context) query.RestartFunc

Restarter re-arms an ended trace session as a new capture of its kind with its params, under a context base gives the restart's request.

func (*Runtime) Start

func (r *Runtime) Start(ctx dbcontext.Context, request StartRequest) (*query.ManagedSession, error)

Start begins a capture of request.Kind into a new stream, as a managed session that ends at StopAt, on a stop, or when a historical capture is done. It refuses an unknown kind (ErrUnknownKind) and params the kind refuses (ErrInvalidParams) before any session exists.

type StartRequest

type StartRequest struct {
	Kind   string
	Params json.RawMessage
	// StopAt ends the capture; nil runs it for the registry's longest duration.
	StopAt    *time.Time
	Labels    map[string]string
	Principal string
	RestartOf string
}

StartRequest is one capture to start.

type Summary

type Summary struct {
	Emitted      int64 `json:"emitted"`
	Deduplicated int64 `json:"deduplicated,omitempty"`
	Dropped      int64 `json:"dropped,omitempty"`
	Unencodable  int64 `json:"unencodable,omitempty"`
	Collapsed    int64 `json:"collapsed,omitempty"`
	// Report is the kind's own summary, as it last reported it.
	Report any `json:"report,omitempty"`
}

Summary counts what a capture's handler emitted: the records buffered for the store, those it lost before they got there, and the copies of a key one page collapsed, which the store never saw.

type TraceHandler

type TraceHandler[P, R any] interface {
	// Params are the defaults a session starts from; P's type is the params
	// form a client renders.
	Params() P
	// Schema declares how R's records are stored and read: its title, time
	// and key columns, indexes. R's own columns are reflected from its fields.
	Schema() recordresults.ResultType[R]
	// Handle captures until ctx is done, or until its source is exhausted,
	// emitting each record. It may read back the records it already emitted.
	Handle(ctx dbcontext.Context, params P, records Emitter[R], store Records[R]) error
}

TraceHandler is a trace kind's implementation over its params P and its records R.

type TracePlugin

type TracePlugin interface {
	// Title is the kind's human name.
	Title() string
	// Params is the JSON schema of the kind's params, with their defaults: the
	// form a client renders to start a capture.
	Params() (*rpc.OpenAPISchema, error)
	// Schema is the record-store schema the kind's records are stored under
	// when it is registered as kind.
	Schema(kind string) (recordstore.KindSchema, error)
	Capabilities() Capabilities
	// ValidateParams decodes params as a session start would, refusing what
	// the session would refuse.
	ValidateParams(params json.RawMessage) error
	// contains filtered or unexported methods
}

TracePlugin is a trace kind as the kinds catalog holds it.

type Truncation

type Truncation struct {
	Truncated []string `json:"truncated,omitempty" pretty:"label=Truncated"`
}

Truncation is the column a record embeds to carry the paths of the values WithTruncation cut, which a kind that truncates must declare.

type Validator

type Validator interface {
	Validate() error
}

Validator is implemented by params that check themselves once decoded.

Directories

Path Synopsis
Package httptraffic is the http trace kind: it records the outbound HTTP exchanges commons-db makes for chosen features, one HAR entry per record.
Package httptraffic is the http trace kind: it records the outbound HTTP exchanges commons-db makes for chosen features, one HAR entry per record.
Package opensearchtraces is the opensearch trace kind: span documents of an opentelemetry connection's index, imported over a window or followed.
Package opensearchtraces is the opensearch trace kind: span documents of an opentelemetry connection's index, imported over a window or followed.
Package sqlstatements is the sql trace kind: it records the SQL statements commons-db runs on chosen connections, as the SQL statement tap publishes them.
Package sqlstatements is the sql trace kind: it records the SQL statements commons-db runs on chosen connections, as the SQL statement tap publishes them.
Package tracestest runs trace plugins in specs: a runtime over a real sqlite results store and session registry, and helpers that read a session's rows.
Package tracestest runs trace plugins in specs: a runtime over a real sqlite results store and session registry, and helpers that read a session's rows.
Package xevent is the sql_xevent trace kind: a SQL Server Extended Events capture whose events are stored as sqltrace event rows.
Package xevent is the sql_xevent trace kind: a SQL Server Extended Events capture whose events are stored as sqltrace event rows.

Jump to

Keyboard shortcuts

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