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

Commit f3017ce

Browse files
authored
Async by default if env var is set (#6)
* Async by default if env var is set * Fix tests to use batchRunner
1 parent d5f0a51 commit f3017ce

File tree

3 files changed

+40
-44
lines changed

3 files changed

+40
-44
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,21 @@ Trace data to the [Stackdriver Trace][stackdriver-trace] service.
2626
2727
## Customization
2828
29-
TODO: Fill out these instructions
29+
You may provide an associative array of options to the `StackdriverExporter` at
30+
initialization:
31+
32+
```php
33+
$options = [];
34+
$exporter = new StackdriverExporter($options);
35+
```
36+
37+
The following options are available:
38+
39+
| Option | Default | Description |
40+
| ------ | ------- | ----------- |
41+
| `client` | `new TraceClient($clientConfig)` | A configured [`TraceClient`][trace-client] to use to export traces |
42+
| `clientConfig` | `[]` | Options to pass to the default TraceClient |
43+
3044
3145
## Versioning
3246
@@ -74,3 +88,5 @@ This is not an official Google product.
7488
[census-org]: https://github.com/census-instrumentation
7589
[composer]: https://getcomposer.org/
7690
[semver]: http://semver.org/
91+
[trace-client]: https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.59.0/trace/traceclient
92+
[google-cloud-php]: https://github.com/GoogleCloudPlatform/google-cloud-php

src/StackdriverExporter.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,8 @@
4747
* Stackdriver Trace. You can enable an experimental asynchronous reporting
4848
* mechanism using
4949
* <a href="https://github.com/GoogleCloudPlatform/google-cloud-php/tree/master/src/Core/Batch">BatchDaemon</a>.
50-
*
51-
* Example:
52-
* ```
53-
* use OpenCensus\Trace\Tracer;
54-
* use OpenCensus\Trace\Exporter\StackdriverExporter;
55-
*
56-
* $reporter = new StackdriverExporter([
57-
* 'async' => true,
58-
* 'clientConfig' => [
59-
* 'projectId' => 'my-project'
60-
* ]
61-
* ]);
62-
* Tracer::start($reporter);
63-
* ```
64-
*
65-
* Note that to use the `async` option, you will also need to set the
50+
* To enable asynchronous exporting, set the
6651
* `IS_BATCH_DAEMON_RUNNING` environment variable to `true`.
67-
*
68-
* @experimental The experimental flag means that while we believe this method
69-
* or class is ready for use, it may change before release in backwards-
70-
* incompatible ways. Please use with caution, and test thoroughly when
71-
* upgrading.
7252
*/
7353
class StackdriverExporter implements ExporterInterface
7454
{
@@ -81,11 +61,6 @@ class StackdriverExporter implements ExporterInterface
8161
*/
8262
private static $client;
8363

84-
/**
85-
* @var bool
86-
*/
87-
private $async;
88-
8964
/**
9065
* @var array
9166
*/
@@ -116,16 +91,13 @@ class StackdriverExporter implements ExporterInterface
11691
* BatchRunner.
11792
* @type string $identifier An identifier for the batch job.
11893
* **Defaults to** `stackdriver-trace`.
119-
* @type bool $async Whether we should try to use the batch runner.
120-
* **Defaults to** `false`.
12194
*/
12295
public function __construct(array $options = [])
12396
{
12497
$options += [
12598
'async' => false,
12699
'client' => null
127100
];
128-
$this->async = $options['async'];
129101
$this->setCommonBatchProperties($options + [
130102
'identifier' => 'stackdriver-trace',
131103
'batchMethod' => 'insertBatch'
@@ -160,11 +132,7 @@ public function export(array $spans)
160132
$trace->setSpans($spans);
161133

162134
try {
163-
if ($this->async) {
164-
return $this->batchRunner->submitItem($this->identifier, $trace);
165-
} else {
166-
return self::$client->insert($trace);
167-
}
135+
return $this->batchRunner->submitItem($this->identifier, $trace);
168136
} catch (\Exception $e) {
169137
error_log('Reporting the Trace data failed: ' . $e->getMessage());
170138
return false;

tests/unit/StackdriverExporterTest.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use OpenCensus\Trace\Tracer\ContextTracer;
2626
use OpenCensus\Trace\Span as OCSpan;
2727
use Prophecy\Argument;
28+
use Google\Cloud\Core\Batch\BatchRunner;
2829
use Google\Cloud\Trace\Trace;
2930
use Google\Cloud\Trace\Span;
3031
use Google\Cloud\Trace\TraceClient;
@@ -63,15 +64,20 @@ public function setUp()
6364

6465
public function testReportWithAnExceptionErrorLog()
6566
{
66-
$this->client->insert(Argument::any())->willThrow(
67-
new \Exception('error_log test')
68-
);
6967
$trace = $this->prophesize(Trace::class);
7068
$trace->setSpans(Argument::any())->shouldBeCalled();
7169
$this->client->trace(Argument::any())->willReturn($trace->reveal());
72-
$exporter = new StackdriverExporter(
73-
['client' => $this->client->reveal()]
74-
);
70+
71+
$batchRunner = $this->prophesize(BatchRunner::class);
72+
$batchRunner->registerJob(Argument::any(), Argument::any(), Argument::any())->shouldBeCalled();
73+
$batchRunner->submitItem(Argument::any(), Argument::any())->willThrow(
74+
new \Exception('error_log test')
75+
)->shouldBeCalled();
76+
77+
$exporter = new StackdriverExporter([
78+
'client' => $this->client->reveal(),
79+
'batchRunner' => $batchRunner->reveal()
80+
]);
7581
$this->expectOutputString(
7682
'Reporting the Trace data failed: error_log test'
7783
);
@@ -95,16 +101,22 @@ public function testReportsVersionAttribute()
95101
return true;
96102
}))->shouldBeCalled();
97103
$this->client->trace('aaa')->willReturn($trace->reveal());
98-
$this->client->insert(Argument::type(Trace::class))
99-
->willReturn(true)->shouldBeCalled();
100104

101105
$span = new OCSpan([
102106
'traceId' => 'aaa'
103107
]);
104108
$span->setStartTime();
105109
$span->setEndTime();
106110

107-
$exporter = new StackdriverExporter(['client' => $this->client->reveal()]);
111+
$batchRunner = $this->prophesize(BatchRunner::class);
112+
$batchRunner->registerJob(Argument::any(), Argument::any(), Argument::any())->shouldBeCalled();
113+
$batchRunner->submitItem(Argument::any(), Argument::any())
114+
->willReturn(true)
115+
->shouldBeCalled();
116+
$exporter = new StackdriverExporter([
117+
'client' => $this->client->reveal(),
118+
'batchRunner' => $batchRunner->reveal()
119+
]);
108120
$this->assertTrue($exporter->export([$span->spanData()]));
109121
}
110122
}

0 commit comments

Comments
 (0)