Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/OpenTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function register(): void
__DIR__.'/../config/otel.php', 'otel',
);

// Check if OpenTelemetry is enabled
if (! config('otel.enabled', true)) {
return;
}

// Register Tracer
$this->app->singleton(Support\Measure::class, function ($app) {
return new Support\Measure($app);
Expand Down
41 changes: 41 additions & 0 deletions tests/OpenTelemetryServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,47 @@ public function test_provider_logs_startup_and_registration()
$this->assertTrue(true); // Mockery will fail if expectation not met
}

public function test_provider_does_not_register_services_when_disabled()
{
// Create a fresh Laravel application WITHOUT auto-loading our package provider
$app = new \Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);

$app->singleton(
\Illuminate\Contracts\Http\Kernel::class,
\Illuminate\Foundation\Http\Kernel::class
);

$app->singleton(
\Illuminate\Contracts\Console\Kernel::class,
\Illuminate\Foundation\Console\Kernel::class
);

$app->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
\Illuminate\Foundation\Exceptions\Handler::class
);

// Set config with enabled = false
$app['config'] = new \Illuminate\Config\Repository([
'otel' => array_merge(
include __DIR__.'/../config/otel.php',
['enabled' => false]
),
]);

// Create and register service provider
$provider = new OpenTelemetryServiceProvider($app);
$provider->register();

// Verify services are NOT bound when disabled
$this->assertFalse($app->bound(Measure::class));
$this->assertFalse($app->bound(\OpenTelemetry\API\Trace\TracerInterface::class));
$this->assertFalse($app->bound(\OpenTelemetry\API\Metrics\MeterInterface::class));
$this->assertFalse($app->bound(\Overtrue\LaravelOpenTelemetry\Support\Metric::class));
}

public function test_tracer_provider_is_initialized()
{
// 获取全局 TracerProvider
Expand Down