diff --git a/src/SDK/Metrics/MetricReader/ExportingReader.php b/src/SDK/Metrics/MetricReader/ExportingReader.php index c56bf6f9..8bc849e4 100644 --- a/src/SDK/Metrics/MetricReader/ExportingReader.php +++ b/src/SDK/Metrics/MetricReader/ExportingReader.php @@ -70,6 +70,10 @@ final class ExportingReader implements MetricReaderInterface, MetricSourceRegist $metrics[] = $source->collect($timestamp); } + if ($metrics === []) { + return true; + } + return $this->exporter->export($metrics); } diff --git a/tests/Unit/SDK/Metrics/MeterProviderTest.php b/tests/Unit/SDK/Metrics/MeterProviderTest.php index 7615771f..ac6370aa 100644 --- a/tests/Unit/SDK/Metrics/MeterProviderTest.php +++ b/tests/Unit/SDK/Metrics/MeterProviderTest.php @@ -94,7 +94,6 @@ final class MeterProviderTest extends TestCase ClockFactory::getDefault(), Attributes::factory(), new InstrumentationScopeFactory(Attributes::factory()), - /** @phpstan-ignore-next-line */ [$metricReader->reveal()], new CriteriaViewRegistry(), null, @@ -123,7 +122,6 @@ final class MeterProviderTest extends TestCase ClockFactory::getDefault(), Attributes::factory(), new InstrumentationScopeFactory(Attributes::factory()), - /** @phpstan-ignore-next-line */ [$metricReader->reveal()], new CriteriaViewRegistry(), null, diff --git a/tests/Unit/SDK/Metrics/MetricReader/ExportingReaderTest.php b/tests/Unit/SDK/Metrics/MetricReader/ExportingReaderTest.php index d6ca5642..12df7211 100644 --- a/tests/Unit/SDK/Metrics/MetricReader/ExportingReaderTest.php +++ b/tests/Unit/SDK/Metrics/MetricReader/ExportingReaderTest.php @@ -157,7 +157,6 @@ final class ExportingReaderTest extends TestCase public function test_shutdown_calls_exporter_shutdown(): void { $exporter = $this->createMock(MetricExporterInterface::class); - $exporter->expects($this->once())->method('export')->willReturn(true); $exporter->expects($this->once())->method('shutdown')->willReturn(true); $clock = new TestClock(); $reader = new ExportingReader($exporter, $clock); @@ -165,10 +164,43 @@ final class ExportingReaderTest extends TestCase $this->assertTrue($reader->shutdown()); } + public function test_shutdown_does_not_export_empty_metrics(): void + { + $exporter = $this->createMock(MetricExporterInterface::class); + $exporter->expects($this->never())->method('export'); + $exporter->expects($this->once())->method('shutdown')->willReturn(true); + + $clock = new TestClock(); + $reader = new ExportingReader($exporter, $clock); + + $reader->shutdown(); + } + + public function test_shutdown_exports_metrics(): void + { + $exporter = $this->createMock(MetricExporterInterface::class); + $provider = $this->createMock(MetricSourceProviderInterface::class); + $source = $this->createMock(MetricSourceInterface::class); + $source->method('collect')->willReturn($this->createMock(Metric::class)); + $provider->method('create')->willReturn($source); + $exporter->method('temporality')->willReturn('foo'); + $exporter->expects($this->once())->method('export')->willReturn(true); + $exporter->expects($this->once())->method('shutdown')->willReturn(true); + + $clock = new TestClock(); + $reader = new ExportingReader($exporter, $clock); + $reader->add( + $provider, + $this->createMock(MetricMetadataInterface::class), + $this->createMock(StalenessHandlerInterface::class) + ); + + $this->assertTrue($reader->shutdown()); + } + public function test_force_flush_calls_exporter_force_flush(): void { $exporter = $this->createMock(MetricExporterInterface::class); - $exporter->expects($this->once())->method('export')->willReturn(true); $exporter->expects($this->once())->method('forceFlush')->willReturn(true); $clock = new TestClock(); $reader = new ExportingReader($exporter, $clock);