diff --git a/composer.json b/composer.json index e179aa0..925266e 100644 --- a/composer.json +++ b/composer.json @@ -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 + } } -} \ No newline at end of file +} diff --git a/src/Middleware/Trace.php b/src/Middleware/Trace.php index a36eb4a..c4d0724 100644 --- a/src/Middleware/Trace.php +++ b/src/Middleware/Trace.php @@ -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) { @@ -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'); diff --git a/tests/Unit/TraceTest.php b/tests/Unit/TraceTest.php new file mode 100644 index 0000000..774410e --- /dev/null +++ b/tests/Unit/TraceTest.php @@ -0,0 +1,84 @@ +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); + +}); +