Skip to content

Add console tracing #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2019
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Flushing Spans](#flushing-spans)
- [Logging Integration](#logging-integration)
- [Middleware](#middleware)
- [Console Commands](#console-commands)
- [Context Propagation](#context-propagation)
- [Custom Drivers](#custom-drivers)
- [Writing New Driver](#writing-new-driver)
Expand Down Expand Up @@ -245,6 +246,21 @@ You can override the default name of the span in the controller:
Trace::getRootSpan()->setName('Create Order')
```

### Console Commands

If you want to trace select console commands, make them implement `Vinelab\Tracing\Contracts\ShouldBeTraced` interface, indicating that we should start spans for the command.

The trace will include the following **tags** on a root span:

- `type` (cli)
- `argv`

You can override the default name of the span in the command itself:

```php
Trace::getRootSpan()->setName('Mark Orders Expired')
```

### Context Propagation

As we talked about previously, the tracer understands how to inject and extract trace context across different applications (services).
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
],
"require": {
"php": "^7.1.3",
"illuminate/contracts": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/http": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/support": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/console": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/contracts": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/http": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"openzipkin/zipkin": "~1.0",
"psr/http-message": "~1.0",
"ramsey/uuid": "~3.0"
Expand Down
8 changes: 8 additions & 0 deletions src/Contracts/ShouldBeTraced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Vinelab\Tracing\Contracts;

interface ShouldBeTraced
{

}
65 changes: 65 additions & 0 deletions src/Listeners/TraceCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Vinelab\Tracing\Listeners;

use Illuminate\Console\Command;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Arr;
use Vinelab\Tracing\Contracts\ShouldBeTraced;
use Vinelab\Tracing\Contracts\Tracer;

class TraceCommand
{
/**
* @var Tracer
*/
protected $tracer;

/**
* @var Kernel
*/
protected $artisan;

/**
* Create the event listener.
*
* @param Tracer $tracer
* @param Kernel $artisan
*/
public function __construct(Tracer $tracer, Kernel $artisan)
{
$this->tracer = $tracer;
$this->artisan = $artisan;
}

/**
* Handle the event.
*
* @param CommandStarting $event
* @return void
*/
public function handle(CommandStarting $event)
{
if ($this->shouldTraceCommand($event->command)) {
$span = $this->tracer->startSpan('Console Command');

$span->tag('type', 'cli');
$span->tag('argv', implode(PHP_EOL, $_SERVER['argv']));
}
}

/**
* @param string $command
* @return bool
*/
protected function shouldTraceCommand(string $command): bool
{
/** @var Command $command */
$command = Arr::get($this->artisan->all(), $command);

$interfaces = class_implements($command);

return isset($interfaces[ShouldBeTraced::class]);
}
}
4 changes: 4 additions & 0 deletions src/TracingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Vinelab\Tracing;

use Illuminate\Console\Events\CommandStarting;
use Illuminate\Support\ServiceProvider;
use Vinelab\Tracing\Contracts\Tracer;
use Vinelab\Tracing\Facades\Trace;
use Vinelab\Tracing\Listeners\TraceCommand;

class TracingServiceProvider extends ServiceProvider
{
Expand All @@ -21,6 +23,8 @@ public function boot()
]);
}

$this->app['events']->listen(CommandStarting::class, TraceCommand::class);

$this->app->terminating(function () {
$rootSpan = Trace::getRootSpan();

Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/ExampleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Vinelab\Tracing\Tests\Fixtures;

use Vinelab\Tracing\Contracts\ShouldBeTraced;

class ExampleCommand implements ShouldBeTraced
{

}
46 changes: 46 additions & 0 deletions tests/Zipkin/TraceCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Vinelab\Tracing\Tests\Zipkin;

use Illuminate\Console\Events\CommandStarting;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Arr;
use Mockery;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
use Vinelab\Tracing\Listeners\TraceCommand;
use Vinelab\Tracing\Tests\Fixtures\ExampleCommand;
use Vinelab\Tracing\Tests\Fixtures\NoopReporter;

class TraceCommandTest extends TestCase
{
use InteractsWithZipkin;

/** @test */
public function trace_console_command()
{
$reporter = Mockery::spy(NoopReporter::class);
$tracer = $this->createTracer($reporter);

$artisan = Mockery::mock(Kernel::class);
$artisan->shouldReceive('all')->andReturn([
'example' => new ExampleCommand(),
]);

$listener = new TraceCommand($tracer, $artisan);
$listener->handle(new CommandStarting('example', new ArrayInput(['test']), new DummyOutput()));

$tracer->flush();

$reporter->shouldHaveReceived('report')->with(Mockery::on(function ($spans) {
$span = $this->shiftSpan($spans);

$this->assertEquals('Console Command', Arr::get($span, 'name'));
$this->assertEquals('cli', Arr::get($span, 'tags.type'));
$this->assertContains('phpunit', Arr::get($span, 'tags.argv'));

return true;
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Vinelab\Tracing\Middleware\TraceRequests;
use Vinelab\Tracing\Tests\Fixtures\NoopReporter;

class MiddlewareTest extends TestCase
class TraceRequestsTest extends TestCase
{
use InteractsWithZipkin;

Expand Down