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

Commit de03d59

Browse files
castaneaichingor13
authored andcommitted
Add OneLineEchoExporter (#177)
* Add OneLineEchoExporter * Fix based on review
1 parent b20205e commit de03d59

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ The provided exporters are:
107107
| [JaegerExporter][jaeger-exporter] | Report traces to Jaeger server via Thrift over UDP | [opencensus/opencensus-exporter-jaeger][jaeger-packagist] |
108108
| [LoggerExporter][logger-exporter] | Exporter JSON encoded spans to a PSR-3 logger | |
109109
| [NullExporter][null-exporter] | No-op | |
110+
| [OneLineEchoExporter][one-line-echo-exporter] | Output the collected spans to stdout with one-line | |
110111
| [StackdriverExporter][stackdriver-exporter] | Report traces to Google Cloud Stackdriver Trace | |
111112
| [ZipkinExporter][zipkin-exporter] | Report collected spans to a Zipkin server | |
112113
@@ -165,6 +166,7 @@ This is not an official Google product.
165166
[qps-sampler]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Sampler/NeverSampleSampler.html
166167
[probability-sampler]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Sampler/NeverSampleSampler.html
167168
[echo-exporter]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Exporter/EchoExporter.html
169+
[one-line-echo-exporter]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Exporter/OneLineEchoExporter.html
168170
[file-exporter]: https://opencensus.io/api/php/api/master/OpenCensus/Trace/Exporter/FileExporter.html
169171
[jaeger-exporter]: https://github.com/census-instrumentation/opencensus-php-exporter-jaeger/blob/master/src/JaegerExporter.php
170172
[jaeger-packagist]: https://packagist.org/packages/opencensus/opencensus-exporter-jaeger
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensus Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCensus\Trace\Exporter;
19+
20+
use OpenCensus\Trace\SpanData;
21+
22+
class OneLineEchoExporter implements ExporterInterface
23+
{
24+
25+
/**
26+
* Report the provided Trace to a backend.
27+
*
28+
* @param SpanData[] $spans
29+
* @return bool
30+
*/
31+
public function export(array $spans)
32+
{
33+
foreach ($spans as $span) {
34+
$time = (float) ($span->endTime()->format('U.u')) - (float) ($span->startTime()->format('U.u'));
35+
printf("[ %8.2f ms] %s%s", $time * 1000, $span->name(), PHP_EOL);
36+
foreach ($span->attributes() as $key => $value) {
37+
printf(" [%s] %s%s", $key, $value, PHP_EOL);
38+
}
39+
}
40+
return true;
41+
}
42+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright 2018 OpenCensus Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace OpenCensus\Tests\Unit\Trace\Exporter;
19+
20+
use OpenCensus\Trace\Exporter\OneLineEchoExporter;
21+
use OpenCensus\Trace\Span;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* @group trace
26+
*/
27+
class OneLineEchoExporterTest extends TestCase
28+
{
29+
public function testLogsTrace()
30+
{
31+
$startTime = microtime(true);
32+
$span = new Span([
33+
'name' => 'span',
34+
'startTime' => $startTime,
35+
'endTime' => $startTime + 10
36+
]);
37+
38+
ob_start();
39+
$exporter = new OneLineEchoExporter();
40+
$this->assertTrue($exporter->export([$span->spanData()]));
41+
$output = ob_get_contents();
42+
ob_end_clean();
43+
$this->assertEquals('[ 10000.00 ms] span' . PHP_EOL, $output);
44+
}
45+
}

0 commit comments

Comments
 (0)