Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async trace reporter readme #566

Merged
merged 3 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Trace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ $ composer require google/cloud-trace

```php
use Google\Cloud\Trace\TraceClient;
use Google\Cloud\Trace\Reporter\SyncReporter;
use Google\Cloud\Trace\RequestTracer;

$trace = new TraceClient();
$reporter = new SyncReporter($trace);
$reporter = $trace->reporter();
RequestTracer::start($reporter);
```

## Customizing

### Reporting Traces

The above sample uses the `SyncReporter` to sychronously report trace results to the
Stackdriver servers at the end of the request.
The above sample uses the `AsyncReporter` to report trace results to the Stackdriver servers.
If you are using the experimental
[google-cloud-batch daemon](https://github.com/GoogleCloudPlatform/google-cloud-php-core/blob/master/Batch/BatchDaemon.php)
and have set the `IS_BATCH_DAEMON_RUNNING=true` environment variable, then the reporting of the trace will happen
asynchronously. If not, the reporting will happen at the end of the request and can add some latency to your request.

For testing/development, we also provide an `EchoReporter`, `FileReporter` and `LoggerReporter`.

Expand Down
22 changes: 22 additions & 0 deletions src/Trace/TraceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Google\Cloud\Core\Iterator\PageIterator;
use Google\Cloud\Trace\Connection\ConnectionInterface;
use Google\Cloud\Trace\Connection\Rest;
use Google\Cloud\Trace\Reporter\AsyncReporter;
use Google\Cloud\Trace\Reporter\ReporterInterface;

/**
* Google Stackdriver Trace allows you to collect latency data from
Expand Down Expand Up @@ -51,6 +53,11 @@ class TraceClient
*/
protected $connection;

/**
* @var array
*/
private $clientConfig;

/**
* Create a Trace client.
*
Expand Down Expand Up @@ -79,6 +86,7 @@ class TraceClient
*/
public function __construct(array $config = [])
{
$this->clientConfig = $config;
if (!isset($config['scopes'])) {
$config['scopes'] = [self::FULL_CONTROL_SCOPE];
}
Expand Down Expand Up @@ -187,4 +195,18 @@ function (array $trace) {
)
);
}

/**
* Return a Trace reporter that utilizes this client's configuration
*
* @param array $options [optional] Reporter options.
* {@see Google\Cloud\Trace\Reporter\AsyncReporter::__construct()}
* @return ReporterInterface
*/
public function reporter(array $options = [])
{
return new AsyncReporter($options + [
'clientConfig' => $this->clientConfig
]);
}
}