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

Commit bbbf7a7

Browse files
authored
ExtensionTracer should set traceId on spans (#157)
* Failing test for extension not preserving traceId * ExtensionTracer correctly sets traceId on returned spans
1 parent 6255978 commit bbbf7a7

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Trace/Tracer/ExtensionTracer.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function withSpan(Span $span)
9393
? (float)($spanData->startTime()->format('U.u'))
9494
: microtime(true);
9595
$info = [
96+
'traceId' => $spanData->traceId(),
9697
'spanId' => $spanData->spanId(),
9798
'parentSpanId' => $spanData->parentSpanId(),
9899
'startTime' => $startTime,
@@ -117,7 +118,10 @@ public function spans()
117118
{
118119
// each span returned from opencensus_trace_list should be a
119120
// OpenCensus\Span object
120-
return array_map([$this, 'mapSpan'], opencensus_trace_list());
121+
$traceId = $this->spanContext()->traceId();
122+
return array_map(function ($span) use ($traceId) {
123+
return $this->mapSpan($span, $traceId);
124+
}, opencensus_trace_list());
121125
}
122126

123127
/**
@@ -246,9 +250,10 @@ private function generateSpanName()
246250
return uniqid('span');
247251
}
248252

249-
private function mapSpan($span)
253+
private function mapSpan($span, $traceId)
250254
{
251255
return new Span([
256+
'traceId' => $traceId,
252257
'name' => $span->name(),
253258
'spanId' => $span->spanId(),
254259
'parentSpanId' => $span->parentSpanId(),

tests/unit/Trace/Tracer/AbstractTracerTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function testMaintainsContext()
4848
$spans = $tracer->spans();
4949
$this->assertCount(1, $spans);
5050
$spanData = $spans[0]->spanData();
51+
$this->assertEquals('traceid', $spanData->traceId());
5152
$this->assertEquals('test', $spanData->name());
5253
$this->assertEquals($parentSpanId, $spanData->parentSpanId());
5354
}

0 commit comments

Comments
 (0)