PHP code example of ditliti / laravel-sdk

1. Go to this page and download the library: Download ditliti/laravel-sdk 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/ */

    

ditliti / laravel-sdk example snippets


return [
    'endpoint' => env('DITLITI_ENDPOINT', 'http://localhost:8305'),
    'api_key' => env('DITLITI_API_KEY'),
    'project_id' => env('DITLITI_PROJECT_ID'),
];

use Ditliti\Laravel\DitlitiClient;

$client = app(DitlitiClient::class);

try {
    riskyOperation();
} catch (\Throwable $e) {
    $client->report($e, ['extra' => 'context']);
}

// app/Http/Middleware/ContinueDitlitiTrace.php
public function handle(Request $request, Closure $next)
{
    $client = app(DitlitiClient::class)->withTrace($request->header('traceparent'));
    app()->instance(DitlitiClient::class, $client);

    return $next($request);
}

// Service A: start a trace and call Service B, forwarding the header.
$client = $client->withTrace();
Http::withHeaders(['traceparent' => $client->getTraceparent()])->get($serviceBUrl);

$span = $client->startSpan('db.query', ['db' => 'postgres']);
// ... do the work ...
$span->finish();

// report() automatically tags trace_id when a trace is active,
// so errors show up correlated to the trace at GET /api/v1/traces/:trace_id (org-wide).

// config/ditliti.php
return [
    // ...
    'trace_queries' => env('DITLITI_TRACE_QUERIES', false),
];

use Ditliti\Laravel\DbTracing;

DbTracing::listen($client, system: 'postgresql');

// In your exception handler, alongside $client->report($exception, ...):
if ($exception instanceof \Illuminate\Database\QueryException) {
    DbTracing::reportQueryException($client, $exception, system: 'postgresql');
}