Help us shape the future of Go! Take the 2026 Go Developer Survey

telemetry

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package telemetry binds github.com/titpetric/oida into the phpscript namespace: traces and spans recorded in process, with a server side rendered front end mounted at /debug/oida.

This is the only package in phpscript that imports oida. Everything else instruments through the symbols bound here, so no call site names the provider. The bindings are type aliases and thin wrappers, so a *telemetry.Span is a *oida.Span: nothing is copied or adapted at runtime.

That covers the call sites, not the whole dependency. The recorder and the front end belong to the host platform, which names oida itself, so replacing the provider means replacing it there as well. A host hands over the tracer that platform built:

var recorder *platform.TelemetryModule
if svc.Find(&recorder) {
	module = telemetry.NewModule(recorder.Tracer())
}

The module is a runner.Observer, so a Runtime handed to it reports its scoreboard state and its spans onto the trace of the request that is running:

rt.SetContext(r.Context())
rt.Observe(module)

Instrumentation is nil safe. Spans started without a trace in the context, or in a process where telemetry is disabled, return a nil span whose methods do nothing, so instrumented code runs unchanged either way.

Index

Constants

View Source
const (
	KindInternal = oida.KindInternal
	KindHTTP     = oida.KindHTTP
	KindDatabase = oida.KindDatabase
	KindExternal = oida.KindExternal
	KindTemplate = oida.KindTemplate
	KindCache    = oida.KindCache
	KindQueue    = oida.KindQueue
)

Span kinds. The set is open: an unrecognized value is valid, which is what lets PHP pass a plain string.

View Source
const (
	StateWaiting    = oida.StateWaiting
	StateStarting   = oida.StateStarting
	StateReading    = oida.StateReading
	StateProcessing = oida.StateProcessing
	StateWriting    = oida.StateWriting
	StateKeepalive  = oida.StateKeepalive
	StateClosing    = oida.StateClosing
	StateError      = oida.StateError
)

Scoreboard states of a trace in flight. The one-character values follow the convention used by servers such as lighttpd.

View Source
const (
	// DefaultPath is the mount path of the debug front end.
	DefaultPath = oida.DefaultPath

	// RequestIDHeader carries the trace identifier on the request and the
	// response.
	RequestIDHeader = oida.RequestIDHeader

	// BackgroundHost is the host label of traces that did not arrive over the
	// network: startup steps, cron ticks, queue consumers. oida stopped
	// re-exporting it from its root package; the constant itself is still the
	// recorded data's, so this reads it from model rather than inventing a
	// second label for the same group.
	BackgroundHost = model.BackgroundHost
)

Variables

View Source
var (
	ErrNilRouter         = oida.ErrNilRouter
	ErrInvalidOptions    = oida.ErrInvalidOptions
	ErrInvalidPath       = oida.ErrInvalidPath
	ErrInvalidSampleRate = oida.ErrInvalidSampleRate
	ErrTraceNotFound     = oida.ErrTraceNotFound
	ErrDisabled          = oida.ErrDisabled
)

Recording and configuration failures. Every configuration failure wraps ErrInvalidOptions.

Functions

func Do

func Do(ctx context.Context, name string, fn func(context.Context) error, kind ...Kind) error

Do runs fn inside a span, records the returned error on it and ends it. The error is returned unchanged.

func Mount

func Mount(r Router, tracer *Tracer) error

Mount registers the debug front end on r under Options.Path, serving the traces of that tracer. oida takes the tracer itself now, there being no process wide one to resolve from options.

func Recordable added in v0.3.1

func Recordable(err error) bool

Recordable reports whether an error is worth failing a span over. A query that found no rows and a request the client hung up on are control flow, not failures, and marking them would fail the trace and the recorded SLA with it.

It is the shared answer to a question every traced binding asks, so that a cache miss and a canceled request read the same way on the front end whether they came from a query or from session storage.

func SpanSource

func SpanSource(ctx context.Context) (string, int)

SpanSource returns the source location carried by ctx. Both results are zero when no PHP frame published one.

func TraceHost

func TraceHost(trace Trace) string

TraceHost returns the host a trace belongs to. Background traces did not arrive over the network and have no host, so they group under BackgroundHost. oida dropped the function along with the rest of its view model; the grouping is still what the server's own reporting reads by, so it is stated here rather than at each caller.

func TraceID

func TraceID(ctx context.Context) string

TraceID returns the identifier of the trace in ctx, or an empty string. It is the value of the Request-Id header for HTTP traces, which makes it the cheapest correlation key for logs.

func WithSpanFilename

func WithSpanFilename(ctx context.Context, filename string) context.Context

WithSpanFilename associates spans started from ctx with a source file.

func WithSpanLine

func WithSpanLine(ctx context.Context, line int) context.Context

WithSpanLine associates spans started from ctx with a source line.

func WithTrace

func WithTrace(ctx context.Context, t *Trace) context.Context

