From e4b186a5ae9a1705aeee5209318aa7293b56a948 Mon Sep 17 00:00:00 2001 From: Alexandre Choura Date: Mon, 16 Sep 2024 09:43:59 +0200 Subject: [PATCH] perf: Change condition checks --- src/DDTrace/OpenTelemetry/Context.php | 13 +++++++++---- src/DDTrace/OpenTelemetry/Span.php | 23 ++++++++++++----------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/DDTrace/OpenTelemetry/Context.php b/src/DDTrace/OpenTelemetry/Context.php index 62cdaf938f5..f8fddd5c445 100644 --- a/src/DDTrace/OpenTelemetry/Context.php +++ b/src/DDTrace/OpenTelemetry/Context.php @@ -27,6 +27,9 @@ final class Context implements ContextInterface /** @var ContextStorageInterface&ExecutionContextAwareInterface */ private static ContextStorageInterface $storage; + /** @var string $storageClass */ + private static string $storageClass = ''; + // Optimization for spans to avoid copying the context array. private static ContextKeyInterface $spanContextKey; private ?object $span = null; @@ -58,11 +61,13 @@ public static function setStorage(ContextStorageInterface $storage): void */ public static function storage(): ContextStorageInterface { - if (class_exists('\OpenTelemetry\Context\FiberBoundContextStorageExecutionAwareBC')) { - return self::$storage ??= new FiberBoundContextStorageExecutionAwareBC(); - } else { - return self::$storage ??= new ContextStorage(); + if (self::$storageClass === '') { + self::$storageClass = class_exists('\OpenTelemetry\Context\FiberBoundContextStorageExecutionAwareBC') + ? '\OpenTelemetry\Context\FiberBoundContextStorageExecutionAwareBC' // v1.1+ + : '\OpenTelemetry\Context\ContextStorage'; } + + return self::$storage ??= new self::$storageClass(); } /** diff --git a/src/DDTrace/OpenTelemetry/Span.php b/src/DDTrace/OpenTelemetry/Span.php index d1396d328b7..dd5c83153af 100644 --- a/src/DDTrace/OpenTelemetry/Span.php +++ b/src/DDTrace/OpenTelemetry/Span.php @@ -225,7 +225,7 @@ public function toSpanData(): SpanDataInterface $this->updateSpanLinks(); $this->updateSpanEvents(); - if (in_array('addLink', get_class_methods(SpanInterface::class))) { + if (PHP_VERSION_ID < 80100) { return new ImmutableSpan( $this, $this->getName(), @@ -233,22 +233,23 @@ public function toSpanData(): SpanDataInterface $this->events, Attributes::create(array_merge($this->span->meta, $this->span->metrics)), $this->totalRecordedEvents, - $this->totalRecordedLinks, StatusData::create($this->status->getCode(), $this->status->getDescription()), $hasEnded ? $this->span->getStartTime() + $this->span->getDuration() : 0, $this->hasEnded(), ); } else { + // v1.1 backward compatibility: totalRecordedLinks parameter added return new ImmutableSpan( - $this, - $this->getName(), - $this->links, - $this->events, - Attributes::create(array_merge($this->span->meta, $this->span->metrics)), - $this->totalRecordedEvents, - StatusData::create($this->status->getCode(), $this->status->getDescription()), - $hasEnded ? $this->span->getStartTime() + $this->span->getDuration() : 0, - $this->hasEnded(), + span: $this, + name: $this->getName(), + links: $this->links, + events: $this->events, + attributes: Attributes::create(array_merge($this->span->meta, $this->span->metrics)), + totalRecordedEvents: $this->totalRecordedEvents, + totalRecordedLinks: $this->totalRecordedLinks, + status: StatusData::create($this->status->getCode(), $this->status->getDescription()), + endEpochNanos: $hasEnded ? $this->span->getStartTime() + $this->span->getDuration() : 0, + hasEnded: $this->hasEnded(), ); } }