|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\TestUtils\Tests\Unit; |
| 6 | + |
| 7 | +use ArrayObject; |
| 8 | +use OpenTelemetry\API\Logs\LogRecord; |
| 9 | +use OpenTelemetry\API\Trace\SpanKind; |
| 10 | +use OpenTelemetry\SDK\Trace\ImmutableSpan; |
| 11 | +use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; |
| 12 | +use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; |
| 13 | +use OpenTelemetry\SDK\Trace\TracerProvider; |
| 14 | +use OpenTelemetry\TestUtils\TraceStructureAssertionTrait; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test to demonstrate that TraceStructureAssertionTrait only works with spans. |
| 19 | + */ |
| 20 | +class MixedBufferTest extends TestCase |
| 21 | +{ |
| 22 | + use TraceStructureAssertionTrait; |
| 23 | + |
| 24 | + private ArrayObject $sharedBuffer; |
| 25 | + private TracerProvider $tracerProvider; |
| 26 | + |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + // Create a shared buffer for both spans and logrecords |
| 30 | + $this->sharedBuffer = new ArrayObject(); |
| 31 | + |
| 32 | + // Create a TracerProvider with an InMemoryExporter using the shared buffer |
| 33 | + $this->tracerProvider = new TracerProvider( |
| 34 | + new SimpleSpanProcessor( |
| 35 | + new InMemoryExporter($this->sharedBuffer) |
| 36 | + ) |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Test that demonstrates the trait now automatically filters out logrecords. |
| 42 | + */ |
| 43 | + public function test_trait_automatically_filters_logrecords(): void |
| 44 | + { |
| 45 | + // Create a span |
| 46 | + $tracer = $this->tracerProvider->getTracer('test-tracer'); |
| 47 | + $span = $tracer->spanBuilder('test-span') |
| 48 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 49 | + ->startSpan(); |
| 50 | + $span->setAttribute('attribute.one', 'value1'); |
| 51 | + $span->end(); |
| 52 | + |
| 53 | + // Manually add a logrecord to the shared buffer |
| 54 | + $logRecord = new LogRecord('Test log message'); |
| 55 | + $this->sharedBuffer->append($logRecord); |
| 56 | + |
| 57 | + // Define the expected structure |
| 58 | + $expectedStructure = [ |
| 59 | + [ |
| 60 | + 'name' => 'test-span', |
| 61 | + 'kind' => SpanKind::KIND_SERVER, |
| 62 | + 'attributes' => [ |
| 63 | + 'attribute.one' => 'value1', |
| 64 | + ], |
| 65 | + ], |
| 66 | + ]; |
| 67 | + |
| 68 | + // This should now pass because the trait automatically filters out logrecords |
| 69 | + $this->assertTraceStructure($this->sharedBuffer, $expectedStructure); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Test that demonstrates the manual solution to filter out logrecords still works. |
| 74 | + */ |
| 75 | + public function test_manual_filtering_of_logrecords_still_works(): void |
| 76 | + { |
| 77 | + // Create a span |
| 78 | + $tracer = $this->tracerProvider->getTracer('test-tracer'); |
| 79 | + $span = $tracer->spanBuilder('test-span') |
| 80 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 81 | + ->startSpan(); |
| 82 | + $span->setAttribute('attribute.one', 'value1'); |
| 83 | + $span->end(); |
| 84 | + |
| 85 | + // Manually add a logrecord to the shared buffer |
| 86 | + $logRecord = new LogRecord('Test log message'); |
| 87 | + $this->sharedBuffer->append($logRecord); |
| 88 | + |
| 89 | + // Define the expected structure |
| 90 | + $expectedStructure = [ |
| 91 | + [ |
| 92 | + 'name' => 'test-span', |
| 93 | + 'kind' => SpanKind::KIND_SERVER, |
| 94 | + 'attributes' => [ |
| 95 | + 'attribute.one' => 'value1', |
| 96 | + ], |
| 97 | + ], |
| 98 | + ]; |
| 99 | + |
| 100 | + // Filter the buffer to only include spans |
| 101 | + $spansOnly = array_filter(iterator_to_array($this->sharedBuffer), function ($item) { |
| 102 | + return $item instanceof ImmutableSpan; |
| 103 | + }); |
| 104 | + |
| 105 | + // This should pass because we've filtered out the logrecords |
| 106 | + $this->assertTraceStructure($spansOnly, $expectedStructure); |
| 107 | + } |
| 108 | +} |
0 commit comments