PHP code example of umbrellio / jaravel

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

    

umbrellio / jaravel example snippets


// config/jaravel.php
...
'http' => [
        'span_name' => fn (Request $request) => 'App: ' . $request->path(),
        'tags' => fn (Request $request, Response $response) => [
            'type' => 'http',
            'request_host' => $request->getHost(),
            'request_path' => $path = $request->path(),
            'request_method' => $request->method(),
            'response_status' => $response->getStatusCode(),
            'error' => !$response->isSuccessful() && !$response->isRedirection(),
        ],
...
    'guzzle' => [
        'span_name' => Umbrellio\Jaravel\Configurations\Guzzle\SpanNameResolver::class,
        'tags' => Umbrellio\Jaravel\Configurations\Guzzle\TagsResolver::class,
    ],
...

// config/jaravel.php
...
'http' => [
        'allow_request' => fn (Request $request) => str_contains($request->path(), '/api'),
...

// config/jaravel.php
...
'console' => [
        'filter_commands' => ['schedule:run'],
...


declare(strict_types=1);

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Umbrellio\Jaravel\Middleware\JobTracingMiddleware;

class JaravelTestJob implements ShouldQueue
{
    use InteractsWithQueue;

    public function handle()
    {
        ...
    }

    public function middleware()
    {
        return [new JobTracingMiddleware()];
    }
}

// config/jaravel.php
...
'job' => [
    'tags' => fn ($realJob, ?Job $job) => [
        'type' => 'job',
        'job_class' => get_class($realJob),
        'job_id' => optional($job)
            ->getJobId(),
        'job_connection_name' => optional($job)
            ->getConnectionName(),
        'job_name' => optional($job)
            ->getName(),
        'job_queue' => optional($job)
            ->getQueue(),
        'job_attempts' => optional($job)
            ->attempts(),
    ],
],
...

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Umbrellio\Jaravel\HttpTracingMiddlewareFactory;

$stack = HandlerStack::create();
$stack->push(HttpTracingMiddlewareFactory::create());
$client = new Client(['handler' => $stack]);

use App\Services\MyService;
use OpenTracing\Tracer;
use Umbrellio\Jaravel\Services\Span\SpanCreator;
use Umbrellio\Jaravel\Services\Span\SpanTagHelper;

// You should use dependency injection in your code, it`s just an example 
$spanCreator = app(SpanCreator::class);  
$myService = app(MyService::class);
$tracer = app(Tracer::class);

// First you need to create a span. It will be a child of current active span, if active span exists
$span = $spanCreator->create('Call MyService');

// Do something 
$myService->doSomething();

// Close active scope (span will be finished automatically) and flush tracer.
optional($tracer->getScopeManager()->getActive())->close();
$tracer->flush();