PHP code example of erfanmomeniii / laravel-jaeger-client

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

    

erfanmomeniii / laravel-jaeger-client example snippets


'cache' => ['enabled' => true],
'redis' => ['enabled' => true],

use LaravelJaeger\Laravel\Facades\Jaeger;

$result = Jaeger::build('payment.charge')
    ->withTag('payment.id', $payment->id)
    ->withTag('payment.amount', $payment->amount)
    ->measure(function () use ($payment) {
        return $this->gateway->charge($payment);
    });

$scope = jaeger_span('order.process', ['order.id' => $order->id]);

try {
    $this->processOrder($order);
    $scope->getSpan()->setTag('order.status', 'completed');
} catch (\Throwable $e) {
    $scope->getSpan()->setTag('error', true);
    $scope->getSpan()->log([
        'event' => 'error',
        'error.kind' => get_class($e),
        'error.message' => $e->getMessage(),
    ]);
    throw $e;
} finally {
    $scope->close();
}

// Tag the current active span (from middleware or a parent)
jaeger_active_span()?->setTag('user.id', auth()->id());

// Access the tracer directly
$tracer = jaeger();

use Illuminate\Contracts\Queue\ShouldQueue;
use LaravelJaeger\Laravel\Traits\TracedJob;

class ProcessPayment implements ShouldQueue
{
    use TracedJob;

    public function handle(): void
    {
        // The parent trace from the HTTP request is linked here.
        jaeger_active_span()?->setTag('payment.status', 'completed');
    }
}

use LaravelJaeger\Laravel\Facades\Jaeger;

public function test_checkout_creates_payment_trace(): void
{
    $fake = Jaeger::fake();

    $this->postJson('/api/checkout', ['item_id' => 1]);

    // Assert spans were created
    $fake->assertSpanCreated('HTTP POST /api/checkout');
    $fake->assertSpanCreated('db.query');
    $fake->assertSpanCreated('payment.charge');

    // Assert specific tags
    $fake->assertSpanHasTag('payment.charge', 'payment.amount', 99.99);

    // Assert errors were recorded
    $fake->assertSpanHasLog('payment.charge', ['event' => 'error']);

    // Assert span count
    $fake->assertSpanCount(5);
}

use LaravelJaeger\Testing\WithTracing;

class PaymentTest extends TestCase
{
    use WithTracing;

    public function test_charge(): void
    {
        // $this->fakeTracer is automatically available
        $this->processPayment();

        $this->fakeTracer->assertSpanCreated('payment.charge');
    }
}

use LaravelJaeger\Contracts\SamplerInterface;

class UserBasedSampler implements SamplerInterface
{
    public function isSampled(string $traceId, string $operationName): array
    {
        $isAdmin = auth()->user()?->is_admin ?? false;

        return [$isAdmin, ['sampler.type' => 'user-based', 'sampler.param' => $isAdmin]];
    }

    public function close(): void {}
}

// Register in AppServiceProvider:
$this->app->bind(SamplerInterface::class, UserBasedSampler::class);

use LaravelJaeger\Contracts\TransportInterface;

class KafkaTransport implements TransportInterface
{
    public function append(array $spans): void { /* buffer spans */ }
    public function flush(): void { /* publish to Kafka topic */ }
    public function close(): void { /* disconnect */ }
}
bash
php artisan vendor:publish --tag=jaeger-config