Skip to content

Commit e4b186a

Browse files
committed
perf: Change condition checks
1 parent 58c87b4 commit e4b186a

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/DDTrace/OpenTelemetry/Context.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ final class Context implements ContextInterface
2727
/** @var ContextStorageInterface&ExecutionContextAwareInterface */
2828
private static ContextStorageInterface $storage;
2929

30+
/** @var string $storageClass */
31+
private static string $storageClass = '';
32+
3033
// Optimization for spans to avoid copying the context array.
3134
private static ContextKeyInterface $spanContextKey;
3235
private ?object $span = null;
@@ -58,11 +61,13 @@ public static function setStorage(ContextStorageInterface $storage): void
5861
*/
5962
public static function storage(): ContextStorageInterface
6063
{
61-
if (class_exists('\OpenTelemetry\Context\FiberBoundContextStorageExecutionAwareBC')) {
62-
return self::$storage ??= new FiberBoundContextStorageExecutionAwareBC();
63-
} else {
64-
return self::$storage ??= new ContextStorage();
64+
if (self::$storageClass === '') {
65+
self::$storageClass = class_exists('\OpenTelemetry\Context\FiberBoundContextStorageExecutionAwareBC')
66+
? '\OpenTelemetry\Context\FiberBoundContextStorageExecutionAwareBC' // v1.1+
67+
: '\OpenTelemetry\Context\ContextStorage';
6568
}
69+
70+
return self::$storage ??= new self::$storageClass();
6671
}
6772

6873
/**

src/DDTrace/OpenTelemetry/Span.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,30 +225,31 @@ public function toSpanData(): SpanDataInterface
225225
$this->updateSpanLinks();
226226
$this->updateSpanEvents();
227227

228-
if (in_array('addLink', get_class_methods(SpanInterface::class))) {
228+
if (PHP_VERSION_ID < 80100) {
229229
return new ImmutableSpan(
230230
$this,
231231
$this->getName(),
232232
$this->links,
233233
$this->events,
234234
Attributes::create(array_merge($this->span->meta, $this->span->metrics)),
235235
$this->totalRecordedEvents,
236-
$this->totalRecordedLinks,
237236
StatusData::create($this->status->getCode(), $this->status->getDescription()),
238237
$hasEnded ? $this->span->getStartTime() + $this->span->getDuration() : 0,
239238
$this->hasEnded(),
240239
);
241240
} else {
241+
// v1.1 backward compatibility: totalRecordedLinks parameter added
242242
return new ImmutableSpan(
243-
$this,
244-
$this->getName(),
245-
$this->links,
246-
$this->events,
247-
Attributes::create(array_merge($this->span->meta, $this->span->metrics)),
248-
$this->totalRecordedEvents,
249-
StatusData::create($this->status->getCode(), $this->status->getDescription()),
250-
$hasEnded ? $this->span->getStartTime() + $this->span->getDuration() : 0,
251-
$this->hasEnded(),
243+
span: $this,
244+
name: $this->getName(),
245+
links: $this->links,
246+
events: $this->events,
247+
attributes: Attributes::create(array_merge($this->span->meta, $this->span->metrics)),
248+
totalRecordedEvents: $this->totalRecordedEvents,
249+
totalRecordedLinks: $this->totalRecordedLinks,
250+
status: StatusData::create($this->status->getCode(), $this->status->getDescription()),
251+
endEpochNanos: $hasEnded ? $this->span->getStartTime() + $this->span->getDuration() : 0,
252+
hasEnded: $this->hasEnded(),
252253
);
253254
}
254255
}

0 commit comments

Comments
 (0)