From bbbf7a752a2d1063d34548ad07f236d6b7cada3f Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Tue, 3 Apr 2018 13:49:07 -0700 Subject: [PATCH] ExtensionTracer should set traceId on spans (#157) * Failing test for extension not preserving traceId * ExtensionTracer correctly sets traceId on returned spans --- src/Trace/Tracer/ExtensionTracer.php | 9 +++++++-- tests/unit/Trace/Tracer/AbstractTracerTest.php | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Trace/Tracer/ExtensionTracer.php b/src/Trace/Tracer/ExtensionTracer.php index 98960e025..82f73b292 100644 --- a/src/Trace/Tracer/ExtensionTracer.php +++ b/src/Trace/Tracer/ExtensionTracer.php @@ -93,6 +93,7 @@ public function withSpan(Span $span) ? (float)($spanData->startTime()->format('U.u')) : microtime(true); $info = [ + 'traceId' => $spanData->traceId(), 'spanId' => $spanData->spanId(), 'parentSpanId' => $spanData->parentSpanId(), 'startTime' => $startTime, @@ -117,7 +118,10 @@ public function spans() { // each span returned from opencensus_trace_list should be a // OpenCensus\Span object - return array_map([$this, 'mapSpan'], opencensus_trace_list()); + $traceId = $this->spanContext()->traceId(); + return array_map(function ($span) use ($traceId) { + return $this->mapSpan($span, $traceId); + }, opencensus_trace_list()); } /** @@ -246,9 +250,10 @@ private function generateSpanName() return uniqid('span'); } - private function mapSpan($span) + private function mapSpan($span, $traceId) { return new Span([ + 'traceId' => $traceId, 'name' => $span->name(), 'spanId' => $span->spanId(), 'parentSpanId' => $span->parentSpanId(), diff --git a/tests/unit/Trace/Tracer/AbstractTracerTest.php b/tests/unit/Trace/Tracer/AbstractTracerTest.php index 1c89c5634..3dc3bf7dc 100644 --- a/tests/unit/Trace/Tracer/AbstractTracerTest.php +++ b/tests/unit/Trace/Tracer/AbstractTracerTest.php @@ -48,6 +48,7 @@ public function testMaintainsContext() $spans = $tracer->spans(); $this->assertCount(1, $spans); $spanData = $spans[0]->spanData(); + $this->assertEquals('traceid', $spanData->traceId()); $this->assertEquals('test', $spanData->name()); $this->assertEquals($parentSpanId, $spanData->parentSpanId()); }