Skip to content

Commit

Permalink
Merge pull request #4 from Vinelab/exclude_paths
Browse files Browse the repository at this point in the history
Allow to disable tracing for specified paths
  • Loading branch information
adiachenko authored Aug 30, 2019
2 parents 0a504fb + bb5f063 commit 45e2b3a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
12 changes: 9 additions & 3 deletions config/tracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

'service_name' => env('TRACING_SERVICE_NAME', 'example'),

'logging' => [
'content_types' => [
'application/json',
'middleware' => [
'excluded_paths' => [
//
],

'payload' => [
'content_types' => [
'application/json',
],
],
],

Expand Down
28 changes: 23 additions & 5 deletions src/Middleware/TraceRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Vinelab\Http\Response;
use Vinelab\Tracing\Contracts\Span;
use Vinelab\Tracing\Contracts\Tracer;
Expand Down Expand Up @@ -43,11 +44,13 @@ public function __construct(Tracer $tracer, Repository $config)
*/
public function handle(Request $request, Closure $next)
{
$spanContext = $this->tracer->extract($request, Formats::ILLUMINATE_HTTP);
if (!$this->shouldBeExcluded($request->path())) {
$spanContext = $this->tracer->extract($request, Formats::ILLUMINATE_HTTP);

$span = $this->tracer->startSpan('Http Request', $spanContext);
$span = $this->tracer->startSpan('Http Request', $spanContext);

$this->tagRequestData($span, $request);
$this->tagRequestData($span, $request);
}

return $next($request);
}
Expand Down Expand Up @@ -78,7 +81,7 @@ protected function tagRequestData(Span $span, Request $request): void
$span->tag('request_headers', strval($request->headers));
$span->tag('request_ip', $request->ip());

if (in_array($request->headers->get('Content_Type'), $this->config->get('tracing.logging.content_types'))) {
if (in_array($request->headers->get('Content_Type'), $this->config->get('tracing.middleware.payload.content_types'))) {
$span->tag('request_input', json_encode($request->input()));
}
}
Expand All @@ -97,8 +100,23 @@ protected function tagResponseData(Span $span, Request $request, $response): voi
$span->tag('response_status', strval($response->getStatusCode()));
$span->tag('response_headers', strval($response->headers));

if (in_array($response->headers->get('Content_Type'), $this->config->get('tracing.logging.content_types'))) {
if (in_array($response->headers->get('Content_Type'), $this->config->get('tracing.middleware.payload.content_types'))) {
$span->tag('response_content', $response->content());
}
}

/**
* @param string $path
* @return bool
*/
protected function shouldBeExcluded(string $path): bool
{
foreach ($this->config->get('tracing.middleware.excluded_paths') as $excludedPath) {
if (Str::is($excludedPath, $path)) {
return true;
}
}

return false;
}
}
30 changes: 28 additions & 2 deletions tests/Zipkin/TraceRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,42 @@ public function trace_http_requests()
}));
}

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

$request = Request::create('/users/1', 'GET', [], [], [], []);

$middleware = new TraceRequests($tracer, $this->mockConfig(['users/*']));
$middleware->handle($request, function ($req) {});
$middleware->terminate($request, new JsonResponse);

$tracer->flush();

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

return true;
}));
}

/**
* @param array $excludedPaths
* @return Repository|Mockery\LegacyMockInterface|Mockery\MockInterface
*/
protected function mockConfig()
protected function mockConfig(array $excludedPaths = [])
{
$config = Mockery::mock(Repository::class);
$config->shouldReceive('get')
->with('tracing.logging.content_types')
->with('tracing.middleware.payload.content_types')
->andReturn(['application/json']);

$config->shouldReceive('get')
->with('tracing.middleware.excluded_paths')
->andReturn($excludedPaths);

return $config;
}
}

0 comments on commit 45e2b3a

Please sign in to comment.