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

Commit

Permalink
Async by default if env var is set (#6)
Browse files Browse the repository at this point in the history
* Async by default if env var is set

* Fix tests to use batchRunner
  • Loading branch information
chingor13 committed Apr 19, 2018
1 parent d5f0a51 commit f3017ce
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 44 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ Trace data to the [Stackdriver Trace][stackdriver-trace] service.

## Customization

TODO: Fill out these instructions
You may provide an associative array of options to the `StackdriverExporter` at
initialization:

```php
$options = [];
$exporter = new StackdriverExporter($options);
```

The following options are available:

| Option | Default | Description |
| ------ | ------- | ----------- |
| `client` | `new TraceClient($clientConfig)` | A configured [`TraceClient`][trace-client] to use to export traces |
| `clientConfig` | `[]` | Options to pass to the default TraceClient |


## Versioning

Expand Down Expand Up @@ -74,3 +88,5 @@ This is not an official Google product.
[census-org]: https://github.com/census-instrumentation
[composer]: https://getcomposer.org/
[semver]: http://semver.org/
[trace-client]: https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.59.0/trace/traceclient
[google-cloud-php]: https://github.com/GoogleCloudPlatform/google-cloud-php
36 changes: 2 additions & 34 deletions src/StackdriverExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,8 @@
* Stackdriver Trace. You can enable an experimental asynchronous reporting
* mechanism using
* <a href="https://github.com/GoogleCloudPlatform/google-cloud-php/tree/master/src/Core/Batch">BatchDaemon</a>.
*
* Example:
* ```
* use OpenCensus\Trace\Tracer;
* use OpenCensus\Trace\Exporter\StackdriverExporter;
*
* $reporter = new StackdriverExporter([
* 'async' => true,
* 'clientConfig' => [
* 'projectId' => 'my-project'
* ]
* ]);
* Tracer::start($reporter);
* ```
*
* Note that to use the `async` option, you will also need to set the
* To enable asynchronous exporting, set the
* `IS_BATCH_DAEMON_RUNNING` environment variable to `true`.
*
* @experimental The experimental flag means that while we believe this method
* or class is ready for use, it may change before release in backwards-
* incompatible ways. Please use with caution, and test thoroughly when
* upgrading.
*/
class StackdriverExporter implements ExporterInterface
{
Expand All @@ -81,11 +61,6 @@ class StackdriverExporter implements ExporterInterface
*/
private static $client;

/**
* @var bool
*/
private $async;

/**
* @var array
*/
Expand Down Expand Up @@ -116,16 +91,13 @@ class StackdriverExporter implements ExporterInterface
* BatchRunner.
* @type string $identifier An identifier for the batch job.
* **Defaults to** `stackdriver-trace`.
* @type bool $async Whether we should try to use the batch runner.
* **Defaults to** `false`.
*/
public function __construct(array $options = [])
{
$options += [
'async' => false,
'client' => null
];
$this->async = $options['async'];
$this->setCommonBatchProperties($options + [
'identifier' => 'stackdriver-trace',
'batchMethod' => 'insertBatch'
Expand Down Expand Up @@ -160,11 +132,7 @@ public function export(array $spans)
$trace->setSpans($spans);

try {
if ($this->async) {
return $this->batchRunner->submitItem($this->identifier, $trace);
} else {
return self::$client->insert($trace);
}
return $this->batchRunner->submitItem($this->identifier, $trace);
} catch (\Exception $e) {
error_log('Reporting the Trace data failed: ' . $e->getMessage());
return false;
Expand Down
30 changes: 21 additions & 9 deletions tests/unit/StackdriverExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OpenCensus\Trace\Tracer\ContextTracer;
use OpenCensus\Trace\Span as OCSpan;
use Prophecy\Argument;
use Google\Cloud\Core\Batch\BatchRunner;
use Google\Cloud\Trace\Trace;
use Google\Cloud\Trace\Span;
use Google\Cloud\Trace\TraceClient;
Expand Down Expand Up @@ -63,15 +64,20 @@ public function setUp()

public function testReportWithAnExceptionErrorLog()
{
$this->client->insert(Argument::any())->willThrow(
new \Exception('error_log test')
);
$trace = $this->prophesize(Trace::class);
$trace->setSpans(Argument::any())->shouldBeCalled();
$this->client->trace(Argument::any())->willReturn($trace->reveal());
$exporter = new StackdriverExporter(
['client' => $this->client->reveal()]
);

$batchRunner = $this->prophesize(BatchRunner::class);
$batchRunner->registerJob(Argument::any(), Argument::any(), Argument::any())->shouldBeCalled();
$batchRunner->submitItem(Argument::any(), Argument::any())->willThrow(
new \Exception('error_log test')
)->shouldBeCalled();

$exporter = new StackdriverExporter([
'client' => $this->client->reveal(),
'batchRunner' => $batchRunner->reveal()
]);
$this->expectOutputString(
'Reporting the Trace data failed: error_log test'
);
Expand All @@ -95,16 +101,22 @@ public function testReportsVersionAttribute()
return true;
}))->shouldBeCalled();
$this->client->trace('aaa')->willReturn($trace->reveal());
$this->client->insert(Argument::type(Trace::class))
->willReturn(true)->shouldBeCalled();

$span = new OCSpan([
'traceId' => 'aaa'
]);
$span->setStartTime();
$span->setEndTime();

$exporter = new StackdriverExporter(['client' => $this->client->reveal()]);
$batchRunner = $this->prophesize(BatchRunner::class);
$batchRunner->registerJob(Argument::any(), Argument::any(), Argument::any())->shouldBeCalled();
$batchRunner->submitItem(Argument::any(), Argument::any())
->willReturn(true)
->shouldBeCalled();
$exporter = new StackdriverExporter([
'client' => $this->client->reveal(),
'batchRunner' => $batchRunner->reveal()
]);
$this->assertTrue($exporter->export([$span->spanData()]));
}
}

0 comments on commit f3017ce

Please sign in to comment.