Skip to content

Commit

Permalink
do not export empty metrics (#907)
Browse files Browse the repository at this point in the history
If no metrics have been generated, do not send an empty export request (except via forceFlush())

Closes 905
  • Loading branch information
brettmc authored Jan 9, 2023
1 parent 558e7ce commit 2bd6bc5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/SDK/Metrics/MetricReader/ExportingReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ private function doCollect(): bool
$metrics[] = $source->collect($timestamp);
}

if ($metrics === []) {
return true;
}

return $this->exporter->export($metrics);
}

Expand Down
2 changes: 0 additions & 2 deletions tests/Unit/SDK/Metrics/MeterProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public function test_shutdown_calls_metric_reader_shutdown(): void
ClockFactory::getDefault(),
Attributes::factory(),
new InstrumentationScopeFactory(Attributes::factory()),
/** @phpstan-ignore-next-line */
[$metricReader->reveal()],
new CriteriaViewRegistry(),
null,
Expand Down Expand Up @@ -123,7 +122,6 @@ public function test_force_flush_calls_metric_reader_force_flush(): void
ClockFactory::getDefault(),
Attributes::factory(),
new InstrumentationScopeFactory(Attributes::factory()),
/** @phpstan-ignore-next-line */
[$metricReader->reveal()],
new CriteriaViewRegistry(),
null,
Expand Down
34 changes: 33 additions & 1 deletion tests/Unit/SDK/Metrics/MetricReader/ExportingReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,50 @@ public function test_collect_collects_sources_with_current_timestamp(): void
public function test_shutdown_calls_exporter_shutdown(): void
{
$exporter = $this->createMock(MetricExporterInterface::class);
$exporter->expects($this->once())->method('shutdown')->willReturn(true);
$clock = new TestClock();
$reader = new ExportingReader($exporter, $clock);

$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);
Expand Down

0 comments on commit 2bd6bc5

Please sign in to comment.