Skip to content

Commit

Permalink
Add Tracing Package
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesChou committed Aug 4, 2023
1 parent 2a28dc1 commit dc5a26c
Show file tree
Hide file tree
Showing 21 changed files with 1,041 additions and 449 deletions.
14 changes: 14 additions & 0 deletions app/Tracing/Tracer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Tracing;

use Illuminate\Support\Facades\Facade;
use OpenTracing\Tracer as OpenTracingTracer;

class Tracer extends Facade
{
protected static function getFacadeAccessor()
{
return OpenTracingTracer::class;
}
}
56 changes: 56 additions & 0 deletions app/Tracing/TracingServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Tracing;

use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\ServiceProvider;
use Jaeger\Config;
use Jaeger\Tracer as JaegerTracer;
use OpenTracing\Tracer;
use Psr\Log\LoggerInterface;

use const Jaeger\SAMPLER_TYPE_CONST;

class TracingServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(JaegerTracer::class, function () {
$config = new Config([
'sampler' => [
'type' => SAMPLER_TYPE_CONST,
'param' => true,
],

'logging' => true,

'local_agent' => [
'reporting_host' => 'localhost',
// You can override port by setting local_agent.reporting_port value
'reporting_port' => 6832,
],

// Different ways to send data to Jaeger. Config::ZIPKIN_OVER_COMPACT - default):
'dispatch_mode' => Config::JAEGER_OVER_BINARY_UDP,
], 'Mart Web Server', $this->app->make(LoggerInterface::class));

return $config->initializeTracer();
});

$this->app->bind(Tracer::class, JaegerTracer::class);
}

public function boot(Tracer $tracer): void
{
$scope = $tracer->startActiveSpan(
Request::getMethod() . ' ' . Request::getUri(),
);

Event::listen(RequestHandled::class, function () use ($scope, $tracer) {
$scope->close();
$tracer->flush();
});
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"php": "^8.1",
"guzzlehttp/guzzle": "^7.0.1",
"intervention/image": "^2.5",
"jonahgeorge/jaeger-client-php": "^1.4",
"laravel/fortify": "^1.6",
"laravel/framework": "^10.14",
"laravel/octane": "^2.0",
Expand Down
Loading

0 comments on commit dc5a26c

Please sign in to comment.