Skip to content

Commit

Permalink
move dropped warning into toSpanData, add trace+span id
Browse files Browse the repository at this point in the history
moving the warning removes an extra call to toSpanData, thus some overhead. Outside of tests, this function
is only called from span processors once
  • Loading branch information
brettmc committed Jun 18, 2024
1 parent f69bb8c commit d0819ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
23 changes: 13 additions & 10 deletions src/SDK/Trace/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,6 @@ public function end(int $endEpochNanos = null): void
return;
}

$spanData = $this->toSpanData();
if ($spanData->getTotalDroppedLinks() || $spanData->getTotalDroppedEvents() || $spanData->getAttributes()->getDroppedAttributesCount()) {
self::logWarning('Dropped span attributes, links or events', [
'attributes' => $spanData->getAttributes()->getDroppedAttributesCount(),
'links' => $spanData->getTotalDroppedLinks(),
'events' => $spanData->getTotalDroppedEvents(),
]);
}

$this->endEpochNanos = $endEpochNanos ?? Clock::getDefault()->now();
$this->hasEnded = true;

Expand Down Expand Up @@ -289,7 +280,7 @@ public function hasEnded(): bool

public function toSpanData(): SpanDataInterface
{
return new ImmutableSpan(
$spanData = new ImmutableSpan(
$this,
$this->name,
$this->links,
Expand All @@ -301,6 +292,18 @@ public function toSpanData(): SpanDataInterface
$this->endEpochNanos,
$this->hasEnded
);

if ($spanData->getTotalDroppedLinks() || $spanData->getTotalDroppedEvents() || $spanData->getAttributes()->getDroppedAttributesCount()) {
self::logWarning('Dropped span attributes, links or events', [
'trace_id' => $spanData->getTraceId(),
'span_id' => $spanData->getSpanId(),
'attributes' => $spanData->getAttributes()->getDroppedAttributesCount(),
'links' => $spanData->getTotalDroppedLinks(),
'events' => $spanData->getTotalDroppedEvents(),
]);
}

return $spanData;
}

/** @inheritDoc */
Expand Down
17 changes: 10 additions & 7 deletions tests/Unit/SDK/Trace/SpanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,15 +943,18 @@ private function assertSpanData(

private function expectDropped(int $attributes, int $events, int $links): void
{
$this->logWriter->expects($this->once())->method('write')->with(
$this->logWriter->expects($this->atLeastOnce())->method('write')->with(
$this->anything(),
$this->stringContains('Dropped span attributes'),
$this->equalTo([
'attributes' => $attributes,
'links' => $links,
'events' => $events,
'source' => 'OpenTelemetry\\SDK\\Trace\\Span',
])
$this->callback(function (array $context) use ($attributes, $events, $links) {
$this->assertSame($context['attributes'], $attributes);
$this->assertSame($context['events'], $events);
$this->assertSame($context['links'], $links);
$this->assertNotNull($context['trace_id']);
$this->assertNotNull($context['span_id']);

return true;
}),
);
}
}

0 comments on commit d0819ea

Please sign in to comment.