-
Notifications
You must be signed in to change notification settings - Fork 117
Improve openentelemetry-auto-laravel test definitions for Eloquent hooks. #394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
brettmc
merged 10 commits into
open-telemetry:main
from
rozeo:feature/improve-laravel-instrument-test-definitions
Jun 19, 2025
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6a917b8
create test boilerplate
rozeo 1e6f064
add testcases
rozeo 3e91955
remove replaced integration test cases
rozeo 639f767
format and replace class path
rozeo ac4fb9e
update namespace
rozeo cab14e9
assert only scoped spans
rozeo 3864d26
fix test errors
rozeo 66a2fbb
add psalm-suppress annotation
rozeo 38d2a8d
remove table dropping with tearDown (already initialized by in-memory…
rozeo 2aaa162
add test case for delete hook
rozeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
src/Instrumentation/Laravel/tests/Integration/Database/Eloquent/ModelTest.php
This file contains hidden or 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,189 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration\Database\Eloquent; | ||
|
|
||
| use Illuminate\Support\Facades\DB; | ||
| use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel; | ||
| use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration\TestCase; | ||
|
|
||
| /** | ||
| * Integration test for Eloquent\Model hooks | ||
| * @psalm-suppress UnusedClass | ||
| */ | ||
| class ModelTest extends TestCase | ||
| { | ||
| protected function getEnvironmentSetUp($app): void | ||
| { | ||
| // Setup default database to use sqlite :memory: | ||
| $app['config']->set('database.default', 'testbench'); | ||
| $app['config']->set('database.connections.testbench', [ | ||
| 'driver' => 'sqlite', | ||
| 'database' => ':memory:', | ||
| 'prefix' => '', | ||
| ]); | ||
| } | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| // Setup database table for fixture model | ||
| DB::statement('CREATE TABLE IF NOT EXISTS test_models( | ||
| id BIGINT, | ||
| name VARCHAR(255), | ||
| created_at DATETIME, | ||
| updated_at DATETIME | ||
| ) | ||
| '); | ||
| } | ||
|
|
||
| public function tearDown(): void | ||
| { | ||
| // Reset table | ||
| DB::statement('DROP TABLE IF EXISTS test_models'); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| /** | ||
| * @return \OpenTelemetry\SDK\Trace\ImmutableSpan[] | ||
| */ | ||
| private function filterOnlyEloquentSpans(): array | ||
| { | ||
| // SQL spans be mixed up in storage because \Illuminate\Support\Facades\DB called. | ||
| // So, filtering only spans has attribute named 'laravel.eloquent.operation'. | ||
| return array_values( | ||
| array_filter( | ||
| iterator_to_array($this->storage), | ||
| fn ($span) => | ||
| $span instanceof \OpenTelemetry\SDK\Trace\ImmutableSpan && | ||
| $span->getAttributes()->has('laravel.eloquent.operation') | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public function test_create(): void | ||
| { | ||
| TestModel::create(['id' => 1, 'name' => 'test']); | ||
|
|
||
| $spans = $this->filterOnlyEloquentSpans(); | ||
|
|
||
| $this->assertCount(1, $spans); | ||
|
|
||
| $span = $spans[0]; | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::create', $span->getName()); | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $span->getAttributes()->get('laravel.eloquent.model')); | ||
| $this->assertSame('test_models', $span->getAttributes()->get('laravel.eloquent.table')); | ||
| $this->assertSame('create', $span->getAttributes()->get('laravel.eloquent.operation')); | ||
| } | ||
|
|
||
| public function test_find(): void | ||
| { | ||
| TestModel::find(1); | ||
|
|
||
| // spans contains 2 eloquent spans for 'find' and 'get', because it method internally calls 'getModels' method. | ||
| // So, filtering span only find span. | ||
| $spans = array_values( | ||
| array_filter( | ||
| $this->filterOnlyEloquentSpans(), | ||
| fn ($span) => $span->getAttributes()->get('laravel.eloquent.operation') === 'find' | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertCount(1, $spans); | ||
|
|
||
| $span = $spans[0]; | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::find', $span->getName()); | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $span->getAttributes()->get('laravel.eloquent.model')); | ||
| $this->assertSame('test_models', $span->getAttributes()->get('laravel.eloquent.table')); | ||
| $this->assertSame('find', $span->getAttributes()->get('laravel.eloquent.operation')); | ||
|
|
||
| } | ||
|
|
||
| public function test_perform_insert(): void | ||
| { | ||
| // Illuminate\Database\Eloquent\Model::performInsert is called from Illuminate\Database\Eloquent\Model::save. | ||
| // Mark as exists = false required, because performUpdate called if exists = true. | ||
| $model = (new TestModel())->newInstance(['id' => 1, 'name' => 'test'], false); | ||
| $model->save(); | ||
|
|
||
| $spans = $this->filterOnlyEloquentSpans(); | ||
| $this->assertCount(1, $spans); | ||
|
|
||
| $span = $spans[0]; | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::create', $span->getName()); | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $span->getAttributes()->get('laravel.eloquent.model')); | ||
| $this->assertSame('test_models', $span->getAttributes()->get('laravel.eloquent.table')); | ||
| $this->assertSame('create', $span->getAttributes()->get('laravel.eloquent.operation')); | ||
| } | ||
|
|
||
| public function test_perform_update(): void | ||
| { | ||
| // Illuminate\Database\Eloquent\Model::performInsert is called from Illuminate\Database\Eloquent\Model::save. | ||
| // Mark as exists = true required, because performInsert called if exists = false. | ||
| $model = (new TestModel())->newInstance(['id' => 1, 'name' => 'test'], true); | ||
| $model->save(); | ||
|
|
||
| $spans = $this->filterOnlyEloquentSpans(); | ||
| $this->assertCount(1, $spans); | ||
|
|
||
| $span = $spans[0]; | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::update', $span->getName()); | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $span->getAttributes()->get('laravel.eloquent.model')); | ||
| $this->assertSame('test_models', $span->getAttributes()->get('laravel.eloquent.table')); | ||
| $this->assertSame('update', $span->getAttributes()->get('laravel.eloquent.operation')); | ||
| } | ||
|
|
||
| public function test_get_models(): void | ||
| { | ||
| TestModel::get(); | ||
|
|
||
| $spans = $this->filterOnlyEloquentSpans(); | ||
| $this->assertCount(1, $spans); | ||
|
|
||
| $span = $spans[0]; | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::get', $span->getName()); | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $span->getAttributes()->get('laravel.eloquent.model')); | ||
| $this->assertSame('test_models', $span->getAttributes()->get('laravel.eloquent.table')); | ||
| $this->assertSame('get', $span->getAttributes()->get('laravel.eloquent.operation')); | ||
| } | ||
|
|
||
| public function test_destory(): void | ||
| { | ||
| TestModel::destroy([1]); | ||
|
|
||
| // spans contains 2 eloquent spans for 'destroy' and 'get', because it method internally calls 'getModels' method. | ||
| // So, filtering span only 'destroy' span. | ||
| $spans = array_values( | ||
| array_filter( | ||
| $this->filterOnlyEloquentSpans(), | ||
| fn ($span) => $span->getAttributes()->get('laravel.eloquent.operation') === 'destroy' | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertCount(1, $spans); | ||
|
|
||
| $span = $spans[0]; | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::destroy', $span->getName()); | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $span->getAttributes()->get('laravel.eloquent.model')); | ||
| $this->assertSame('test_models', $span->getAttributes()->get('laravel.eloquent.table')); | ||
| $this->assertSame('destroy', $span->getAttributes()->get('laravel.eloquent.operation')); | ||
| } | ||
|
|
||
| public function test_refresh(): void | ||
| { | ||
| $model = new TestModel(); | ||
| $model->refresh(); // no effect | ||
|
|
||
| $spans = $this->filterOnlyEloquentSpans(); | ||
| $this->assertCount(1, $spans); | ||
|
|
||
| $span = $spans[0]; | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::refresh', $span->getName()); | ||
| $this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $span->getAttributes()->get('laravel.eloquent.model')); | ||
| $this->assertSame('test_models', $span->getAttributes()->get('laravel.eloquent.table')); | ||
| $this->assertSame('refresh', $span->getAttributes()->get('laravel.eloquent.operation')); | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.