Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 39585f2

Browse files
klibbbschingor13
authored andcommitted
Only set status of the root span on exit if an HTTP response code has been set (#194)
* Only set status of the root span on exit if an HTTP response code has been set * Account for the fact that there is no analogue for the span status field in the PECL
1 parent 21824d2 commit 39585f2

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed

src/Trace/RequestHandler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,12 @@ public function addMessageEvent($type, $id, $options)
261261

262262
public function addCommonRequestAttributes(array $headers)
263263
{
264-
$responseCode = http_response_code();
265-
$this->rootSpan->setStatus($responseCode, "HTTP status code: $responseCode");
266-
$this->tracer->addAttribute(Span::ATTRIBUTE_STATUS_CODE, $responseCode, [
267-
'spanId' => $this->rootSpan->spanId()
268-
]);
264+
if ($responseCode = http_response_code()) {
265+
$this->rootSpan->setStatus($responseCode, "HTTP status code: $responseCode");
266+
$this->tracer->addAttribute(Span::ATTRIBUTE_STATUS_CODE, $responseCode, [
267+
'spanId' => $this->rootSpan->spanId()
268+
]);
269+
}
269270
foreach (self::ATTRIBUTE_MAP as $attributeKey => $headerKeys) {
270271
if ($val = $this->detectKey($headerKeys, $headers)) {
271272
$this->tracer->addAttribute($attributeKey, $val, [

tests/unit/Trace/RequestHandlerTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@
1717

1818
namespace OpenCensus\Tests\Unit\Trace;
1919

20+
require_once __DIR__ . '/mock_http_response_code.php';
21+
2022
use OpenCensus\Trace\Annotation;
2123
use OpenCensus\Trace\Link;
2224
use OpenCensus\Trace\MessageEvent;
2325
use OpenCensus\Trace\Span;
2426
use OpenCensus\Trace\SpanContext;
2527
use OpenCensus\Trace\SpanData;
28+
use OpenCensus\Trace\Status;
2629
use OpenCensus\Trace\RequestHandler;
2730
use OpenCensus\Trace\Exporter\ExporterInterface;
2831
use OpenCensus\Trace\Sampler\SamplerInterface;
2932
use OpenCensus\Trace\Tracer\NullTracer;
3033
use OpenCensus\Trace\Propagator\HttpHeaderPropagator;
34+
use OpenCensus\Trace\MockHttpResponseCode;
3135
use PHPUnit\Framework\TestCase;
3236

3337
/**
@@ -663,4 +667,56 @@ public function testAddsMessageEventToSpecificDetachedSpan()
663667
$this->assertEquals(123, $messageEvent->compressedSize());
664668
$this->assertEquals(234, $messageEvent->uncompressedSize());
665669
}
670+
671+
public function testNoStatusOfRootSpanOnExitWithoutHttpResponse()
672+
{
673+
$this->sampler->shouldSample()->willReturn(true);
674+
$rt = new RequestHandler(
675+
$this->exporter->reveal(),
676+
$this->sampler->reveal(),
677+
new HttpHeaderPropagator(),
678+
[
679+
'skipReporting' => true
680+
]
681+
);
682+
MockHttpResponseCode::$status = false;
683+
$rt->onExit();
684+
$spans = $rt->tracer()->spans();
685+
$this->assertCount(1, $spans);
686+
$spanData = $spans[0];
687+
$this->assertInstanceOf(SpanData::class, $spanData);
688+
$this->assertNotEmpty($spanData->endTime());
689+
$this->assertEquals('main', $spanData->name());
690+
$this->assertEquals([], $spanData->attributes());
691+
$this->assertNull($spanData->status());
692+
}
693+
694+
public function testSetsStatusOfRootSpanOnExitWithHttpResponse()
695+
{
696+
$this->sampler->shouldSample()->willReturn(true);
697+
$rt = new RequestHandler(
698+
$this->exporter->reveal(),
699+
$this->sampler->reveal(),
700+
new HttpHeaderPropagator(),
701+
[
702+
'skipReporting' => true
703+
]
704+
);
705+
MockHttpResponseCode::$status = 200;
706+
$rt->onExit();
707+
$spans = $rt->tracer()->spans();
708+
$this->assertCount(1, $spans);
709+
$spanData = $spans[0];
710+
$this->assertInstanceOf(SpanData::class, $spanData);
711+
$this->assertNotEmpty($spanData->endTime());
712+
$this->assertEquals('main', $spanData->name());
713+
$this->assertEquals([Span::ATTRIBUTE_STATUS_CODE => 200], $spanData->attributes());
714+
715+
if (extension_loaded('opencensus')) {
716+
$this->assertNull($spanData->status());
717+
} else {
718+
$this->assertInstanceOf(Status::class, $spanData->status());
719+
$this->assertEquals(200, $spanData->status()->code());
720+
}
721+
}
666722
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensus Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCensus\Trace;
19+
20+
/**
21+
* A mock function for testing the http_response_code function.
22+
* See http://us2.php.net/manual/en/function.http-response-code.php
23+
*/
24+
25+
function http_response_code($status = 0)
26+
{
27+
if (php_sapi_name() === 'cli') {
28+
if ($status) {
29+
MockHttpResponseCode::$status = $status;
30+
return true;
31+
} else {
32+
return MockHttpResponseCode::$status ?: false;
33+
}
34+
} else {
35+
$last_status = MockHttpResponseCode::$status ?: 200;
36+
if ($status) {
37+
MockHttpResponseCode::$status = $status;
38+
}
39+
return $last_status;
40+
}
41+
}
42+
43+
/**
44+
* A class for overriding the return value of the mocked http_response_code function.
45+
*/
46+
47+
class MockHttpResponseCode
48+
{
49+
public static $status = null;
50+
}

0 commit comments

Comments
 (0)