Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure zipkin tags and attributes are cast to strings #1406

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
}