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

Commit

Permalink
Add OneLineEchoExporter (#177)
Browse files Browse the repository at this point in the history
* Add OneLineEchoExporter

* Fix based on review
  • Loading branch information
castaneai authored and chingor13 committed Apr 23, 2018
1 parent b20205e commit de03d59
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ The provided exporters are:
| [JaegerExporter][jaeger-exporter] | Report traces to Jaeger server via Thrift over UDP | [opencensus/opencensus-exporter-jaeger][jaeger-packagist] |
| [LoggerExporter][logger-exporter] | Exporter JSON encoded spans to a PSR-3 logger | |
| [NullExporter][null-exporter] | No-op | |
| [OneLineEchoExporter][one-line-echo-exporter] | Output the collected spans to stdout with one-line | |
| [StackdriverExporter][stackdriver-exporter] | Report traces to Google Cloud Stackdriver Trace | |
| [ZipkinExporter][zipkin-exporter] | Report collected spans to a Zipkin server | |
Expand Down Expand Up @@ -165,6 +166,7 @@ This is not an official Google product.
[qps-sampler]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Sampler/NeverSampleSampler.html
[probability-sampler]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Sampler/NeverSampleSampler.html
[echo-exporter]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Exporter/EchoExporter.html
[one-line-echo-exporter]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Exporter/OneLineEchoExporter.html
[file-exporter]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Exporter/FileExporter.html
[jaeger-exporter]: https://github.com/census-instrumentation/opencensus-php-exporter-jaeger/blob/master/src/JaegerExporter.php
[jaeger-packagist]: https://packagist.org/packages/opencensus/opencensus-exporter-jaeger
Expand Down
42 changes: 42 additions & 0 deletions src/Trace/Exporter/OneLineEchoExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright 2018 OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenCensus\Trace\Exporter;

use OpenCensus\Trace\SpanData;

class OneLineEchoExporter implements ExporterInterface
{

/**
* Report the provided Trace to a backend.
*
* @param SpanData[] $spans
* @return bool
*/
public function export(array $spans)
{
foreach ($spans as $span) {
$time = (float) ($span->endTime()->format('U.u')) - (float) ($span->startTime()->format('U.u'));
printf("[ %8.2f ms] %s%s", $time * 1000, $span->name(), PHP_EOL);
foreach ($span->attributes() as $key => $value) {
printf(" [%s] %s%s", $key, $value, PHP_EOL);
}
}
return true;
}
}
45 changes: 45 additions & 0 deletions tests/unit/Trace/Exporter/OneLineEchoExporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright 2018 OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenCensus\Tests\Unit\Trace\Exporter;

use OpenCensus\Trace\Exporter\OneLineEchoExporter;
use OpenCensus\Trace\Span;
use PHPUnit\Framework\TestCase;

/**
* @group trace
*/
class OneLineEchoExporterTest extends TestCase
{
public function testLogsTrace()
{
$startTime = microtime(true);
$span = new Span([
'name' => 'span',
'startTime' => $startTime,
'endTime' => $startTime + 10
]);

ob_start();
$exporter = new OneLineEchoExporter();
$this->assertTrue($exporter->export([$span->spanData()]));
$output = ob_get_contents();
ob_end_clean();
$this->assertEquals('[ 10000.00 ms] span' . PHP_EOL, $output);
}
}

0 comments on commit de03d59

Please sign in to comment.