Skip to content

Commit

Permalink
Remove SDK weakmap dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay committed Oct 15, 2022
1 parent f5429f2 commit 2e523d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
2 changes: 0 additions & 2 deletions deptrac.baseline.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
deptrac:
skip_violations:
OpenTelemetry\API\Common\Instrumentation\Instrumentation:
- OpenTelemetry\SDK\Common\Util\WeakMap
39 changes: 32 additions & 7 deletions src/API/Common/Instrumentation/Instrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace OpenTelemetry\API\Common\Instrumentation;

use ArrayAccess;
use function assert;
use function class_exists;
use OpenTelemetry\API\Metrics\MeterInterface;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Metrics\Noop\NoopMeterProvider;
Expand All @@ -16,7 +18,7 @@
use OpenTelemetry\Context\ContextStorageInterface;
use OpenTelemetry\Context\Propagation\NoopTextMapPropagator;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\SDK\Common\Util\WeakMap;
use const PHP_VERSION_ID;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

Expand All @@ -28,10 +30,10 @@ final class Instrumentation
private ?string $version;
private ?string $schemaUrl;
private iterable $attributes;
/** @var ArrayAccess<TracerProviderInterface, TracerInterface> */
private ArrayAccess $tracers;
/** @var ArrayAccess<MeterProviderInterface, MeterInterface> */
private ArrayAccess $meters;
/** @var ArrayAccess<TracerProviderInterface, TracerInterface>|null */
private ?ArrayAccess $tracers;
/** @var ArrayAccess<MeterProviderInterface, MeterInterface>|null */
private ?ArrayAccess $meters;

public function __construct(string $name, ?string $version = null, ?string $schemaUrl = null, iterable $attributes = [], ?ContextStorageInterface $contextStorage = null)
{
Expand All @@ -40,8 +42,23 @@ public function __construct(string $name, ?string $version = null, ?string $sche
$this->version = $version;
$this->schemaUrl = $schemaUrl;
$this->attributes = $attributes;
$this->tracers = WeakMap::create();
$this->meters = WeakMap::create();
$this->tracers = self::createWeakMap();
$this->meters = self::createWeakMap();
}

private static function createWeakMap(): ?ArrayAccess
{
if (PHP_VERSION_ID < 80000) {
return null;
}

/** @phan-suppress-next-line PhanUndeclaredClassReference */
assert(class_exists(\WeakMap::class, false));
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$map = new \WeakMap();
assert($map instanceof ArrayAccess);

return $map;
}

private function get(ContextKeyInterface $contextKey)
Expand All @@ -54,6 +71,10 @@ public function tracer(): TracerInterface
static $noop;
$tracerProvider = $this->get(ContextKeys::tracerProvider()) ?? $noop ??= new NoopTracerProvider();

if ($this->tracers === null) {
return $tracerProvider->getTracer($this->name, $this->version, $this->schemaUrl, $this->attributes);
}

return $this->tracers[$tracerProvider] ??= $tracerProvider->getTracer($this->name, $this->version, $this->schemaUrl, $this->attributes);
}

Expand All @@ -62,6 +83,10 @@ public function meter(): MeterInterface
static $noop;
$meterProvider = $this->get(ContextKeys::meterProvider()) ?? $noop ??= new NoopMeterProvider();

if ($this->meters === null) {
return $meterProvider->getMeter($this->name, $this->version, $this->schemaUrl, $this->attributes);
}

return $this->meters[$meterProvider] ??= $meterProvider->getMeter($this->name, $this->version, $this->schemaUrl, $this->attributes);
}

Expand Down

0 comments on commit 2e523d6

Please sign in to comment.