From 1c14c3bcb572a93492209819efd850db943116e3 Mon Sep 17 00:00:00 2001 From: Alexandre Choura Date: Mon, 16 Sep 2024 11:17:56 +0200 Subject: [PATCH] perf: condition check --- src/DDTrace/OpenTelemetry/Context.php | 13 +++++++++---- src/DDTrace/OpenTelemetry/Span.php | 3 ++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/DDTrace/OpenTelemetry/Context.php b/src/DDTrace/OpenTelemetry/Context.php index 62cdaf938f..f8fddd5c44 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 d1396d328b..c6aa4ae95e 100644 --- a/src/DDTrace/OpenTelemetry/Span.php +++ b/src/DDTrace/OpenTelemetry/Span.php @@ -225,7 +225,8 @@ public function toSpanData(): SpanDataInterface $this->updateSpanLinks(); $this->updateSpanEvents(); - if (in_array('addLink', get_class_methods(SpanInterface::class))) { + if (method_exists(SpanInterface::class, 'addLink')) { + // v1.1 backward compatibility: totalRecordedLinks parameter added return new ImmutableSpan( $this, $this->getName(),