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

Commit 33671e5

Browse files
authored
Fix extension stacktrace report (#23)
* Run php tests with and without the extension installed * Fix ExtensionTracer tests * Update ContextTracerTest to match the ExtensionTracerTest for maintaining context * Add failing test for persisting backtrace * Persist and filter the backtrace when using ExtensionTracer
1 parent 30bfa18 commit 33671e5

5 files changed

Lines changed: 42 additions & 17 deletions

File tree

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ WORKDIR /build
4343

4444
RUN composer install && \
4545
vendor/bin/phpcs --standard=./phpcs-ruleset.xml && \
46-
vendor/bin/phpunit
46+
vendor/bin/phpunit && \
47+
php -d extension=opencensus.so vendor/bin/phpunit

src/Trace/TraceSpan.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ public function __construct($options = [])
8383
}
8484

8585
if (array_key_exists('backtrace', $options)) {
86-
$this->info['backtrace'] = $options['backtrace'];
86+
$this->info['backtrace'] = $this->filterBacktrace($options['backtrace']);
8787
unset($options['backtrace']);
8888
} else {
89-
$this->info['backtrace'] = $this->generateBacktrace();
89+
$this->info['backtrace'] = $this->filterBacktrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
9090
}
9191

9292
if (array_key_exists('kind', $options)) {
@@ -298,10 +298,10 @@ private function generateSpanId()
298298
*
299299
* @return array
300300
*/
301-
private function generateBacktrace()
301+
private function filterBacktrace($backtrace)
302302
{
303303
return array_values(
304-
array_filter(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($bt) {
304+
array_filter($backtrace, function ($bt) {
305305
return !array_key_exists('class', $bt) || substr($bt['class'], 0, 16) != 'OpenCensus\Trace';
306306
})
307307
);

src/Trace/Tracer/ExtensionTracer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ public function spans()
114114
'parentSpanId' => $span->parentSpanId(),
115115
'startTime' => $span->startTime(),
116116
'endTime' => $span->endTime(),
117-
'labels' => $span->labels()
117+
'labels' => $span->labels(),
118+
'backtrace' => $span->backtrace()
118119
]);
119120
}, opencensus_trace_list());
120121
}

tests/unit/Trace/Tracer/ContextTracerTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,25 @@ class ContextTracerTest extends \PHPUnit_Framework_TestCase
2727
{
2828
public function testMaintainsContext()
2929
{
30-
$initialContext = new TraceContext('traceid', 'spanid');
30+
$parentSpanId = 12345;
31+
$initialContext = new TraceContext('traceid', $parentSpanId);
3132

3233
$tracer = new ContextTracer($initialContext);
3334
$context = $tracer->context();
3435

3536
$this->assertEquals('traceid', $context->traceId());
36-
$this->assertEquals('spanid', $context->spanId());
37+
$this->assertEquals($parentSpanId, $context->spanId());
3738

38-
$tracer->inSpan(['name' => 'test'], function() use ($tracer) {
39+
$tracer->inSpan(['name' => 'test'], function () use ($tracer, $parentSpanId) {
3940
$context = $tracer->context();
40-
$this->assertNotEquals('spanid', $context->spanId());
41+
$this->assertNotEquals($parentSpanId, $context->spanId());
4142
});
4243

4344
$spans = $tracer->spans();
4445
$this->assertCount(1, $spans);
4546
$span = $spans[0];
4647
$this->assertEquals('test', $span->name());
47-
$this->assertEquals('spanid', $span->parentSpanId());
48+
$this->assertEquals($parentSpanId, $span->parentSpanId());
4849
}
4950

5051
public function testAddsLabelsToCurrentSpan()
@@ -80,4 +81,14 @@ public function testAddsLabelsToRootSpan()
8081
$info = $span->info();
8182
$this->assertEquals('bar', $info['labels']['foo']);
8283
}
84+
85+
public function testPersistsBacktrace()
86+
{
87+
$tracer = new ContextTracer();
88+
$tracer->inSpan(['name' => 'test'], function () {});
89+
$span = $tracer->spans()[0];
90+
$stackframe = $span->backtrace()[0];
91+
$this->assertEquals('testPersistsBacktrace', $stackframe['function']);
92+
$this->assertEquals(self::class, $stackframe['class']);
93+
}
8394
}

tests/unit/Trace/Tracer/ExtentionTracerTest.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,32 @@ class ExtensionTracerTest extends \PHPUnit_Framework_TestCase
2828
public function setUp()
2929
{
3030
if (!extension_loaded('opencensus')) {
31-
$this->markTestSkipped('Must have the stackdriver_trace extension installed to run this test.');
31+
$this->markTestSkipped('Must have the opencensus extension installed to run this test.');
3232
}
33+
opencensus_trace_clear();
3334
}
3435

3536
public function testMaintainsContext()
3637
{
37-
$initialContext = new TraceContext('traceid', 'spanid');
38+
$parentSpanId = 12345;
39+
$initialContext = new TraceContext('traceid', $parentSpanId);
3840

3941
$tracer = new ExtensionTracer($initialContext);
4042
$context = $tracer->context();
4143

4244
$this->assertEquals('traceid', $context->traceId());
43-
$this->assertEquals('spanid', $context->spanId());
45+
$this->assertEquals($parentSpanId, $context->spanId());
4446

45-
$tracer->inSpan(['name' => 'test'], function() use ($tracer) {
47+
$tracer->inSpan(['name' => 'test'], function () use ($tracer, $parentSpanId) {
4648
$context = $tracer->context();
47-
$this->assertNotEquals('spanid', $context->spanId());
49+
$this->assertNotEquals($parentSpanId, $context->spanId());
4850
});
4951

5052
$spans = $tracer->spans();
5153
$this->assertCount(1, $spans);
5254
$span = $spans[0];
5355
$this->assertEquals('test', $span->name());
54-
$this->assertEquals('spanid', $span->parentSpanId());
56+
$this->assertEquals($parentSpanId, $span->parentSpanId());
5557
}
5658

5759
public function testAddsLabelsToCurrentSpan()
@@ -87,4 +89,14 @@ public function testAddsLabelsToRootSpan()
8789
$info = $span->info();
8890
$this->assertEquals('bar', $info['labels']['foo']);
8991
}
92+
93+
public function testPersistsBacktrace()
94+
{
95+
$tracer = new ExtensionTracer();
96+
$tracer->inSpan(['name' => 'test'], function () {});
97+
$span = $tracer->spans()[0];
98+
$stackframe = $span->backtrace()[0];
99+
$this->assertEquals('testPersistsBacktrace', $stackframe['function']);
100+
$this->assertEquals(self::class, $stackframe['class']);
101+
}
90102
}

0 commit comments

Comments
 (0)