Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 6fd3159

Browse files
authored
Merge pull request #7 from overtrue/copilot/fix-otel-register-check
Add enabled check to register() method to prevent service registration when disabled
2 parents 09fab78 + 4508610 commit 6fd3159

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/OpenTelemetryServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function register(): void
4646
__DIR__.'/../config/otel.php', 'otel',
4747
);
4848

49+
// Check if OpenTelemetry is enabled
50+
if (! config('otel.enabled', true)) {
51+
return;
52+
}
53+
4954
// Register Tracer
5055
$this->app->singleton(Support\Measure::class, function ($app) {
5156
return new Support\Measure($app);

tests/OpenTelemetryServiceProviderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,47 @@ public function test_provider_logs_startup_and_registration()
133133
$this->assertTrue(true); // Mockery will fail if expectation not met
134134
}
135135

136+
public function test_provider_does_not_register_services_when_disabled()
137+
{
138+
// Create a fresh Laravel application WITHOUT auto-loading our package provider
139+
$app = new \Illuminate\Foundation\Application(
140+
realpath(__DIR__.'/../')
141+
);
142+
143+
$app->singleton(
144+
\Illuminate\Contracts\Http\Kernel::class,
145+
\Illuminate\Foundation\Http\Kernel::class
146+
);
147+
148+
$app->singleton(
149+
\Illuminate\Contracts\Console\Kernel::class,
150+
\Illuminate\Foundation\Console\Kernel::class
151+
);
152+
153+
$app->singleton(
154+
\Illuminate\Contracts\Debug\ExceptionHandler::class,
155+
\Illuminate\Foundation\Exceptions\Handler::class
156+
);
157+
158+
// Set config with enabled = false
159+
$app['config'] = new \Illuminate\Config\Repository([
160+
'otel' => array_merge(
161+
include __DIR__.'/../config/otel.php',
162+
['enabled' => false]
163+
),
164+
]);
165+
166+
// Create and register service provider
167+
$provider = new OpenTelemetryServiceProvider($app);
168+
$provider->register();
169+
170+
// Verify services are NOT bound when disabled
171+
$this->assertFalse($app->bound(Measure::class));
172+
$this->assertFalse($app->bound(\OpenTelemetry\API\Trace\TracerInterface::class));
173+
$this->assertFalse($app->bound(\OpenTelemetry\API\Metrics\MeterInterface::class));
174+
$this->assertFalse($app->bound(\Overtrue\LaravelOpenTelemetry\Support\Metric::class));
175+
}
176+
136177
public function test_tracer_provider_is_initialized()
137178
{
138179
// 获取全局 TracerProvider

0 commit comments

Comments
 (0)