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

Commit ca25c80

Browse files
authored
Add Version::VERSION constant. StackdriverExporter reports version attribute (#160)
1 parent c878902 commit ca25c80

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

RELEASING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ The PHP library and extension are released independently of each other.
44

55
## Packagist Package
66

7+
1. Bump `VERSION` constant in [`src/Version.php`][version-file]
8+
79
1. Create a GitHub release.
810

911
1. Click `Update` from the admin view of the [opencensus/opencensus][packagist] package.
@@ -28,5 +30,6 @@ The PHP library and extension are released independently of each other.
2830

2931
1. Upload the new release to PECL from the [admin console][pecl-upload].
3032

33+
[version-file]: https://github.com/census-instrumentation/opencensus-php/tree/master/src/Version.php
3134
[packagist]: https://packagist.org/packages/opencensus/opencensus
3235
[pecl-upload]: https://pecl.php.net/release-upload.php

src/Trace/Exporter/StackdriverExporter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Google\Cloud\Trace\Trace;
2525
use OpenCensus\Trace\Span as OCSpan;
2626
use OpenCensus\Trace\SpanData;
27+
use OpenCensus\Version;
2728

2829
/**
2930
* This implementation of the ExporterInterface use the BatchRunner to provide
@@ -79,6 +80,7 @@ class StackdriverExporter implements ExporterInterface
7980
OCSpan::ATTRIBUTE_USER_AGENT => '/http/user_agent',
8081
OCSpan::ATTRIBUTE_STATUS_CODE => '/http/status_code'
8182
];
83+
const AGENT = 'g.co/agent';
8284

8385
use BatchTrait;
8486

@@ -147,8 +149,13 @@ public function export(array $spans)
147149
}
148150

149151
// Pull the traceId from the first span
152+
$rootSpan = $spans[0];
150153
$trace = self::$client->trace(
151-
$spans[0]->traceId()
154+
$rootSpan->traceId()
155+
);
156+
$rootSpan->addAttribute(
157+
self::AGENT,
158+
sprintf('opencensus-php [%s]', Version::VERSION)
152159
);
153160

154161
// build a Trace object and assign Spans
@@ -172,7 +179,8 @@ public function export(array $spans)
172179
* @access private
173180
*
174181
* @param SpanData[] $spans
175-
* @return Span[] Representation of the collected trace spans ready for serialization
182+
* @return Span[] Representation of the collected trace spans ready for
183+
* serialization
176184
*/
177185
public function convertSpans(array $spans)
178186
{

src/Version.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensus Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCensus;
19+
20+
/**
21+
* This class provides a constant for the current version of this library.
22+
*/
23+
class Version
24+
{
25+
const VERSION = '0.4.1';
26+
}

tests/unit/Trace/Exporter/StackdriverExporterTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,31 @@ public function attributesToTest()
163163
['http.user_agent', 'user agent', '/http/user_agent', 'user agent']
164164
];
165165
}
166+
167+
public function testReportsVersionAttribute()
168+
{
169+
$trace = $this->prophesize(Trace::class);
170+
$trace->setSpans(Argument::that(function ($spans) {
171+
$this->assertCount(1, $spans);
172+
$attributes = $spans[0]->jsonSerialize()['attributes'];
173+
$this->assertArrayHasKey('g.co/agent', $attributes);
174+
$this->assertRegexp('/\d+\.\d+\.\d+/', $attributes['g.co/agent']);
175+
return true;
176+
}))->shouldBeCalled();
177+
$this->client->trace('aaa')->willReturn($trace->reveal());
178+
$this->client->insert(Argument::type(Trace::class))
179+
->willReturn(true)->shouldBeCalled();
180+
181+
$span = new OCSpan([
182+
'traceId' => 'aaa',
183+
'attributes' => [
184+
$key => $value
185+
]
186+
]);
187+
$span->setStartTime();
188+
$span->setEndTime();
189+
190+
$exporter = new StackdriverExporter(['client' => $this->client->reveal()]);
191+
$this->assertTrue($exporter->export([$span->spanData()]));
192+
}
166193
}

0 commit comments

Comments
 (0)