Skip to content

Commit

Permalink
Merge pull request #34 from eliatcodecov/fix-no-status
Browse files Browse the repository at this point in the history
Fix no status
  • Loading branch information
eliatcodecov authored Jul 11, 2022
2 parents 0f510ad + 485b568 commit 268960b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
"php-http/mock-client": "^1.5",
"php-http/message": "^1.0",
"php-http/guzzle7-adapter": "^1.0"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
}
15 changes: 11 additions & 4 deletions src/Middleware/Trace.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,15 @@ public function handle($request, Closure $next)
$span->setAttribute('codecov.coverage', $coverage);
}

$this->setSpanStatus($span, $response->status());
$this->addConfiguredTags($span, $request, $response);
//In the event we can't get a status, we just use code 520.
if(!method_exists($response, 'status')) {
$status = '520';
} else {
$status = $response->status();
}

$this->setSpanStatus($span, $status);
$this->addConfiguredTags($span, $request, $response, $status);

$uri = $request->route() ? $request->route()->uri() : null;
if($uri) {
Expand All @@ -105,9 +112,9 @@ private function setSpanStatus(Span $span, int $httpStatusCode)
}
}

private function addConfiguredTags(Span $span, Request $request, $response)
private function addConfiguredTags(Span $span, Request $request, $response, $status)
{
$span->setAttribute('http.status_code', $response->status() ?? 'not passed');
$span->setAttribute('http.status_code', $status ?? 'not passed');
$span->setAttribute('http.method', $request->method()) ?? 'not passed';
$span->setAttribute('http.host', $request->root() ?? 'not passed');
$span->setAttribute('http.target', '/'.$request->path() ?? 'not passed');
Expand Down
84 changes: 84 additions & 0 deletions tests/Unit/TraceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

use Orchestra\Testbench\TestCase;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

use Codecov\LaravelCodecovOpenTelemetry\Codecov\Exporter as CodecovExporter;
use OpenTelemetry\Sdk\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\Sdk\Trace\TracerProvider;


use Codecov\LaravelCodecovOpenTelemetry\Middleware\Trace as CodecovTrace;


uses(TestCase::class);

beforeEach(function () {
$config = testConfig();
config($config);
});

it('properly handles no status response', function(){
//mock a binary response.
$response = Mockery::mock(BinaryFileResponse::class);

$request = Request::create('/example', 'GET');

$provider = new TracerProvider();

$exporter = new CodecovExporter(
config('laravel_codecov_opentelemetry.service_name'),
config('laravel_codecov_opentelemetry.codecov_host'),
config('laravel_codecov_opentelemetry.profiling_token')
);

$tracer = $provider
->addSpanProcessor(new SimpleSpanProcessor($exporter))
->getTracer('io.opentelemetry.contrib.php')
;

$traceMiddleware = new CodecovTrace($tracer);

$newResp = $traceMiddleware->handle($request, function() use($response) {
return $response;
});

//make sure we get our mocked response back and handle() hasn't blown up.
expect($newResp)->toBe($response);

});

it('properly handles a standard response', function(){
//mock a binary response.
$response = Mockery::mock(Response::class)
->shouldReceive('status')
->andReturn('200');

$request = Request::create('/example', 'GET');

$provider = new TracerProvider();

$exporter = new CodecovExporter(
config('laravel_codecov_opentelemetry.service_name'),
config('laravel_codecov_opentelemetry.codecov_host'),
config('laravel_codecov_opentelemetry.profiling_token')
);

$tracer = $provider
->addSpanProcessor(new SimpleSpanProcessor($exporter))
->getTracer('io.opentelemetry.contrib.php')
;

$traceMiddleware = new CodecovTrace($tracer);

$newResp = $traceMiddleware->handle($request, function() use($response) {
return $response;
});

//make sure we get our mocked response back and handle() hasn't blown up.
expect($newResp)->toBe($response);

});

0 comments on commit 268960b

Please sign in to comment.