PHP code example of adelinferaru / nestedflowtracker

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

    

adelinferaru / nestedflowtracker example snippets


use AdelinFeraru\NestedFlowTracker\Laravel\Facades\Flow;

$account = Flow::span('register user', function () use ($data) {
    $account = Flow::span('create account', fn () => Account::create($data));

    Flow::span('send welcome email', fn () => Mail::to($account)->send(new Welcome()));

    return $account;
});

flow()->span('charge card', fn () => $gateway->charge($card));

app(\AdelinFeraru\NestedFlowTracker\Core\FlowTracker::class)->span(/* ... */);

use AdelinFeraru\NestedFlowTracker\Core\Drivers\PdoDriver;
use AdelinFeraru\NestedFlowTracker\Core\Drivers\PdoSchema;
use AdelinFeraru\NestedFlowTracker\Core\FlowConfig;
use AdelinFeraru\NestedFlowTracker\Core\FlowTracker;

$pdo = new PDO('sqlite:flows.sqlite');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
PdoSchema::create($pdo);                                    // sqlite / mysql / pgsql

$flow = new FlowTracker(
    new FlowConfig(enabled: true, component: 'orders'),
    $events,                                                // any PSR-14 EventDispatcherInterface
    new PdoDriver($pdo),                                    // or BufferedPdoDriver for one bulk insert per flow
);

$flow->span('checkout', function ($span) use ($flow) {
    $flow->span('charge card', fn () => /* ... */);
});

Flow::span('import csv', function ($span) use ($rows) {
    $span->context = ['rows' => count($rows)];
    $imported = $this->import($rows);
    $span->result = ['imported' => $imported];
    return $imported;
});

Flow::start('long running process');
// ...
Flow::end(['result' => ['ok' => true]]);

Http::withFlowTrace()->post('https://orders.internal/checkout', $payload);

use AdelinFeraru\NestedFlowTracker\Core\TraceContext;

if ($ctx = TraceContext::parse($request->header('traceparent'))) {
    Flow::setTraceId($ctx->traceId);
}

use AdelinFeraru\NestedFlowTracker\Core\Events\SpanFinished;

Event::listen(function (SpanFinished $event) {
    if ($event->span->duration > 1.0) {
        Log::warning("Slow span: {$event->span->name} ({$event->span->duration}s)");
    }
});

use AdelinFeraru\NestedFlowTracker\Core\Attributes\Trace;

class CheckoutController
{
    #[Trace('checkout')]                  // or #[Trace] to name it after the action
    public function store(Request $request) { /* … */ }
}

#[Trace]                                  // class-level: on a job (or every controller action)
class SendInvoice implements ShouldQueue { /* … */ }

use Illuminate\Support\Facades\Gate;

Gate::define('viewFlow', fn ($user) => $user->isAdmin());
bash
php artisan vendor:publish --tag="flow-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="flow-config"
bash
php examples/plain-php.php
bash
php artisan flow:show {trace}   # print a flow as a tree
php artisan flow:prune --days=30 # delete flow spans older than N days

GET {path}/api/flows               # recent flows; ?component=, ?status=, ?per_page=, ?page=
GET {path}/api/flows/{trace}       # one flow as a nested span tree
bash
php artisan flow:benchmark --flows=300 --spans=5