telemetry

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 2 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 telemetry is the seam Bluelink traces through.

Nothing here imports OpenTelemetry. Callers depend on this interface and an implementation is installed at startup when tracing is configured, so a build that traces nothing carries no tracing machinery and every call site reads the same either way.

It lives here rather than in any one of them because the deploy engine, the blueprint library and the plugin framework all have work worth a span, and a trace is only a trace if the spans they start belong to the same one. An implementation is installed once, by whichever process is running, and every library it hosts traces through it without depending on it.

Index

Constants

View Source
const (
	TraceIDField = "traceId"
	SpanIDField  = "spanId"
)

Log field names for correlating a log line with the span it was written under.

Variables

This section is empty.

Functions

func AttrString

func AttrString(value any) string

AttrString renders an attribute's value for an implementation that can only carry strings, so that an unexpected type is recorded rather than dropped.

func SetTracer

func SetTracer(tracer Tracer)

SetTracer installs the tracer the engine traces through. A nil tracer restores the one that does nothing.

func Traced

func Traced[T any](
	ctx context.Context,
	name string,
	attrs []Attr,
	fn func(ctx context.Context, span Span) (T, error),
) (T, error)

Traced runs fn inside a span, recording an error it returns against the span.

The span is handed to fn as well as the context, so that detail only known once the work is done, a count of what it removed for instance, can be recorded on it.

A panic ends the span without recording anything. Describing it belongs to whatever recovers it, which knows what it means.

func TracedCall

func TracedCall(
	ctx context.Context,
	name string,
	attrs []Attr,
	fn func(ctx context.Context, span Span) error,
) error

TracedCall runs fn inside a span, for work that has no result to return.

Types

type Attr

type Attr struct {
	Key   string
	Value any
}

Attr is one piece of detail on a span.

The constructors below fix the value's type, so a backend is given a number as a number rather than as whatever its string happened to look like.

func BoolAttr

func BoolAttr(key string, value bool) Attr

BoolAttr records a boolean.

func FloatAttr

func FloatAttr(key string, value float64) Attr

FloatAttr records a floating-point number.

func IntAttr

func IntAttr(key string, value int64) Attr

IntAttr records an integer.

func StringAttr

func StringAttr(key string, value string) Attr

StringAttr records a string.

type RecordedSpan

type RecordedSpan struct {
	Name     string
	TraceID  string
	SpanID   string
	ParentID string
	// LinkedTo is the span named as having asked for this work, for a span started
	// through StartRoot. Empty for every other span.
	LinkedTo SpanContext
	// Root records that the span was started through StartRoot,
	// to allow for checking that a span deliberately began its own trace.
	Root  bool
	Attrs map[string]any
	Err   error
	Ended bool
}

RecordedSpan is one span a Recorder was asked for.

func (*RecordedSpan) Attr

func (s *RecordedSpan) Attr(key string) (any, bool)

Attr returns the value recorded under key, and whether it was recorded at all.

type Recorder

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

Recorder is a Tracer that keeps what it was asked to trace, for tests to assert on.

A third implementation of the seam rather than a mock of the OpenTelemetry one, so a test can state what work should have been traced without standing up an exporter.

func NewRecorder

func NewRecorder() *Recorder

NewRecorder creates a Recorder. Install it with SetTracer.

func (*Recorder) Names

func (r *Recorder) Names() []string

Names returns the names of the spans recorded so far, in order.

func (*Recorder) Span

func (r *Recorder) Span(name string) (*RecordedSpan, bool)

Span returns a snapshot of the single span recorded under the given name, and whether exactly one was.

func (*Recorder) SpanFromContext

func (r *Recorder) SpanFromContext(ctx context.Context) Span

SpanFromContext returns the span the context is carrying, so a test can exercise work that ends its span somewhere other than where it started it.

func (*Recorder) Spans

func (r *Recorder) Spans() []*RecordedSpan

Spans returns the spans recorded so far, in the order they were started.

Takes snapshots rather than the spans themselves as traced work often keeps writing to its span on another goroutine after this has returned.

func (*Recorder) Start

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

func (*Recorder) StartRoot

func (r *Recorder) StartRoot(
	ctx context.Context,
	name string,
	linkedTo SpanContext,
	attrs ...Attr,
) (context.Context, Span)

StartRoot records the span as belonging to a trace of its own, so a test can state that an operation was traced separately from the request that asked for it rather than only that both were traced.

type Span

type Span interface {
	// SetAttributes records what was learned about the work, for anything only known
	// once it has been done.
	SetAttributes(attrs ...Attr)
	// RecordError marks the work as having failed, with the error that failed it.
	RecordError(err error)
	// End closes the span. A span that is never ended is never exported.
	End()
	// Context identifies the span, for correlating it with what was logged.
	Context() SpanContext
}

Span is one unit of work in progress.

type SpanContext

type SpanContext struct {
	TraceID string
	SpanID  string
}

SpanContext identifies a span well enough to find it in a trace backend.

func LinkFrom

func LinkFrom(ctx context.Context) SpanContext

LinkFrom returns the identity of the span that ctx is carrying, for work that will be traced as a root of its own to name as having asked for it.

This will return an empty value where ctx carries no span, which is the case for work started on an interval with no external client that has made a request.

func (SpanContext) Recorded

func (c SpanContext) Recorded() bool

Recorded reports whether the span is one a backend will have.

What this answers is whether it is worth putting the IDs on a log line: an unrecorded span's IDs point at nothing, and a line carrying them reads as though a trace exists.

type Tracer

type Tracer interface {
	// Start begins a span for the named unit of work, returning a context that carries
	// it so that work nested inside becomes a child.
	Start(ctx context.Context, name string, attrs ...Attr) (context.Context, Span)
	// StartRoot begins a span for work that outlives whatever asked for it, as the root
	// of a trace of its own rather than as a child of any span ctx carries.
	//
	// A deployment runs for minutes to hours where the request asking for it lasts
	// milliseconds. Parenting one on the other would hold the request's span open for
	// the whole deployment and produce a child outliving its parent by three orders of
	// magnitude, which reads badly in every backend. So linkedTo names the span that
	// asked for the work instead, letting a backend lead from the request to the trace
	// of what it started. The zero value links to nothing, which is what an operation
	// started by a scheduled or internal path has.
	StartRoot(
		ctx context.Context,
		name string,
		linkedTo SpanContext,
		attrs ...Attr,
	) (context.Context, Span)
	// SpanFromContext returns the span ctx is carrying, or one that does nothing where
	// it carries none.
	//
	// This is for work whose span outlives the function that started it. A deployment
	// is started on one goroutine and reaches its terminal status on another, which is
	// where the outcome is known and so where the span has to be ended.
	SpanFromContext(ctx context.Context) Span
}

Tracer starts spans for units of work.

This is deliberately not HTTP-shaped, a scheduled cleanup pass and a request are both work worth a span, and only one of them has a request.

func CurrentTracer

func CurrentTracer() Tracer

CurrentTracer returns the installed tracer, or one that does nothing.

Never nil, so that a call site pays a call that does nothing rather than a branch at every point it might trace.

Jump to

Keyboard shortcuts

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