Tracer facade is the primary entry point for manual tracing. It wraps the underlying Keepsuit\LaravelOpenTelemetry\Tracer class and gives you access to the active span, trace context, propagation headers, and span creation.
traceStarted()
Returns whether an active, valid trace is currently in progress.
true if there is a currently active span with a valid trace context, false otherwise.traceId()
Returns the trace ID of the currently active span, or null if no valid trace is active.
The 32-character hex trace ID string, or
null when no valid trace is active.activeSpan()
Returns the currently active SpanInterface. When no span has been started this returns a no-op span.
The currently active span.
activeScope()
Returns the current ScopeInterface, or null if there is no active scope. A scope represents the lifetime of a span activation and must be detached when you are done.
The active scope, or
null.currentContext()
Returns the current ContextInterface. Useful for capturing context to pass across asynchronous boundaries.
The current OpenTelemetry context.
propagationHeaders()
Returns an associative array of HTTP headers that encode the current trace context for propagation to downstream services.
The context to inject into the headers. When
null, the current active context is used.Associative array of propagation headers (e.g.
traceparent, tracestate).extractContextFromPropagationHeaders()
Extracts a ContextInterface from an incoming set of HTTP headers. Use this to continue a trace that originated in an upstream service.
Associative array of HTTP headers received from an upstream service.
The context extracted from the propagation headers.
newSpan()
Creates a new SpanBuilder for the given span name. Chain builder methods to configure the span, then call measure() or start() to begin recording.
The name of the span. Must be a non-empty string.
A fluent span builder instance.
updateLogContext()
Injects the current trace ID into Laravel’s shared log context so that all subsequent log entries carry the trace_id field. Call this after starting a root span manually.
When using the built-in instrumentations (HTTP server, queue, etc.) or the
otlp log channel, this is called automatically. You only need to call it when starting the root trace manually.terminateActiveSpansUpToRoot()
Ends all currently active, recording spans by detaching their scopes and calling end() on each one, walking up the stack until there are no more recording spans or the optional $root span is reached.
Stop terminating spans when this span is reached. When
null, all active recording spans are ended.SpanBuilder methods
Tracer::newSpan() returns a SpanBuilder instance. All methods except measure() and start() return the builder itself for fluent chaining.
setAttribute()
Sets a single attribute on the span.
Attribute key.
Attribute value. Scalars, arrays, and booleans are supported.
setAttributes()
Sets multiple attributes at once from any iterable.
An iterable of
string => mixed pairs.setParent()
Overrides the parent context for this span. Pass null to create a root span with no parent.
Parent context, or
null for a root span.addLink()
Adds a link to another span context. Useful for associating spans across unrelated traces.
The span context to link to.
Attributes to attach to the link.
setStartTimestamp()
Sets an explicit start timestamp for the span. Accepts a Carbon instance or a raw nanosecond integer.
A Carbon instance or a Unix timestamp in nanoseconds.
setSpanKind()
Sets the span kind. Accepts any SpanKind::KIND_* constant.
One of
SpanKind::KIND_INTERNAL, KIND_SERVER, KIND_CLIENT, KIND_PRODUCER, or KIND_CONSUMER.start()
Starts the span and returns a SpanInterface. The span is not automatically activated — call $span->activate() to make it the active span for nested operations.
The started span.
measure()
Starts the span, activates it as the current span, executes the callback, then ends the span. Exceptions are automatically recorded on the span and re-thrown.
A closure that receives the active
SpanInterface as its first argument. The return value of the closure is returned by measure().The value returned by the callback. If the callback returns a
PendingDispatch (e.g. from dispatch()), null is returned instead to ensure the job is dispatched correctly.measure() vs start() — when to use each
Use measure() when the work you want to trace is synchronous and fits inside a closure. The span lifecycle is managed automatically.
Use start() when you need fine-grained control: for example, when the span must stay open across multiple steps, when you want to activate the span conditionally, or when you need to attach it to an asynchronous operation.
Distributed tracing example
The following example shows how to propagate a trace from a producer service to a consumer service usingpropagationHeaders() and extractContextFromPropagationHeaders().
