Tracing is off until you turn it on. When disabled, no listeners are registered and the tracer is a no-op, so the cost is zero.
Enable tracing
Add these to .env:
APM_ENABLED=trueAPM_SAMPLE_RATE=1.0 OTEL_SERVICE_NAME=my-appOTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20<secret-token>"
The OTLP endpoint must be an APM intake or an OTel collector. See the Elastic notes on the introduction page. For Elastic, pass credentials in the headers. A secret token:
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20<secret-token>"
An API key:
OTEL_EXPORTER_OTLP_HEADERS="Authorization=ApiKey%20<base64 id:key>"
The space in the header value is encoded as %20.
Note: The safest setup is a collector on localhost: the app posts to
localhost:4318in a millisecond, and the collector (or EDOT agent) handles auth, retries and the trip to Elastic.
After that, requests, queue jobs, Artisan commands, queries, cache calls, outgoing Http calls, Livewire updates and reported exceptions are traced without further code.
Add a custom span
Resolve Fledge\Apm\Tracer from the container and wrap work in span(). The span is the current span of the fiber while the closure runs, and an exception is recorded on it and rethrown.
<?php use Fledge\Apm\ActiveSpan;use Fledge\Apm\Tracer; $result = app(Tracer::class)->span('invoice.render', function (ActiveSpan $span) { $span->setAttribute('invoice.lines', 12); return renderInvoice();});
Outside a transaction, span() starts a new root span (a trace of its own). Only when APM is disabled does it run the closure without recording anything. The built-in query, cache and HTTP client spans are the ones skipped outside a trace.