Skip to content

Commit 1c185dc

Browse files
committed
fix: move eval to fixture
1 parent 28d1968 commit 1c185dc

File tree

3 files changed

+112
-20
lines changed

3 files changed

+112
-20
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class TestModel extends Model
10+
{
11+
protected $table = 'test_models';
12+
protected $fillable = ['name'];
13+
}

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Facades\Http;
1010
use Illuminate\Support\Facades\Log;
1111
use OpenTelemetry\SemConv\TraceAttributes;
12+
use OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel;
1213

1314
/** @psalm-suppress UnusedClass */
1415
class LaravelInstrumentationTest extends TestCase
@@ -86,12 +87,6 @@ public function test_cache_log_db(): void
8687

8788
public function test_eloquent_operations(): void
8889
{
89-
/** @var class-string<\Illuminate\Database\Eloquent\Model> */
90-
$model = new class() extends \Illuminate\Database\Eloquent\Model {
91-
protected $table = 'test_models';
92-
protected $fillable = ['name'];
93-
};
94-
9590
// Assert storage is empty before interacting with the database
9691
$this->assertCount(0, $this->storage);
9792

@@ -103,16 +98,13 @@ public function test_eloquent_operations(): void
10398
updated_at DATETIME
10499
)');
105100

106-
$this->router()->get('/eloquent', function () use ($modelClass) {
101+
$this->router()->get('/eloquent', function () {
107102
try {
108-
/** @var \Illuminate\Database\Eloquent\Model $model */
109-
$model = new $modelClass();
110-
111103
// Test create
112-
$created = $modelClass::create(['name' => 'test']);
104+
$created = TestModel::create(['name' => 'test']);
113105

114106
// Test find
115-
$found = $modelClass::find($created->id);
107+
$found = TestModel::find($created->id);
116108

117109
// Test update
118110
$found->update(['name' => 'updated']);
@@ -161,35 +153,35 @@ public function test_eloquent_operations(): void
161153
$createSpan = array_values(array_filter($eloquentSpans, function ($span) {
162154
return $span->getAttributes()->get('laravel.eloquent.operation') === 'create';
163155
}))[0];
164-
$this->assertSame('TestModel::create', $createSpan->getName());
165-
$this->assertSame('TestModel', $createSpan->getAttributes()->get('laravel.eloquent.model'));
156+
$this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::create', $createSpan->getName());
157+
$this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $createSpan->getAttributes()->get('laravel.eloquent.model'));
166158
$this->assertSame('test_models', $createSpan->getAttributes()->get('laravel.eloquent.table'));
167159
$this->assertSame('create', $createSpan->getAttributes()->get('laravel.eloquent.operation'));
168160

169161
// Find span
170162
$findSpan = array_values(array_filter($eloquentSpans, function ($span) {
171163
return $span->getAttributes()->get('laravel.eloquent.operation') === 'find';
172164
}))[0];
173-
$this->assertSame('TestModel::find', $findSpan->getName());
174-
$this->assertSame('TestModel', $findSpan->getAttributes()->get('laravel.eloquent.model'));
165+
$this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::find', $findSpan->getName());
166+
$this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $findSpan->getAttributes()->get('laravel.eloquent.model'));
175167
$this->assertSame('test_models', $findSpan->getAttributes()->get('laravel.eloquent.table'));
176168
$this->assertSame('find', $findSpan->getAttributes()->get('laravel.eloquent.operation'));
177169

178170
// Update span
179171
$updateSpan = array_values(array_filter($eloquentSpans, function ($span) {
180172
return $span->getAttributes()->get('laravel.eloquent.operation') === 'update';
181173
}))[0];
182-
$this->assertSame('TestModel::update', $updateSpan->getName());
183-
$this->assertSame('TestModel', $updateSpan->getAttributes()->get('laravel.eloquent.model'));
174+
$this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::update', $updateSpan->getName());
175+
$this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $updateSpan->getAttributes()->get('laravel.eloquent.model'));
184176
$this->assertSame('test_models', $updateSpan->getAttributes()->get('laravel.eloquent.table'));
185177
$this->assertSame('update', $updateSpan->getAttributes()->get('laravel.eloquent.operation'));
186178

187179
// Delete span
188180
$deleteSpan = array_values(array_filter($eloquentSpans, function ($span) {
189181
return $span->getAttributes()->get('laravel.eloquent.operation') === 'delete';
190182
}))[0];
191-
$this->assertSame('TestModel::delete', $deleteSpan->getName());
192-
$this->assertSame('TestModel', $deleteSpan->getAttributes()->get('laravel.eloquent.model'));
183+
$this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel::delete', $deleteSpan->getName());
184+
$this->assertSame('OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Fixtures\Models\TestModel', $deleteSpan->getAttributes()->get('laravel.eloquent.model'));
193185
$this->assertSame('test_models', $deleteSpan->getAttributes()->get('laravel.eloquent.table'));
194186
$this->assertSame('delete', $deleteSpan->getAttributes()->get('laravel.eloquent.operation'));
195187
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Instrumentation\Symfony\Propagation;
6+
7+
use OpenTelemetry\Context\Propagation\PropagationGetterInterface;
8+
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
9+
use Symfony\Component\Messenger\Envelope;
10+
use Symfony\Component\Messenger\Stamp\SerializerStamp;
11+
12+
/**
13+
* Propagator for handling OpenTelemetry context in Symfony Messenger envelopes.
14+
* This class handles both injection and extraction of context through SerializerStamp.
15+
*/
16+
final class EnvelopeContextPropagator implements PropagationGetterInterface, PropagationSetterInterface
17+
{
18+
private const CONTEXT_KEY = 'otel_context';
19+
20+
private static ?self $instance = null;
21+
22+
public static function getInstance(): self
23+
{
24+
if (null === self::$instance) {
25+
self::$instance = new self();
26+
}
27+
return self::$instance;
28+
}
29+
30+
/**
31+
* @param array<string, mixed> $carrier
32+
* @return array<int, string>
33+
*/
34+
public function keys($carrier): array
35+
{
36+
return array_keys($carrier);
37+
}
38+
39+
/**
40+
* @param array<string, mixed> $carrier
41+
*/
42+
public function get($carrier, string $key): ?string
43+
{
44+
return $carrier[$key] ?? null;
45+
}
46+
47+
/**
48+
* @param array<string, mixed> $carrier
49+
*/
50+
public function set(&$carrier, string $key, string $value): void
51+
{
52+
$carrier[$key] = $value;
53+
}
54+
55+
/**
56+
* Injects OpenTelemetry context into a Symfony Messenger envelope.
57+
*/
58+
public function injectContextIntoEnvelope(Envelope $envelope, array $context): Envelope
59+
{
60+
if (empty($context)) {
61+
return $envelope;
62+
}
63+
64+
$serializerContext = [
65+
self::CONTEXT_KEY => $context,
66+
];
67+
68+
return $envelope->with(new SerializerStamp($serializerContext));
69+
}
70+
71+
/**
72+
* Extracts OpenTelemetry context from a Symfony Messenger envelope.
73+
*/
74+
public function extractContextFromEnvelope(Envelope $envelope): ?array
75+
{
76+
$serializerStamps = $envelope->all(SerializerStamp::class);
77+
foreach ($serializerStamps as $serializerStamp) {
78+
/** @var SerializerStamp $serializerStamp */
79+
$context = $serializerStamp->getContext();
80+
if (isset($context[self::CONTEXT_KEY])) {
81+
return $context[self::CONTEXT_KEY];
82+
}
83+
}
84+
85+
return null;
86+
}
87+
}

0 commit comments

Comments
 (0)