PHP code example of ditliti / php-sdk

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



use Ditliti\DitlitiClient;

use Ditliti\DitlitiClient;

$client = new DitlitiClient(
    endpoint: 'http://localhost:8305',
    apiKey: 'mp_xxx',
    projectId: 'PROJECT_UUID',
    environment: 'production'
);

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

// in your plugin's main file
tiClient(DITLITI_ENDPOINT, DITLITI_API_KEY, DITLITI_PROJECT_ID);

set_exception_handler(function (\Throwable $e) use ($ditliti) {
    $ditliti->captureException($e, ['framework' => 'wordpress']);
});

// src/EventSubscriber/DitlitiExceptionSubscriber.php
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class DitlitiExceptionSubscriber implements EventSubscriberInterface
{
    public function __construct(private readonly \Ditliti\DitlitiClient $client) {}

    public static function getSubscribedEvents(): array
    {
        return [KernelEvents::EXCEPTION => 'onKernelException'];
    }

    public function onKernelException(ExceptionEvent $event): void
    {
        $this->client->captureException($event->getThrowable(), ['framework' => 'symfony']);
    }
}

// Service A: start a trace and call Service B, forwarding the header.
$client->startTrace();
$response = $httpClient->request('GET', $serviceBUrl, [
    'headers' => ['traceparent' => $client->getTraceparent()],
]);

// Service B: continue the same trace from the inbound header.
$client->startTrace($request->headers->get('traceparent'));

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

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



use Ditliti\DbTracing;

// Generic wrapper - works for any driver:
$rows = DbTracing::traceQuery(
    $client,
    system: 'postgresql',
    text: 'SELECT * FROM orders WHERE id = ?',
    execute: fn () => $pdo->prepare('SELECT * FROM orders WHERE id = ?')->execute([$orderId]),
);

// Or wrap a PDO connection transparently:
$traced = DbTracing::instrumentPdo($client, $pdo, 'postgresql');
$rows = $traced->query('SELECT * FROM orders WHERE id = ?')->fetchAll();
$stmt = $traced->prepare('UPDATE orders SET status = ? WHERE id = ?');
$stmt->execute(['shipped', $orderId]);