WithTrace returns a context carrying the trace. Spans started from it, or from any context derived from it, are recorded on that trace.

Types

type Attributes

type Attributes = oida.Attributes

Attributes is a set of key/value pairs recorded on a span.

type Kind

type Kind = oida.Kind

Kind classifies the work a span measured.

type Module

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

Module observes the PHP interpreter and records what it reports onto the trace of the request that is running: the scoreboard state, the entrypoint it resolved to, and one span per include, call or template.

It is not a recorder. The host platform registers one, and the tracing middleware that recorder installs is what puts a trace in the request context; this type only writes onto it. That is why interpreter work shows up on the platform's debug front end without phpscript mounting one.

func NewModule

func NewModule(tracer *Tracer) *Module

NewModule returns an observer recording into tracer, which is the tracer the host recorder built. A nil tracer is valid and means nothing is recorded: every call below is nil safe, so instrumented code runs unchanged either way.

func (*Module) Snapshot

func (m *Module) Snapshot() Snapshot

Snapshot returns a race free copy of everything recorded so far.

func (*Module) Trace

func (m *Module) Trace(ctx context.Context, message string, kind ...Kind) *Span

Trace implements runner.Observer: it records one span of interpreter work, such as an include, a call or a template, on the running trace.

func (*Module) Tracer

func (m *Module) Tracer() *Tracer

Tracer returns the tracer the module records into.

func (*Module) TrackLifecycle

func (m *Module) TrackLifecycle(ctx context.Context, name, filename string, run func(context.Context) error) error

TrackLifecycle records work that did not arrive over the network, such as a @startup file, as a trace of its own. There is no request to record onto, so this is the one place the observer starts a trace rather than writing to one.

func (*Module) UpdateFilename

func (m *Module) UpdateFilename(ctx context.Context, filename string)

UpdateFilename records the PHP entrypoint of the running request. Included files do not replace it: the entrypoint is the file the request resolved to.

func (*Module) UpdateIncludedFiles

func (m *Module) UpdateIncludedFiles(ctx context.Context, count int)

UpdateIncludedFiles records how many files the request included beyond its entrypoint.

func (*Module) UpdateStatus

func (m *Module) UpdateStatus(ctx context.Context, state State)

UpdateStatus implements runner.Observer: it moves the trace of the running request to the scoreboard state the interpreter reports.

type Options

type Options = oida.Options

Options configures recording, the middleware and the debug front end.

func NewOptions

func NewOptions(serviceName string) Options

NewOptions returns the default options for a service of that name, which the front end displays and every trace records. oida applies the OIDA_* environment to options built this way, so a deployment still configures itself; options built as a literal read none.

type Router

type Router = oida.Router

Router is the subset of a router needed to mount the debug front end, satisfied by chi.Router, which is what platform.Router is.

type Sampler

type Sampler = oida.Sampler

Sampler decides whether a request is traced.

type Snapshot

type Snapshot = oida.Snapshot

Snapshot is the complete read model of a tracer at one point in time.

type Span

type Span = oida.Span

Span is one timed operation within a trace. Every method tolerates a nil receiver.

func SpanFromContext

func SpanFromContext(ctx context.Context) *Span

SpanFromContext returns the innermost span in ctx, or nil.

func Start

func Start(ctx context.Context, name string, kind ...Kind) (context.Context, *Span)

Start records a span in the trace carried by ctx and returns a context carrying it, so spans started from that context nest below this one.

func StartSpan

func StartSpan(ctx context.Context, name string, kind ...Kind) *Span

StartSpan records a span without deriving a context. Use it for leaf spans that will not nest. The source location carried by ctx, when a PHP frame put one there, is recorded on the span.

type State

type State = oida.State

State is the scoreboard state of a trace in flight.

type Storage

type Storage = oida.Storage

Storage retains completed traces.

func NewStorageDisk

func NewStorageDisk(limit int, paths ...string) (Storage, error)

NewStorageDisk returns storage retaining at most limit traces as JSON documents, so they survive a restart.

This is the one place phpscript names an oida sub-package. oida selects a driver from OIDA_STORAGE_DRIVER and the OIDA_STORAGE_* variables, and the telemetry block in a site's configuration is what phpscript configures a site by; reaching the constructor directly is what keeps `driver: disk` meaning what it says instead of moving that choice into the environment.

type Trace

type Trace = oida.Trace

Trace is one recorded unit of work: an HTTP request, a startup step or a background job.

func TraceFromContext

func TraceFromContext(ctx context.Context) *Trace

TraceFromContext returns the trace in ctx, or nil.

type Tracer

type Tracer = oida.Tracer

Tracer records traces and backs the debug front end.

func New

func New(opts Options) (*Tracer, error)

New returns a tracer configured with opts. It is the only constructor: oida no longer keeps a process wide tracer, so a caller holds the one it built and hands it to whatever records into it.

Jump to

Keyboard shortcuts

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