diff --git a/src/SDK/Common/Util/functions.php b/src/SDK/Common/Util/functions.php index eff0328e1..4120dada1 100644 --- a/src/SDK/Common/Util/functions.php +++ b/src/SDK/Common/Util/functions.php @@ -5,6 +5,7 @@ namespace OpenTelemetry\SDK\Common\Util; use Closure; +use Exception; use function get_class; use ReflectionFunction; use stdClass; @@ -50,3 +51,21 @@ function weaken(Closure $closure, ?object &$target = null): Closure ? static fn (...$args) => ($obj = $ref->get()) ? $closure->call($obj, ...$args) : null : static fn (...$args) => ($obj = $ref->get()) ? $closure->bindTo($obj)(...$args) : null; } + +/** + * Returns whether an iterable is empty or not + */ +function isEmpty(iterable $list) +{ + try { + foreach ($list as $value) { + break; + } + } + //catch exception + catch (Exception $e) { + return true; + } + + return empty($list); +} diff --git a/src/SDK/Metrics/Exporters/AbstractExporter.php b/src/SDK/Metrics/Exporters/AbstractExporter.php index 2897a4b59..8b777a516 100644 --- a/src/SDK/Metrics/Exporters/AbstractExporter.php +++ b/src/SDK/Metrics/Exporters/AbstractExporter.php @@ -7,6 +7,7 @@ use Exception; use InvalidArgumentException; use OpenTelemetry\API\Metrics as API; +use function OpenTelemetry\SDK\Common\Util\isEmpty; use OpenTelemetry\SDK\Metrics\Exceptions\RetryableExportException; abstract class AbstractExporter implements API\ExporterInterface @@ -16,7 +17,7 @@ abstract class AbstractExporter implements API\ExporterInterface */ public function export(iterable $metrics): int { - if (empty($metrics)) { + if (isEmpty($metrics)) { return API\ExporterInterface::SUCCESS; } diff --git a/src/SDK/Trace/Behavior/SpanExporterTrait.php b/src/SDK/Trace/Behavior/SpanExporterTrait.php index 226469a2d..641ff6fe9 100644 --- a/src/SDK/Trace/Behavior/SpanExporterTrait.php +++ b/src/SDK/Trace/Behavior/SpanExporterTrait.php @@ -4,6 +4,7 @@ namespace OpenTelemetry\SDK\Trace\Behavior; +use function OpenTelemetry\SDK\Common\Util\isEmpty; use OpenTelemetry\SDK\Trace\SpanDataInterface; use OpenTelemetry\SDK\Trace\SpanExporterInterface; @@ -40,7 +41,7 @@ public function export(iterable $spans): int return SpanExporterInterface::STATUS_FAILED_NOT_RETRYABLE; } - if (empty($spans)) { + if (isEmpty($spans)) { return SpanExporterInterface::STATUS_SUCCESS; }