Skip to content

Commit da6b725

Browse files
Laravel: allow instrumentation env variables to propagate when using \Illuminate\Foundation\Console\ServeCommand. (#221)
1 parent ac27b40 commit da6b725

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/LaravelInstrumentation.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OpenTelemetry\Contrib\Instrumentation\Laravel;
66

77
use Illuminate\Contracts\Foundation\Application;
8+
use Illuminate\Foundation\Console\ServeCommand;
89
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
910
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\CacheWatcher;
1011
use OpenTelemetry\Contrib\Instrumentation\Laravel\Watchers\ClientRequestWatcher;
@@ -42,5 +43,25 @@ public static function register(): void
4243

4344
ConsoleInstrumentation::register($instrumentation);
4445
HttpInstrumentation::register($instrumentation);
46+
47+
self::developmentInstrumentation();
48+
}
49+
50+
private static function developmentInstrumentation(): void
51+
{
52+
// Allow instrumentation when using the local PHP development server.
53+
if (class_exists(ServeCommand::class) && property_exists(ServeCommand::class, 'passthroughVariables')) {
54+
hook(
55+
ServeCommand::class,
56+
'handle',
57+
pre: static function (ServeCommand $serveCommand, array $params, string $class, string $function, ?string $filename, ?int $lineno) {
58+
foreach ($_ENV as $key => $value) {
59+
if (str_starts_with($key, 'OTEL_') && !in_array($key, ServeCommand::$passthroughVariables)) {
60+
ServeCommand::$passthroughVariables[] = $key;
61+
}
62+
}
63+
},
64+
);
65+
}
4566
}
4667
}

0 commit comments

Comments
 (0)