Skip to content

Commit

Permalink
perf: Change condition checks
Browse files Browse the repository at this point in the history
  • Loading branch information
PROFeNoM committed Sep 16, 2024
1 parent 58c87b4 commit e4b186a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 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
23 changes: 12 additions & 11 deletions src/DDTrace/OpenTelemetry/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,30 +225,31 @@ 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(),
$this->links,
$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(),
);
}
}
Expand Down

0 comments on commit e4b186a

Please sign in to comment.