-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add InMemoryExporter
- Loading branch information
Showing
5 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
require __DIR__ . '/../../../../vendor/autoload.php'; | ||
|
||
use OpenTelemetry\SDK\Trace\SpanDataInterface; | ||
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; | ||
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; | ||
use OpenTelemetry\SDK\Trace\TracerProvider; | ||
|
||
// Create an ArrayObject as the storage for the spans | ||
$storage = new ArrayObject(); | ||
|
||
// Boilerplate setup to create a new tracer with an in-memory exporter | ||
$tracer = (new TracerProvider( | ||
new SimpleSpanProcessor( | ||
new InMemoryExporter($storage) | ||
) | ||
))->getTracer('io.opentelemetry.contrib.php'); | ||
|
||
// This creates a span and sets it as the current parent (and root) span | ||
$rootSpan = $tracer->spanBuilder('foo')->startSpan(); | ||
$rootScope = $rootSpan->activate(); | ||
|
||
// This creates child spans | ||
$childSpan1 = $tracer->spanBuilder('bar')->startSpan(); | ||
$childSpan2 = $tracer->spanBuilder('bar')->startSpan(); | ||
|
||
// This closes all spans | ||
$childSpan2->end(); | ||
$childSpan1->end(); | ||
$rootSpan->end(); | ||
|
||
/** @var SpanDataInterface $span */ | ||
foreach ($storage as $span) { | ||
echo PHP_EOL . sprintf( | ||
'TRACE: "%s", SPAN: "%s", PARENT: "%s"', | ||
$span->getTraceId(), | ||
$span->getSpanId(), | ||
$span->getParentSpanId() | ||
); | ||
} | ||
echo PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Trace\SpanExporter; | ||
|
||
use ArrayObject; | ||
use OpenTelemetry\SDK\Trace\Behavior\SpanExporterTrait; | ||
use OpenTelemetry\SDK\Trace\SpanExporterInterface; | ||
|
||
class InMemoryExporter implements SpanExporterInterface | ||
{ | ||
use SpanExporterTrait; | ||
|
||
private ArrayObject $storage; | ||
|
||
public function __construct(?ArrayObject $storage = null) | ||
{ | ||
$this->storage = $storage ?? new ArrayObject(); | ||
} | ||
|
||
public static function fromConnectionString(string $endpointUrl = null, string $name = null, $args = null) | ||
{ | ||
return new self(); | ||
} | ||
|
||
protected function doExport(iterable $spans): int | ||
{ | ||
foreach ($spans as $span) { | ||
$this->storage[] = $span; | ||
} | ||
|
||
return SpanExporterInterface::STATUS_SUCCESS; | ||
} | ||
|
||
public function getSpans(): array | ||
{ | ||
return (array) $this->storage; | ||
} | ||
|
||
public function getStorage(): ArrayObject | ||
{ | ||
return $this->storage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tests/Unit/SDK/Trace/SpanExporter/InMemoryExporterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Tests\Unit\SDK\Trace\SpanExporter; | ||
|
||
use ArrayObject; | ||
use Generator; | ||
use OpenTelemetry\SDK\Trace\SpanDataInterface; | ||
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter | ||
*/ | ||
class InMemoryExporterTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideSpans | ||
*/ | ||
public function test_export(iterable $spans): void | ||
{ | ||
$instance = new InMemoryExporter(); | ||
|
||
$instance->export($spans); | ||
|
||
$this->assertSame( | ||
$spans, | ||
$instance->getSpans() | ||
); | ||
} | ||
|
||
public function test_from_connection_string(): void | ||
{ | ||
$this->assertInstanceOf( | ||
InMemoryExporter::class, | ||
InMemoryExporter::fromConnectionString() | ||
); | ||
} | ||
|
||
public function test_get_storage(): void | ||
{ | ||
$storage = new ArrayObject(); | ||
|
||
$this->assertSame( | ||
$storage, | ||
(new InMemoryExporter($storage))->getStorage() | ||
); | ||
} | ||
|
||
public function test_get_spans(): void | ||
{ | ||
$storage = new ArrayObject(); | ||
|
||
$this->assertSame( | ||
(array) $storage, | ||
(new InMemoryExporter($storage))->getSpans() | ||
); | ||
} | ||
|
||
public function provideSpans(): Generator | ||
{ | ||
$spans = []; | ||
|
||
for ($x = 0; $x < 3; $x++) { | ||
$spans[] = $this->createMock(SpanDataInterface::class); | ||
|
||
yield $x => [$spans]; | ||
} | ||
} | ||
} |