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

Fix ExporterInterface #150

Merged
merged 6 commits into from
Mar 28, 2018
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"cache/adapter-common": "^1.0"
},
"require-dev": {
"phpunit/phpunit": ">= 4.8",
"phpunit/phpunit": ">= 6.0",
"squizlabs/php_codesniffer": "2.*",
"google/cloud-trace": "^0.4",
"twig/twig": "~2.0",
Expand Down
8 changes: 4 additions & 4 deletions src/Trace/Exporter/EchoExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace OpenCensus\Trace\Exporter;

use OpenCensus\Trace\Tracer\TracerInterface;
use OpenCensus\Trace\SpanData;

/**
* This implementation of the ExporterInterface uses `print_r` to output
Expand All @@ -37,12 +37,12 @@ class EchoExporter implements ExporterInterface
/**
* Report the provided Trace to a backend.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return bool
*/
public function report(TracerInterface $tracer)
public function export(array $spans)
{
print_r($tracer->spans());
print_r($spans);
return true;
}
}
8 changes: 4 additions & 4 deletions src/Trace/Exporter/ExporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

namespace OpenCensus\Trace\Exporter;

use OpenCensus\Trace\Tracer\TracerInterface;
use OpenCensus\Trace\SpanData;

/**
* The ExporterInterface allows you to swap out the Trace reporting mechanism
*/
interface ExporterInterface
{
/**
* Report the provided Trace to a backend.
* Export the provided SpanData to a backend.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return bool
*/
public function report(TracerInterface $tracer);
public function export(array $spans);
}
22 changes: 10 additions & 12 deletions src/Trace/Exporter/FileExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace OpenCensus\Trace\Exporter;

use OpenCensus\Trace\Span;
use OpenCensus\Trace\SpanData;
use OpenCensus\Trace\Tracer\TracerInterface;

/**
Expand Down Expand Up @@ -51,30 +51,28 @@ public function __construct($filename)
}

/**
* Report the provided Trace to a backend.
* Report the provided SpanData to a file in json format.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return bool
*/
public function report(TracerInterface $tracer)
public function export(array $spans)
{
$spans = $this->convertSpans($tracer);
$spans = $this->convertSpans($spans);
return file_put_contents($this->filename, json_encode($spans) . PHP_EOL, FILE_APPEND) !== false;
}

/**
* Convert spans into array ready for serialization.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return array Representation of the collected trace spans ready for serialization
*/
private function convertSpans(TracerInterface $tracer)
private function convertSpans(array $spans)
{
$traceId = $tracer->spanContext()->traceId();

return array_map(function (Span $span) use ($traceId) {
return array_map(function (SpanData $span) use ($traceId) {
return [
'traceId' => $traceId,
'traceId' => $span->traceId(),
'name' => $span->name(),
'spanId' => $span->spanId(),
'parentSpanId' => $span->parentSpanId(),
Expand All @@ -87,6 +85,6 @@ private function convertSpans(TracerInterface $tracer)
'links' => $span->links(),
'sameProcessAsParentSpan' => $span->sameProcessAsParentSpan(),
];
}, $tracer->spans());
}, $spans);
}
}
8 changes: 4 additions & 4 deletions src/Trace/Exporter/LoggerExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace OpenCensus\Trace\Exporter;

