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

Commit b37a9c2

Browse files
authored
StackdriverExporter should report g.co/agent attribute (#4)
1 parent 4c1844e commit b37a9c2

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/StackdriverExporter.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Google\Cloud\Trace\Trace;
2525
use OpenCensus\Trace\SpanData;
2626
use OpenCensus\Trace\Exporter\Stackdriver\SpanConverter;
27+
use OpenCensus\Version;
2728

2829
/**
2930
* This implementation of the ExporterInterface use the BatchRunner to provide
@@ -71,6 +72,8 @@
7172
*/
7273
class StackdriverExporter implements ExporterInterface
7374
{
75+
const AGENT = 'g.co/agent';
76+
7477
use BatchTrait;
7578

7679
/**
@@ -144,8 +147,13 @@ public function export(array $spans)
144147

145148
// Pull the traceId from the first span
146149
$spans = array_map([SpanConverter::class, 'convertSpan'], $spans);
150+
$rootSpan = $spans[0];
147151
$trace = self::$client->trace(
148-
$spans[0]->traceId()
152+
$rootSpan->traceId()
153+
);
154+
$rootSpan->addAttribute(
155+
self::AGENT,
156+
sprintf('opencensus-php [%s]', Version::VERSION)
149157
);
150158

151159
// build a Trace object and assign Spans

tests/unit/StackdriverExporterTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,28 @@ public function testEmptyTrace()
8383
$exporter = new StackdriverExporter(['client' => $this->client->reveal()]);
8484
$this->assertFalse($exporter->export([]));
8585
}
86+
87+
public function testReportsVersionAttribute()
88+
{
89+
$trace = $this->prophesize(Trace::class);
90+
$trace->setSpans(Argument::that(function ($spans) {
91+
$this->assertCount(1, $spans);
92+
$attributes = $spans[0]->jsonSerialize()['attributes'];
93+
$this->assertArrayHasKey('g.co/agent', $attributes);
94+
$this->assertRegexp('/\d+\.\d+\.\d+/', $attributes['g.co/agent']);
95+
return true;
96+
}))->shouldBeCalled();
97+
$this->client->trace('aaa')->willReturn($trace->reveal());
98+
$this->client->insert(Argument::type(Trace::class))
99+
->willReturn(true)->shouldBeCalled();
100+
101+
$span = new OCSpan([
102+
'traceId' => 'aaa'
103+
]);
104+
$span->setStartTime();
105+
$span->setEndTime();
106+
107+
$exporter = new StackdriverExporter(['client' => $this->client->reveal()]);
108+
$this->assertTrue($exporter->export([$span->spanData()]));
109+
}
86110
}

0 commit comments

Comments
 (0)