Skip to content

Commit c34a11d

Browse files
committed
fix: console root span
1 parent 44c7076 commit c34a11d

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/OpenTelemetryServiceProvider.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Http\Kernel;
99
use Illuminate\Support\ServiceProvider;
1010
use OpenTelemetry\API\Trace\SpanKind;
11+
use OpenTelemetry\Context\Context;
1112
use OpenTelemetry\SDK\Common\Time\ClockFactory;
1213
use Overtrue\LaravelOpenTelemetry\Middlewares\MeasureRequest;
1314
use Overtrue\LaravelOpenTelemetry\Support\CarbonClock;
@@ -38,10 +39,6 @@ public function boot(): void
3839
if (config('otle.automatically_trace_requests')) {
3940
$this->injectHttpMiddleware(app(Kernel::class));
4041
}
41-
42-
if ($this->app->runningInConsole() && config('otle.automatically_trace_cli')) {
43-
$this->startMeasureConsole();
44-
}
4542
}
4643

4744
public function register(): void
@@ -67,6 +64,10 @@ public function register(): void
6764
$this->app->singleton(TracerManager::class, function ($app) {
6865
return new TracerManager($app);
6966
});
67+
68+
if ($this->app->runningInConsole() && config('otle.automatically_trace_cli')) {
69+
$this->startMeasureConsole();
70+
}
7071
}
7172

7273
protected function injectLogConfig(): void
@@ -107,6 +108,7 @@ public function startMeasureConsole(): void
107108
$span = Facades\Measure::span('artisan')
108109
->setSpanKind(SpanKind::KIND_SERVER)
109110
->start();
111+
$span->storeInContext(Context::getCurrent());
110112
$scope = $span->activate();
111113

112114
$this->app->terminating(function () use ($span, $scope) {

src/Watchers/CommandWatcher.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class CommandWatcher implements Watcher
1616
public function register(Application $app): void
1717
{
1818
$app['events']->listen(CommandStarting::class, function (CommandStarting $event) {
19+
Measure::activeSpan()->updateName('[Command] '.$event->command);
20+
1921
$this->span = Measure::span('[Command] '.$event->command)
2022
->setAttributes([
2123
'command' => $event->command,

src/Watchers/ScheduledTaskWatcher.php

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class ScheduledTaskWatcher implements Watcher
1818
public function register(Application $app): void
1919
{
2020
$app['events']->listen(ScheduledTaskStarting::class, function (ScheduledTaskStarting $event) {
21+
Measure::activeSpan()->updateName('[Schedule] '.$event->task->getSummaryForDisplay());
22+
2123
$this->span = Measure::span('[Schedule] '.$event->task->getSummaryForDisplay())
2224
->setAttributes([
2325
'task.command' => $event->task->command,

0 commit comments

Comments
 (0)