Skip to main content
use Keepsuit\LaravelOpenTelemetry\Facades\OpenTelemetry;
The OpenTelemetry facade provides a single entry point to all three OTel signals — tracing, metrics, and logging — and exposes the user context resolver configuration. The individual Tracer, Meter, and Logger facades remain fully functional and are the recommended way to access each signal directly; OpenTelemetry is useful when you need to access multiple signals or configure cross-cutting behaviour from a single import.

tracer()

Resolves and returns the Tracer instance from the service container.
Tracer OpenTelemetry::tracer()
return
Keepsuit\LaravelOpenTelemetry\Tracer
The application’s Tracer instance.
$tracer = OpenTelemetry::tracer();

$tracer->newSpan('my-span')->measure(fn () => doWork());

meter()

Resolves and returns the Meter instance from the service container.
Meter OpenTelemetry::meter()
return
Keepsuit\LaravelOpenTelemetry\Meter
The application’s Meter instance.
$meter = OpenTelemetry::meter();

$counter = $meter->counter('my.requests', '{requests}', 'Total requests');
$counter->add(1);

logger()

Resolves and returns the Logger instance from the service container.
Logger OpenTelemetry::logger()
return
Keepsuit\LaravelOpenTelemetry\Logger
The application’s Logger instance.
$logger = OpenTelemetry::logger();

$logger->info('Processing started', ['job.id' => $jobId]);

user()

Registers a resolver closure that is called whenever user context attributes need to be collected for a trace span or log record. The closure receives the currently authenticated Authenticatable instance and must return an associative array of attribute key-value pairs.
void OpenTelemetry::user(Closure $resolver)
resolver
Closure
required
A closure with the signature function (Authenticatable $user): array. The array must use non-empty string keys mapped to scalar or array values.
Register the resolver in your AppServiceProvider (or any service provider’s boot method):
use Keepsuit\LaravelOpenTelemetry\Facades\OpenTelemetry;
use Illuminate\Contracts\Auth\Authenticatable;

public function boot(): void
{
    OpenTelemetry::user(function (Authenticatable $user) {
        return [
            'user.id'    => $user->getAuthIdentifier(),
            'user.email' => $user->email,
        ];
    });
}
When opentelemetry.user_context is true (the default), this resolver is invoked automatically for every trace span and every log record emitted through the Logger facade.
If no custom resolver is registered, the package defaults to including only user.id using $user->getAuthIdentifier().

collectUserContext()

Invokes the registered user context resolver for the given Authenticatable instance and returns the resulting attributes array. This method is called internally by the tracing and logging layers.
array OpenTelemetry::collectUserContext(Authenticatable $user)
user
Illuminate\Contracts\Auth\Authenticatable
required
The authenticated user to collect context for.
return
array
An associative array of non-empty-string => bool|int|float|string|array|null attribute pairs returned by the registered resolver.
$attributes = OpenTelemetry::collectUserContext(Auth::user());
// ['user.id' => 42, 'user.email' => 'alice@example.com']

flush()

Force-flushes all pending telemetry data across all three signals — traces, metrics, and logs — immediately. Use this in long-running processes (queue workers, Octane) when you need to guarantee that data is exported without waiting for the next batch interval or process shutdown.
void OpenTelemetry::flush()
// Ensure all telemetry is exported before the process exits
OpenTelemetry::flush();
In worker mode, the package can be configured to flush automatically after each iteration (HTTP request, queue job) via the OTEL_WORKER_MODE_FLUSH_AFTER_EACH_ITERATION environment variable. Manual calls to flush() are typically only needed outside of that automatic lifecycle.

Relationship to individual facades

The Tracer, Meter, and Logger facades are independent and resolve the same underlying service container bindings as OpenTelemetry::tracer(), OpenTelemetry::meter(), and OpenTelemetry::logger(). You can use them interchangeably:
// These are equivalent:
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer;
use Keepsuit\LaravelOpenTelemetry\Facades\OpenTelemetry;

Tracer::newSpan('my-span')->measure(fn () => doWork());
OpenTelemetry::tracer()->newSpan('my-span')->measure(fn () => doWork());
Prefer the individual facades (Tracer, Meter, Logger) for single-signal usage. Use the OpenTelemetry facade when you need cross-signal access or are configuring global behaviour such as the user context resolver.

Build docs developers (and LLMs) love