Skip to content

Commit

Permalink
perf: condition check
Browse files Browse the repository at this point in the history
  • Loading branch information
PROFeNoM committed Sep 16, 2024
1 parent 58c87b4 commit 1d924b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/DDTrace/OpenTelemetry/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/DDTrace/OpenTelemetry/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 1d924b4

Please sign in to comment.