use OpenCensus\Trace\Tracer\TracerInterface;
use OpenCensus\Trace\SpanData;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -65,13 +65,13 @@ public function __construct(LoggerInterface $logger, $level = self::DEFAULT_LOG_
/**
* Report the provided Trace to a backend.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return bool
*/
public function report(TracerInterface $tracer)
public function export(array $spans)
{
try {
$this->logger->log($this->level, json_encode($tracer->spans()));
$this->logger->log($this->level, json_encode($spans));
} catch (\Exception $e) {
error_log('Reporting the Trace data failed: ' . $e->getMessage());
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Trace/Exporter/NullExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class NullExporter implements ExporterInterface
/**
* Does nothing.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return bool
*/
public function report(TracerInterface $tracer)
public function export(array $spans)
{
return true;
}
Expand Down
19 changes: 10 additions & 9 deletions src/Trace/Exporter/StackdriverExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
use Google\Cloud\Trace\TraceClient;
use Google\Cloud\Trace\Span;
use Google\Cloud\Trace\Trace;
use OpenCensus\Trace\Tracer\TracerInterface;
use OpenCensus\Trace\Span as OCSpan;
use OpenCensus\Trace\SpanData;

/**
* This implementation of the ExporterInterface use the BatchRunner to provide
Expand Down Expand Up @@ -135,19 +135,20 @@ public function __construct(array $options = [])
/**
* Report the provided Trace to a backend.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return bool
*/
public function report(TracerInterface $tracer)
public function export(array $spans)
{
$spans = $this->convertSpans($tracer);
$spans = $this->convertSpans($spans);

if (empty($spans)) {
return false;
}

// Pull the traceId from the first span
$trace = self::$client->trace(
$tracer->spanContext()->traceId()
$spans[0]->traceId()
);

// build a Trace object and assign Spans
Expand All @@ -170,16 +171,16 @@ public function report(TracerInterface $tracer)
*
* @access private
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return Span[] Representation of the collected trace spans ready for serialization
*/
public function convertSpans(TracerInterface $tracer)
public function convertSpans(array $spans)
{
// transform OpenCensus Spans to Google\Cloud\Trace\Spans
return array_map([$this, 'mapSpan'], $tracer->spans());
return array_map([$this, 'mapSpan'], $spans);
}

private function mapSpan($span)
private function mapSpan(SpanData $span)
{
return new Span($span->traceId(), [
'name' => $span->name(),
Expand Down
18 changes: 8 additions & 10 deletions src/Trace/Exporter/ZipkinExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
namespace OpenCensus\Trace\Exporter;

use OpenCensus\Trace\MessageEvent;
use OpenCensus\Trace\Tracer\TracerInterface;
use OpenCensus\Trace\Span;
use OpenCensus\Trace\SpanData;

/**
* This implementation of the ExporterInterface appends a json
Expand Down Expand Up @@ -96,12 +95,12 @@ public function setLocalIpv6($ipv6)
/**
* Report the provided Trace to a backend.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @return bool
*/
public function report(TracerInterface $tracer)
public function export(array $spans)
{
$spans = $this->convertSpans($tracer);
$spans = $this->convertSpans($spans);

if (empty($spans)) {
return false;
Expand Down Expand Up @@ -129,15 +128,13 @@ public function report(TracerInterface $tracer)
* Convert spans into Zipkin's expected JSON output format. See
* <a href="http://zipkin.io/zipkin-api/#/default/post_spans">output format definition</a>.
*
* @param TracerInterface $tracer
* @param SpanData[] $spans
* @param array $headers [optional] HTTP headers to parse. **Defaults to** $_SERVER
* @return array Representation of the collected trace spans ready for serialization
*/
public function convertSpans(TracerInterface $tracer, $headers = null)
public function convertSpans(array $spans, $headers = null)
{
$headers = $headers ?: $_SERVER;
$spans = $tracer->spans();
$traceId = $tracer->spanContext()->traceId();

// True is a request to store this span even if it overrides sampling policy.
// This is true when the X-B3-Flags header has a value of 1.
Expand All @@ -154,6 +151,7 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
$parentSpanId = $span->parentSpanId()
? str_pad($span->parentSpanId(), 16, '0', STR_PAD_LEFT)
: null;
$traceId = str_pad($span->traceId(), 32, '0', STR_PAD_LEFT);

$attributes = $span->attributes();
if (empty($attributes)) {
Expand Down Expand Up @@ -184,7 +182,7 @@ public function convertSpans(TracerInterface $tracer, $headers = null)
return $zipkinSpans;
}

private function spanKind(Span $span)
private function spanKind(SpanData $span)
{
if (strpos($span->name(), 'Sent.') === 0) {
return self::KIND_CLIENT;
Expand Down
15 changes: 10 additions & 5 deletions src/Trace/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RequestHandler
/**
* @var ExporterInterface The reported to use at the end of the request
*/
private $reporter;
private $exporter;

/**
* @var TracerInterface The tracer to use for this request
Expand All @@ -72,7 +72,7 @@ class RequestHandler
/**
* Create a new RequestHandler.
*
* @param ExporterInterface $reporter How to report the trace at the end of the request
* @param ExporterInterface $exporter How to report the trace at the end of the request
* @param SamplerInterface $sampler Which sampler to use for sampling requests
* @param PropagatorInterface $propagator SpanContext propagator
* @param array $options [optional] {
Expand All @@ -85,12 +85,12 @@ class RequestHandler
* }
*/
public function __construct(
ExporterInterface $reporter,
ExporterInterface $exporter,
SamplerInterface $sampler,
PropagatorInterface $propagator,
array $options = []
) {
$this->reporter = $reporter;
$this->exporter = $exporter;
$this->headers = array_key_exists('headers', $options)
? $options['headers']
: $_SERVER;
Expand Down Expand Up @@ -135,7 +135,12 @@ public function onExit()
$this->addCommonRequestAttributes($this->headers);

$this->scope->close();
$this->reporter->report($this->tracer);

// Fetch the SpanData of all the collected Spans
$spans = array_map(function (Span $span) {
return $span->spanData();
}, $this->tracer->spans());
$this->exporter->export($spans);
}

/**
Expand Down
Loading