Skip to content

Commit

Permalink
ensure zipkin tags and attributes are cast to strings (#1406)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettmc authored Oct 16, 2024
1 parent 2c1da9a commit 01d0fd3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Contrib/Zipkin/SpanConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ private function convertSpan(SpanDataInterface $span): array
}

if ($span->getTotalDroppedEvents() > 0) {
$row['tags'][self::KEY_DROPPED_EVENTS_COUNT] = $span->getTotalDroppedEvents();
$row['tags'][self::KEY_DROPPED_EVENTS_COUNT] = (string) $span->getTotalDroppedEvents();
}

if ($span->getTotalDroppedLinks() > 0) {
$row['tags'][self::KEY_DROPPED_LINKS_COUNT] = $span->getTotalDroppedLinks();
$row['tags'][self::KEY_DROPPED_LINKS_COUNT] = (string) $span->getTotalDroppedLinks();
}

$droppedAttributes = $span->getAttributes()->getDroppedAttributesCount()
+ $span->getInstrumentationScope()->getAttributes()->getDroppedAttributesCount()
+ $span->getResource()->getAttributes()->getDroppedAttributesCount();

if ($droppedAttributes > 0) {
$row['tags'][self::KEY_DROPPED_ATTRIBUTES_COUNT] = $droppedAttributes;
$row['tags'][self::KEY_DROPPED_ATTRIBUTES_COUNT] = (string) $droppedAttributes;
}

if (($span->getKind() === SpanKind::KIND_CLIENT) || ($span->getKind() === SpanKind::KIND_PRODUCER)) {
Expand Down Expand Up @@ -201,7 +201,7 @@ private static function toAnnotation(EventInterface $event): array
'value' => $value,
];
if ($event->getAttributes()->getDroppedAttributesCount() > 0) {
$annotation[self::KEY_DROPPED_ATTRIBUTES_COUNT] = $event->getAttributes()->getDroppedAttributesCount();
$annotation[self::KEY_DROPPED_ATTRIBUTES_COUNT] = (string) $event->getAttributes()->getDroppedAttributesCount();
}

return $annotation;
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Contrib/Zipkin/ZipkinSpanConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,11 @@ public function test_displays_non_zero_dropped_counts(int $dropped, bool $expect

if ($expected) {
$this->assertArrayHasKey(SpanConverter::KEY_DROPPED_EVENTS_COUNT, $tags);
$this->assertIsString($tags[SpanConverter::KEY_DROPPED_EVENTS_COUNT]);
$this->assertArrayHasKey(SpanConverter::KEY_DROPPED_LINKS_COUNT, $tags);
$this->assertIsString($tags[SpanConverter::KEY_DROPPED_EVENTS_COUNT]);
$this->assertArrayHasKey(SpanConverter::KEY_DROPPED_ATTRIBUTES_COUNT, $tags);
$this->assertIsString($tags[SpanConverter::KEY_DROPPED_EVENTS_COUNT]);
} else {
$this->assertArrayNotHasKey(SpanConverter::KEY_DROPPED_EVENTS_COUNT, $tags);
$this->assertArrayNotHasKey(SpanConverter::KEY_DROPPED_LINKS_COUNT, $tags);
Expand All @@ -280,4 +283,27 @@ public static function droppedProvider(): array
'some dropped' => [1, true],
];
}

public function test_events(): void
{
$eventAttributes = $this->createMock(AttributesInterface::class);
$eventAttributes->method('getDroppedAttributesCount')->willReturn(99);
$attributes = [
'a_one' => 123,
'a_two' => 3.14159,
'a_three' => true,
'a_four' => false,
];
$eventAttributes->method('count')->willReturn(count($attributes));
$eventAttributes->method('toArray')->willReturn($attributes);
$span = (new SpanData())
->setName('events.test')
->addEvent('event.one', $eventAttributes);
$converted = (new SpanConverter())->convert([$span])[0];
$annotations = $converted['annotations'][0];

$this->assertIsInt($annotations['timestamp']);
$this->assertIsString($annotations['value']);
$this->assertIsString($annotations[SpanConverter::KEY_DROPPED_ATTRIBUTES_COUNT]);
}
}

0 comments on commit 01d0fd3

Please sign in to comment.