PHP code example of lookout / tracing
1. Go to this page and download the library: Download lookout/tracing library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lookout / tracing example snippets
use Lookout\Tracing\Tracer;
Tracer::instance()->configure([
'api_key' => getenv('LOOKOUT_API_KEY') ?: null,
'base_uri' => rtrim((string) getenv('LOOKOUT_URL'), '/') ?: null,
'environment' => getenv('APP_ENV') ?: null,
]);
use Lookout\Tracing\Tracer;
use Lookout\Tracing\TraceWireHeaders;
Tracer::instance()->continueTrace(
$request->getHeaderLine(TraceWireHeaders::HTTP_TRACEPARENT),
$request->getHeaderLine(TraceWireHeaders::HTTP_BAGGAGE),
);
$headers = Tracer::instance()->outgoingTraceHeaders();
// keys: TraceWireHeaders::HTTP_TRACEPARENT, TraceWireHeaders::HTTP_BAGGAGE
use Lookout\Tracing\HtmlTraceMeta;
echo HtmlTraceMeta::render();
use Lookout\Tracing\SpanOperation;
use Lookout\Tracing\Tracing;
$tx = Tracing::startTransaction('GET /orders', SpanOperation::HTTP_SERVER);
Tracing::trace(function () {
// …
}, SpanOperation::HTTP_CLIENT, 'GET https://api.example.com/v1/orders');
$tx->finish();
lookout_logger()->info('order placed', null, ['order_id' => '42']);
lookout_logger()->flush();
use Lookout\Tracing\Logging\Monolog\LookoutMonologHandler;
use Monolog\Logger;
$log = new Logger('app');
$log->pushHandler(new LookoutMonologHandler());
use Lookout\Tracing\Metrics\MetricUnit;
lookout_metrics()->count('button.click', 5, ['plan' => 'pro']);
lookout_metrics()->distribution('page.load_ms', 42.5, ['route' => '/checkout'], MetricUnit::millisecond());
lookout_metrics()->flush();
use Lookout\Tracing\GlowBreadcrumb;
GlowBreadcrumb::glow('Payment branch: validated wallet', 'info', ['wallet_id' => $id]);
GlowBreadcrumb::glow('Skipping cache (feature flag)', 'debug');
use Lookout\Tracing\FilesystemBreadcrumb;
FilesystemBreadcrumb::record('read', '/var/app/config.json', 'info', ['bytes' => 1024]);
use Lookout\Tracing\Cron\CheckInStatus;
use Lookout\Tracing\Cron\Client as CronClient;
use Lookout\Tracing\Cron\MonitorConfig;
use Lookout\Tracing\Cron\MonitorSchedule;
CronClient::configure([
'api_key' => getenv('LOOKOUT_API_KEY'),
'base_uri' => 'https://your-lookout-host.example',
'cron_ingest_path' => '/api/ingest/cron',
]);
$config = MonitorConfig::make(MonitorSchedule::crontab('0 * * * *'), checkinMarginMinutes: 5);
$id = CronClient::captureCheckIn('hourly-job', CheckInStatus::inProgress(), monitorConfig: $config);
CronClient::captureCheckIn('hourly-job', CheckInStatus::ok(), $id);
CronClient::withMonitor('wrapped-job', fn () => doWork(), $config);
CronClient::captureCheckIn('heartbeat', CheckInStatus::ok(), null, 12.0);
use Lookout\Tracing\Profiling\ProfileClient;
ProfileClient::configure([
'api_key' => getenv('LOOKOUT_API_KEY'),
'base_uri' => 'https://your-lookout-host.example',
'profile_ingest_path' => '/api/ingest/profile',
]);
ProfileClient::sendProfile([
'agent' => 'other',
'format' => 'speedscope',
'data' => [/* speedscope JSON object */],
'trace_id' => 'abc123…',
'transaction' => 'GET /checkout',
]);
use Lookout\Tracing\Profiling\LookoutProfileV1Payload;
use Lookout\Tracing\Profiling\ProfileClient;
ProfileClient::sendProfile(LookoutProfileV1Payload::aggregateIngestBody(
[
['file' => 'app/Services/Checkout.php', 'line' => 120, 'samples' => 48],
],
meta: ['source' => 'custom-collector'],
context: ['trace_id' => 'abc123…', 'transaction' => 'POST /checkout'],
));
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Lookout\Tracing\Http\GuzzleTraceMiddleware;
$stack = HandlerStack::create();
$stack->push(GuzzleTraceMiddleware::create());
$client = new Client(['handler' => $stack]);
bash
php artisan config:clear
bash
php artisan vendor:publish --tag=lookout-tracing-config