Skip to content

Commit

Permalink
wip: cli
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 26, 2024
1 parent 80756b5 commit 1145217
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Watchers/CommandWatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Overtrue\LaravelOpenTelemetry\Watchers;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Contracts\Foundation\Application;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\Context\Context;
use Overtrue\LaravelOpenTelemetry\Facades\Measure;

class CommandWatcher implements Watcher
{
protected SpanInterface $span;

public function register(Application $app): void
{
$app['events']->listen(CommandStarting::class, function (CommandStarting $event) {
$this->span = Measure::span('[Command] '.$event->command)
->setAttributes([
'command' => $event->command,
'arguments' => $event->input->getArguments(),
'options' => $event->input->getOptions(),
])
->start();
$this->span->storeInContext(Context::getCurrent());
});

$app['events']->listen(CommandFinished::class, function (CommandFinished $event) {
$this->span->end();
});
}
}
43 changes: 43 additions & 0 deletions src/Watchers/ScheduledTaskWatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Overtrue\LaravelOpenTelemetry\Watchers;

use Illuminate\Console\Events\ScheduledTaskFailed;
use Illuminate\Console\Events\ScheduledTaskFinished;
use Illuminate\Console\Events\ScheduledTaskStarting;
use Illuminate\Contracts\Foundation\Application;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use Overtrue\LaravelOpenTelemetry\Facades\Measure;

class ScheduledTaskWatcher implements Watcher
{
protected SpanInterface $span;

public function register(Application $app): void
{
$app['events']->listen(ScheduledTaskStarting::class, function (ScheduledTaskStarting $event) {
$this->span = Measure::span('[Schedule] '.$event->task->getSummaryForDisplay())
->setAttributes([
'task.command' => $event->task->command,
'task.description' => $event->task->description,
'task.expression' => $event->task->expression,
'task.exitCode' => $event->task->exitCode,
])
->start();
$this->span->storeInContext(Context::getCurrent());
});

$app['events']->listen(ScheduledTaskFinished::class, function (ScheduledTaskFinished $event) {
$this->span->end();
});

$app['events']->listen(ScheduledTaskFailed::class, function (ScheduledTaskFailed $event) {
$this->span->setAttribute('task.error', $event->exception->getMessage());
$this->span->setStatus(StatusCode::STATUS_ERROR);
$this->span->recordException($event->exception);
$this->span->end();
});
}
}

0 comments on commit 1145217

Please sign in to comment.