diff --git a/src/Trace/RequestHandler.php b/src/Trace/RequestHandler.php index e590c4cd8..bb1bc00c1 100644 --- a/src/Trace/RequestHandler.php +++ b/src/Trace/RequestHandler.php @@ -261,11 +261,12 @@ public function addMessageEvent($type, $id, $options) public function addCommonRequestAttributes(array $headers) { - $responseCode = http_response_code(); - $this->rootSpan->setStatus($responseCode, "HTTP status code: $responseCode"); - $this->tracer->addAttribute(Span::ATTRIBUTE_STATUS_CODE, $responseCode, [ - 'spanId' => $this->rootSpan->spanId() - ]); + if ($responseCode = http_response_code()) { + $this->rootSpan->setStatus($responseCode, "HTTP status code: $responseCode"); + $this->tracer->addAttribute(Span::ATTRIBUTE_STATUS_CODE, $responseCode, [ + 'spanId' => $this->rootSpan->spanId() + ]); + } foreach (self::ATTRIBUTE_MAP as $attributeKey => $headerKeys) { if ($val = $this->detectKey($headerKeys, $headers)) { $this->tracer->addAttribute($attributeKey, $val, [ diff --git a/tests/unit/Trace/RequestHandlerTest.php b/tests/unit/Trace/RequestHandlerTest.php index 988afaadf..9865e4d17 100644 --- a/tests/unit/Trace/RequestHandlerTest.php +++ b/tests/unit/Trace/RequestHandlerTest.php @@ -17,17 +17,21 @@ namespace OpenCensus\Tests\Unit\Trace; +require_once __DIR__ . '/mock_http_response_code.php'; + use OpenCensus\Trace\Annotation; use OpenCensus\Trace\Link; use OpenCensus\Trace\MessageEvent; use OpenCensus\Trace\Span; use OpenCensus\Trace\SpanContext; use OpenCensus\Trace\SpanData; +use OpenCensus\Trace\Status; use OpenCensus\Trace\RequestHandler; use OpenCensus\Trace\Exporter\ExporterInterface; use OpenCensus\Trace\Sampler\SamplerInterface; use OpenCensus\Trace\Tracer\NullTracer; use OpenCensus\Trace\Propagator\HttpHeaderPropagator; +use OpenCensus\Trace\MockHttpResponseCode; use PHPUnit\Framework\TestCase; /** @@ -663,4 +667,56 @@ public function testAddsMessageEventToSpecificDetachedSpan() $this->assertEquals(123, $messageEvent->compressedSize()); $this->assertEquals(234, $messageEvent->uncompressedSize()); } + + public function testNoStatusOfRootSpanOnExitWithoutHttpResponse() + { + $this->sampler->shouldSample()->willReturn(true); + $rt = new RequestHandler( + $this->exporter->reveal(), + $this->sampler->reveal(), + new HttpHeaderPropagator(), + [ + 'skipReporting' => true + ] + ); + MockHttpResponseCode::$status = false; + $rt->onExit(); + $spans = $rt->tracer()->spans(); + $this->assertCount(1, $spans); + $spanData = $spans[0]; + $this->assertInstanceOf(SpanData::class, $spanData); + $this->assertNotEmpty($spanData->endTime()); + $this->assertEquals('main', $spanData->name()); + $this->assertEquals([], $spanData->attributes()); + $this->assertNull($spanData->status()); + } + + public function testSetsStatusOfRootSpanOnExitWithHttpResponse() + { + $this->sampler->shouldSample()->willReturn(true); + $rt = new RequestHandler( + $this->exporter->reveal(), + $this->sampler->reveal(), + new HttpHeaderPropagator(), + [ + 'skipReporting' => true + ] + ); + MockHttpResponseCode::$status = 200; + $rt->onExit(); + $spans = $rt->tracer()->spans(); + $this->assertCount(1, $spans); + $spanData = $spans[0]; + $this->assertInstanceOf(SpanData::class, $spanData); + $this->assertNotEmpty($spanData->endTime()); + $this->assertEquals('main', $spanData->name()); + $this->assertEquals([Span::ATTRIBUTE_STATUS_CODE => 200], $spanData->attributes()); + + if (extension_loaded('opencensus')) { + $this->assertNull($spanData->status()); + } else { + $this->assertInstanceOf(Status::class, $spanData->status()); + $this->assertEquals(200, $spanData->status()->code()); + } + } } diff --git a/tests/unit/Trace/mock_http_response_code.php b/tests/unit/Trace/mock_http_response_code.php new file mode 100644 index 000000000..75ab5f5c9 --- /dev/null +++ b/tests/unit/Trace/mock_http_response_code.php @@ -0,0 +1,50 @@ +