telemetry

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 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.
	BackgroundHost = oida.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 Handler

func Handler(opts Options) http.Handler

Handler returns the debug front end handler for the tracer resolved from opts.

func HandlerFor

func HandlerFor(tracer *Tracer) http.Handler

HandlerFor returns the debug front end handler of one tracer.

func Mount

func Mount(r Router, opts Options) error

Mount registers the debug front end on r under Options.Path, wired to the tracer resolved from opts.

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 have none, so they group under BackgroundHost.

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 TracingMiddleware

func TracingMiddleware(opts Options) func(http.Handler) http.Handler

TracingMiddleware returns middleware recording every sampled request into the tracer resolved from opts.

func ValidID

func ValidID(id string) bool

ValidID reports whether id looks like a recorded trace identifier. It keeps hostile input out of lookups and out of rendered links.

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 HTTPInfo

type HTTPInfo = oida.HTTPInfo

HTTPInfo describes the request a trace was created for.

type HostStat

type HostStat = oida.HostStat

HostStat aggregates the traffic of one host.

type Kind

type Kind = oida.Kind

Kind classifies the work a span measured.

type Memory

type Memory = oida.Memory

Memory describes current process memory and GC pressure.

type MemoryUse

type MemoryUse = oida.MemoryUse

MemoryUse holds the allocation deltas observed while a trace ran.

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() Options

NewOptions returns the default options.

type PoolEstimate

type PoolEstimate = oida.PoolEstimate

PoolEstimate is a heuristic concurrency estimate.

type Recorder

type Recorder = oida.Recorder

Recorder is the substitutable subset of Tracer.

type Router

type Router = frontend.Router

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

type Sampler

type Sampler = oida.Sampler

Sampler decides whether a request is traced.

func NewRateSampler

func NewRateSampler(rate float64) Sampler

NewRateSampler returns a sampler tracing the given fraction of requests.

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 StateDuration

type StateDuration = oida.StateDuration

StateDuration is the lifetime trace time observed in one state.

type Statistic

type Statistic = oida.Statistic

Statistic aggregates one group of traces in the rolling window.

type Stats

type Stats = oida.Stats

Stats contains the most frequent trace groups in the rolling window.

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.

func NewStorageMemory

func NewStorageMemory(size int) Storage

NewStorageMemory returns in-memory storage retaining size traces.

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 Configure

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

Configure replaces the process wide tracer with one built from opts and returns it.

func Default

func Default() *Tracer

Default returns the process wide tracer, creating it on first use.

func New

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

New returns a tracer configured with opts. Prefer it over Configure in libraries and tests: it does not touch process wide state.

func Resolve

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

Resolve returns the tracer the options point at: the explicit one when set, the process default otherwise.

Jump to

Keyboard shortcuts

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