Skip to content

Commit

Permalink
Merge pull request #2 from worksome/feature/enable
Browse files Browse the repository at this point in the history
feat: add support to disable Laravel Telemetry
  • Loading branch information
owenvoke authored Jan 31, 2023
2 parents b1cfebe + c02fd07 commit 7946a7f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
3 changes: 3 additions & 0 deletions config/telemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

// The Configuration is based on OpenTelemetry's naming convention.
return [

'enabled' => env('LARAVEL_TELEMETRY_ENABLED', true),

'exporter' => [
'otlp' => [
'endpoint' => env('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4318'),
Expand Down
48 changes: 37 additions & 11 deletions src/LaravelTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Worksome\LaravelTelemetry;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Support\ServiceProvider;
Expand All @@ -30,9 +31,12 @@ class LaravelTelemetryServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(
MeterProviderSdkInterface::class,
fn() => new MeterProvider(
$this->app->singleton(MeterProviderSdkInterface::class, function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return null;
}

return new MeterProvider(
null,
ResourceInfoFactory::defaultResource(),
ClockFactory::getDefault(),
Expand All @@ -47,28 +51,38 @@ public function register(): void
new CriteriaViewRegistry(),
new WithSampledTraceExemplarFilter(),
new NoopStalenessHandlerFactory(),
)
);
);
});

$this->app->bind(MeterProviderInterface::class, MeterProviderSdkInterface::class);

$this->app->singleton(
TracerProviderSdkInterface::class,
fn() => (new TracerProviderFactory())->create()
);
$this->app->singleton(TracerProviderSdkInterface::class, function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return null;
}

return (new TracerProviderFactory())->create();
});

$this->app->bind(TracerProviderInterface::class, TracerProviderSdkInterface::class);
}

public function boot(): void
{
$this->publishes([
__DIR__ . '/../config/telemetry.php' => $this->app->configPath('telemetry.php'),
__DIR__.'/../config/telemetry.php' => $this->app->configPath('telemetry.php'),
], 'laravel-telemetry-config');

$this->mergeConfigFrom(
__DIR__ . '/../config/telemetry.php',
__DIR__.'/../config/telemetry.php',
'telemetry',
);

$this->app->beforeResolving(MeterProviderInterface::class, function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return;
}

/** @var LoggerInterface $logger */
$logger = $this->app->get(LoggerInterface::class);
/** @var ConfigConfigurationResolver $configResolver */
Expand All @@ -78,6 +92,10 @@ public function boot(): void
});

$this->app->beforeResolving(TracerProviderInterface::class, function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return;
}

/** @var LoggerInterface $logger */
$logger = $this->app->get(LoggerInterface::class);
/** @var ConfigConfigurationResolver $configResolver */
Expand All @@ -87,10 +105,18 @@ public function boot(): void
});

$this->callAfterResolving(Dispatcher::class, function (Dispatcher $event) {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return;
}

$event->listen(WorkerStopping::class, WorkerStoppingFlush::class);
});

$this->app->terminating(function () {
if (! $this->app->make(Repository::class)->get('telemetry.enabled')) {
return;
}

if ($this->app->resolved(MeterProviderSdkInterface::class)) {
/** @var MeterProviderSdkInterface $meter */
$meter = $this->app->get(MeterProviderSdkInterface::class);
Expand Down

0 comments on commit 7946a7f

Please sign in to comment.