Skip to content

Commit

Permalink
adding PushMetricExporterInterface
Browse files Browse the repository at this point in the history
open-telemetry/opentelemetry-specification#3563 clarifies the behaviour of
forceFlush with push/non-push metric exporters.
Break forceFlush out into a PushMetricExporterInterface, and update ExportingReader to only collect/flush
if exporter is a push metric exporter.
  • Loading branch information
brettmc committed Aug 9, 2023
1 parent 9f35861 commit 215877f
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 27 deletions.
4 changes: 2 additions & 2 deletions examples/metrics/getting_started.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\SDK\Metrics\Data\Temporality;
use OpenTelemetry\SDK\Metrics\MeterProvider;
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricsExporter;
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporter;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;

Expand All @@ -18,7 +18,7 @@
*/

$reader = new ExportingReader(
new ConsoleMetricsExporter(Temporality::DELTA)
new ConsoleMetricExporter(Temporality::DELTA)
);

$meterProvider = MeterProvider::builder()
Expand Down
4 changes: 2 additions & 2 deletions examples/metrics/weak-reference-observables.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use OpenTelemetry\API\Metrics\ObserverInterface;
use OpenTelemetry\SDK\Metrics\Data\Temporality;
use OpenTelemetry\SDK\Metrics\MeterProvider;
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricsExporter;
use OpenTelemetry\SDK\Metrics\MetricExporter\ConsoleMetricExporter;
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;

Expand All @@ -19,7 +19,7 @@
*/

$reader = new ExportingReader(
new ConsoleMetricsExporter(Temporality::DELTA)
new ConsoleMetricExporter(Temporality::DELTA)
);

$meterProvider = MeterProvider::builder()
Expand Down
4 changes: 2 additions & 2 deletions src/Contrib/Otlp/MetricExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Opentelemetry\Proto\Collector\Metrics\V1\ExportMetricsServiceResponse;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
use OpenTelemetry\SDK\Metrics\Data\Temporality;
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
use OpenTelemetry\SDK\Metrics\MetricMetadataInterface;
use OpenTelemetry\SDK\Metrics\PushMetricExporterInterface;
use RuntimeException;
use Throwable;

Expand All @@ -18,7 +18,7 @@
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/experimental/serialization/json.md#json-file-serialization
* @psalm-import-type SUPPORTED_CONTENT_TYPES from ProtobufSerializer
*/
final class MetricExporter implements MetricExporterInterface
final class MetricExporter implements PushMetricExporterInterface
{
use LogsMessagesTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
use OpenTelemetry\SDK\Metrics\Data\Metric;
use OpenTelemetry\SDK\Metrics\Data\Temporality;
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
use OpenTelemetry\SDK\Metrics\MetricMetadataInterface;
use OpenTelemetry\SDK\Metrics\PushMetricExporterInterface;
use OpenTelemetry\SDK\Resource\ResourceInfo;

/**
* Console metrics exporter.
* Note that the output is human-readable JSON, not compatible with OTLP.
*/
class ConsoleMetricsExporter implements MetricExporterInterface
class ConsoleMetricExporter implements PushMetricExporterInterface
{
/**
* @var string|Temporality|null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class ConsoleMetricExporterFactory implements MetricExporterFactoryInterface
{
public function create(): MetricExporterInterface
{
return new ConsoleMetricsExporter();
return new ConsoleMetricExporter();
}
}
5 changes: 0 additions & 5 deletions src/SDK/Metrics/MetricExporter/InMemoryExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,4 @@ public function shutdown(): bool

return true;
}

public function forceFlush(): bool
{
return !$this->closed;
}
}
5 changes: 0 additions & 5 deletions src/SDK/Metrics/MetricExporter/NoopMetricExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ public function shutdown(): bool
{
return true;
}

public function forceFlush(): bool
{
return true;
}
}
2 changes: 0 additions & 2 deletions src/SDK/Metrics/MetricExporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ public function temporality(MetricMetadataInterface $metric);
public function export(iterable $batch): bool;

public function shutdown(): bool;

public function forceFlush(): bool;
}
10 changes: 7 additions & 3 deletions src/SDK/Metrics/MetricReader/ExportingReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OpenTelemetry\SDK\Metrics\MetricSourceInterface;
use OpenTelemetry\SDK\Metrics\MetricSourceProviderInterface;
use OpenTelemetry\SDK\Metrics\MetricSourceRegistryInterface;
use OpenTelemetry\SDK\Metrics\PushMetricExporterInterface;
use OpenTelemetry\SDK\Metrics\StalenessHandlerInterface;
use function spl_object_id;

Expand Down Expand Up @@ -139,10 +140,13 @@ public function forceFlush(): bool
if ($this->closed) {
return false;
}
if ($this->exporter instanceof PushMetricExporterInterface) {
$collect = $this->doCollect();
$forceFlush = $this->exporter->forceFlush();

$collect = $this->doCollect();
$forceFlush = $this->exporter->forceFlush();
return $collect && $forceFlush;
}

return $collect && $forceFlush;
return true;
}
}
12 changes: 12 additions & 0 deletions src/SDK/Metrics/PushMetricExporterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Metrics;

use OpenTelemetry\SDK\Metrics;

interface PushMetricExporterInterface extends Metrics\MetricExporterInterface
{
public function forceFlush(): bool;
}
15 changes: 12 additions & 3 deletions tests/Unit/SDK/Metrics/MetricReader/ExportingReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OpenTelemetry\SDK\Metrics\MetricReader\ExportingReader;
use OpenTelemetry\SDK\Metrics\MetricSourceInterface;
use OpenTelemetry\SDK\Metrics\MetricSourceProviderInterface;
use OpenTelemetry\SDK\Metrics\PushMetricExporterInterface;
use OpenTelemetry\SDK\Metrics\StalenessHandler\ImmediateStalenessHandler;
use OpenTelemetry\SDK\Metrics\StalenessHandlerInterface;
use OpenTelemetry\SDK\Resource\ResourceInfo;
Expand Down Expand Up @@ -186,20 +187,28 @@ public function test_shutdown_exports_metrics(): void
$this->assertTrue($reader->shutdown());
}

public function test_force_flush_calls_exporter_force_flush(): void
public function test_force_flush_calls_push_exporter_force_flush(): void
{
$exporter = $this->createMock(MetricExporterInterface::class);
$exporter = $this->createMock(PushMetricExporterInterface::class);
$exporter->expects($this->once())->method('forceFlush')->willReturn(true);
$reader = new ExportingReader($exporter);

$this->assertTrue($reader->forceFlush());
}

public function test_closed_reader_does_not_call_exporter_methods(): void
public function test_force_flush_with_non_push_exporter(): void
{
$exporter = $this->createMock(MetricExporterInterface::class);
$reader = new ExportingReader($exporter);

$this->assertTrue($reader->forceFlush());
}

public function test_closed_reader_does_not_call_exporter_methods(): void
{
$exporter = $this->createMock(PushMetricExporterInterface::class);
$reader = new ExportingReader($exporter);

$reader->shutdown();

$exporter->expects($this->never())->method('export');
Expand Down

0 comments on commit 215877f

Please sign in to comment.