do not export empty metrics (#907)

If no metrics have been generated, do not send an empty export request (except via forceFlush())

Closes 905
This commit is contained in:
Brett McBride 2023-01-10 09:23:54 +11:00 committed by GitHub
parent 558e7ce30e
commit 2bd6bc5ff7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -70,6 +70,10 @@ final class ExportingReader implements MetricReaderInterface, MetricSourceRegist
$metrics[] = $source->collect($timestamp);
}
if ($metrics === []) {
return true;
}
return $this->exporter->export($metrics);
}

View File

@ -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,

View File

@ -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);