From 01d0fd361821ba180e5dd267a3aad1e3c4245d1c Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Wed, 16 Oct 2024 23:03:20 +1100 Subject: [PATCH] ensure zipkin tags and attributes are cast to strings (#1406) --- src/Contrib/Zipkin/SpanConverter.php | 8 +++--- .../Zipkin/ZipkinSpanConverterTest.php | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Contrib/Zipkin/SpanConverter.php b/src/Contrib/Zipkin/SpanConverter.php index 1248a1007..e8a7d3045 100644 --- a/src/Contrib/Zipkin/SpanConverter.php +++ b/src/Contrib/Zipkin/SpanConverter.php @@ -148,11 +148,11 @@ 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() @@ -160,7 +160,7 @@ private function convertSpan(SpanDataInterface $span): array + $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)) { @@ -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; diff --git a/tests/Unit/Contrib/Zipkin/ZipkinSpanConverterTest.php b/tests/Unit/Contrib/Zipkin/ZipkinSpanConverterTest.php index 73f9c4340..a0ab591d2 100644 --- a/tests/Unit/Contrib/Zipkin/ZipkinSpanConverterTest.php +++ b/tests/Unit/Contrib/Zipkin/ZipkinSpanConverterTest.php @@ -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); @@ -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]); + } }