Skip to content

Commit cf6deaf

Browse files
Laravel: tests bootstrap via SPI.
1 parent d2887bc commit cf6deaf

File tree

7 files changed

+125
-42
lines changed

7 files changed

+125
-42
lines changed

src/Instrumentation/Laravel/phpunit.xml.dist

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
66
backupGlobals="false"
77
backupStaticAttributes="false"
8+
bootstrap="tests/bootstrap.php"
89
cacheResult="false"
910
colors="false"
1011
convertErrorsToExceptions="true"
@@ -41,7 +42,10 @@
4142
<env name="MAIL_MAILER" value="array"/>
4243
<env name="QUEUE_CONNECTION" value="sync"/>
4344
<env name="SESSION_DRIVER" value="array"/>
44-
<env name="TELESCOPE_ENABLED" value="false"/>
45+
<env name="TELESCOPE_ENABLED" value="false"/>
46+
47+
<server name="OTEL_PHP_AUTOLOAD_ENABLED" value="true"/>
48+
<server name="OTEL_EXPERIMENTAL_CONFIG_FILE" value="tests/Fixtures/otel-instrumentation.yaml"/>
4549
</php>
4650

4751
<testsuites>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\ComponentProvider;
6+
7+
use OpenTelemetry\Config\SDK\Configuration\ComponentProvider;
8+
use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry;
9+
use OpenTelemetry\Config\SDK\Configuration\Context;
10+
use OpenTelemetry\SDK\Logs\Exporter\InMemoryExporter;
11+
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
12+
use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\TestStorage;
13+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14+
15+
final class LogRecordExporterInMemory implements ComponentProvider
16+
{
17+
public function createPlugin(array $properties, Context $context): LogRecordExporterInterface
18+
{
19+
return new InMemoryExporter(TestStorage::getInstance());
20+
}
21+
22+
public function getConfig(ComponentProviderRegistry $registry): ArrayNodeDefinition
23+
{
24+
return new ArrayNodeDefinition('test/in_memory_exporter');
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\ComponentProvider;
6+
7+
use OpenTelemetry\Config\SDK\Configuration\ComponentProvider;
8+
use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry;
9+
use OpenTelemetry\Config\SDK\Configuration\Context;
10+
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
11+
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
12+
use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\TestStorage;
13+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14+
15+
final class SpanExporterInMemory implements ComponentProvider
16+
{
17+
public function createPlugin(array $properties, Context $context): SpanExporterInterface
18+
{
19+
return new InMemoryExporter(TestStorage::getInstance());
20+
}
21+
22+
public function getConfig(ComponentProviderRegistry $registry): ArrayNodeDefinition
23+
{
24+
return new ArrayNodeDefinition('test/in_memory_exporter');
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures;
6+
7+
use ArrayObject;
8+
9+
class TestStorage extends ArrayObject
10+
{
11+
private static ?self $instance = null;
12+
13+
public static function getInstance(): static
14+
{
15+
return self::$instance ??= new self();
16+
}
17+
18+
public static function reset(): void
19+
{
20+
self::$instance->exchangeArray([]);
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
file_format: '0.3'
2+
3+
logger_provider:
4+
processors:
5+
- simple:
6+
exporter:
7+
test/in_memory_exporter: {}
8+
9+
tracer_provider:
10+
processors:
11+
- simple:
12+
exporter:
13+
test/in_memory_exporter: {}
14+
15+
instrumentation:
16+
php:
17+
laravel:
18+
enabled: true

src/Instrumentation/Laravel/tests/Integration/TestCase.php

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,24 @@
55
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration;
66

77
use ArrayObject;
8-
use OpenTelemetry\API\Instrumentation\Configurator;
9-
use OpenTelemetry\Context\ScopeInterface;
10-
use OpenTelemetry\SDK\Common\Attribute\Attributes;
11-
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeFactory;
12-
use OpenTelemetry\SDK\Logs\Exporter\InMemoryExporter as LogInMemoryExporter;
13-
use OpenTelemetry\SDK\Logs\LoggerProvider;
14-
use OpenTelemetry\SDK\Logs\Processor\SimpleLogRecordProcessor;
15-
use OpenTelemetry\SDK\Trace\ImmutableSpan;
16-
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter as SpanInMemoryExporter;
17-
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
18-
use OpenTelemetry\SDK\Trace\TracerProvider;
8+
use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\TestStorage;
199
use Orchestra\Testbench\TestCase as BaseTestCase;
10+
use PHPUnit\Framework\Attributes\After;
11+
use PHPUnit\Framework\Attributes\Before;
2012

2113
abstract class TestCase extends BaseTestCase
2214
{
23-
protected ScopeInterface $scope;
24-
/** @var ArrayObject|ImmutableSpan[] $storage */
2515
protected ArrayObject $storage;
26-
protected ArrayObject $loggerStorage;
27-
protected TracerProvider $tracerProvider;
28-
protected LoggerProvider $loggerProvider;
2916

30-
public function setUp(): void
17+
#[Before]
18+
public function setUpTestStorage(): void
3119
{
32-
parent::setUp();
33-
34-
$this->storage = new ArrayObject();
35-
$this->tracerProvider = new TracerProvider(
36-
new SimpleSpanProcessor(
37-
new SpanInMemoryExporter($this->storage),
38-
),
39-
);
40-
41-
$this->loggerProvider = new LoggerProvider(
42-
new SimpleLogRecordProcessor(
43-
new LogInMemoryExporter($this->storage),
44-
),
45-
new InstrumentationScopeFactory(Attributes::factory())
46-
);
47-
48-
$this->scope = Configurator::create()
49-
->withTracerProvider($this->tracerProvider)
50-
->withLoggerProvider($this->loggerProvider)
51-
->activate();
20+
$this->storage = TestStorage::getInstance();
5221
}
5322

54-
public function tearDown(): void
23+
#[After]
24+
public function tearDownTestStorage(): void
5525
{
56-
parent::tearDown();
57-
58-
$this->scope->detach();
26+
TestStorage::reset();
5927
}
6028
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* This file is loaded by PHPUnit (per phpunit bootstrap configuration).
5+
* Necessary OTEL_* env vars have been set in phpunit.xml.dist at this point,
6+
* so we can re-trigger autoloading the SDK.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
use Nevay\SPI\ServiceLoader;
12+
use OpenTelemetry\Config\SDK\Configuration\ComponentProvider;
13+
use OpenTelemetry\SDK\SdkAutoloader;
14+
15+
ServiceLoader::register(ComponentProvider::class, \OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\ComponentProvider\LogRecordExporterInMemory::class);
16+
ServiceLoader::register(ComponentProvider::class, \OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\ComponentProvider\SpanExporterInMemory::class);
17+
18+
// Finally, ensure that the SDK loads with the updated env vars and SPI test components.
19+
SdkAutoloader::autoload();

0 commit comments

Comments
 (0)