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

Commit fb8bfc8

Browse files
authored
Exporters should handle empty traces (#107)
* Add test for empty trace * Skip setting common root span elements if there are no spans * Add test for empty trace for ZipkinExporter * Fix ZipkinExporter when the trace is empty
1 parent 94d73b2 commit fb8bfc8

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/Trace/Exporter/StackdriverExporter.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,16 @@ public function __construct(array $options = [])
156156
public function report(TracerInterface $tracer)
157157
{
158158
$this->processSpans($tracer);
159-
$trace = self::$client->trace(
160-
$tracer->spanContext()->traceId()
161-
);
162-
$spans = $this->convertSpans($tracer, $trace);
159+
$spans = $this->convertSpans($tracer);
163160

164161
if (empty($spans)) {
165162
return false;
166163
}
167164

165+
$trace = self::$client->trace(
166+
$tracer->spanContext()->traceId()
167+
);
168+
168169
// build a Trace object and assign Spans
169170
$trace->setSpans($spans);
170171

@@ -235,7 +236,11 @@ protected function getCallback()
235236
private function addCommonAttributes(&$tracer, $headers = null)
236237
{
237238
$headers = $headers ?: $_SERVER;
238-
$rootSpan = $tracer->spans()[0];
239+
$spans = $tracer->spans();
240+
if (empty($spans)) {
241+
return;
242+
}
243+
$rootSpan = $spans[0];
239244

240245
$attributeMap = [
241246
self::HTTP_URL => ['REQUEST_URI'],

src/Trace/Exporter/ZipkinExporter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,14 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
115115
{
116116
$headers = $headers ?: $_SERVER;
117117
$spans = $tracer->spans();
118-
$rootSpan = $spans[0];
119118
$traceId = $tracer->spanContext()->traceId();
120119

121120
// True is a request to store this span even if it overrides sampling policy.
122121
// This is true when the X-B3-Flags header has a value of 1.
123122
$isDebug = array_key_exists('HTTP_X_B3_FLAGS', $headers) && $headers['HTTP_X_B3_FLAGS'] == '1';
124123

125124
// True if we are contributing to a span started by another tracer (ex on a different host).
126-
$isShared = $rootSpan && $rootSpan->parentSpanId() != null;
125+
$isShared = !empty($spans) && $spans[0]->parentSpanId() != null;
127126

128127
$localEndpoint = [
129128
'serviceName' => $this->name,

tests/unit/Trace/Exporter/StackdriverExporterTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,12 @@ public function testStacktrace()
134134
$data = $spans[0]->jsonSerialize();
135135
$this->assertArrayHasKey('stackTrace', $data);
136136
}
137+
138+
public function testEmptyTrace()
139+
{
140+
$tracer = new ContextTracer(new SpanContext('testtraceid'));
141+
142+
$reporter = new StackdriverExporter(['client' => $this->client->reveal()]);
143+
$this->assertFalse($reporter->report($tracer));
144+
}
137145
}

tests/unit/Trace/Exporter/ZipkinExporterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,13 @@ public function testSpanShared()
127127
$this->assertCount(1, $spans);
128128
$this->assertTrue($spans[0]['shared']);
129129
}
130+
131+
public function testEmptyTrace()
132+
{
133+
$spanContext = new SpanContext('testtraceid', 12345);
134+
$tracer = new ContextTracer($spanContext);
135+
$reporter = new ZipkinExporter('myapp', 'localhost', 9411);
136+
$spans = $reporter->convertSpans($tracer);
137+
$this->assertEmpty($spans);
138+
}
130139
}

0 commit comments

Comments
 (0)