Skip to content

Commit

Permalink
Use of "iterable" Type May Need Re-Evaluation Across the Repo (#783)
Browse files Browse the repository at this point in the history
* Otel-php:632 Move stack trace formatting out of Span class

* added function usages

* fix linting errors

* Added documentation for adding Span Attributes

* removing TracingUtl class

* Refactor TraceState's __toString method

* Added path with @Covers to remove warnings

* Added @coversDefaultClass annotation

* fix to check if iterable is empty

* fixed for faling test case
  • Loading branch information
amber0612 authored Jul 20, 2022
1 parent 32c4384 commit bf912ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/SDK/Common/Util/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OpenTelemetry\SDK\Common\Util;

use Closure;
use Exception;
use function get_class;
use ReflectionFunction;
use stdClass;
Expand Down Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion src/SDK/Metrics/Exporters/AbstractExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/SDK/Trace/Behavior/SpanExporterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit bf912ed

Please sign in to comment.