Skip to content

Commit

Permalink
adding PushMetricExporterInterface (#1097)
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 authored Aug 9, 2023
1 parent 09b7710 commit 80a387f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
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
2 changes: 1 addition & 1 deletion Metrics/MetricExporter/ConsoleMetricExporterFactory.php
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 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 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 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 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 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;
}

0 comments on commit 80a387f

Please sign in to comment.