From 702e9ae5488ed9c0ad27c92d83185dbc54de3c08 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Mon, 15 Jul 2024 16:09:12 +0200 Subject: [PATCH] Normalize array of key names before converting to string --- .../Laravel/Features/CacheIntegration.php | 34 +++++++++++++++---- test/Sentry/Features/CacheIntegrationTest.php | 15 +++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/Sentry/Laravel/Features/CacheIntegration.php b/src/Sentry/Laravel/Features/CacheIntegration.php index 2e0d8dbe..0495cc55 100644 --- a/src/Sentry/Laravel/Features/CacheIntegration.php +++ b/src/Sentry/Laravel/Features/CacheIntegration.php @@ -103,9 +103,11 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void $this->withParentSpanIfSampled(function (Span $parentSpan) use ($event) { if ($event instanceof Events\RetrievingKey || $event instanceof Events\RetrievingManyKeys) { - $keys = $event instanceof Events\RetrievingKey - ? [$event->key] - : $event->keys; + $keys = $this->normalizeKeyOrKeys( + $event instanceof Events\RetrievingKey + ? [$event->key] + : $event->keys + ); $this->pushSpan( $parentSpan->startChild( @@ -120,9 +122,11 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void } if ($event instanceof Events\WritingKey || $event instanceof Events\WritingManyKeys) { - $keys = $event instanceof Events\WritingKey - ? [$event->key] - : $event->keys; + $keys = $this->normalizeKeyOrKeys( + $event instanceof Events\WritingKey + ? [$event->key] + : $event->keys + ); $this->pushSpan( $parentSpan->startChild( @@ -236,4 +240,22 @@ private function maybeHandleCacheEventAsEndOfSpan(Events\CacheEvent $event): boo return false; } + + /** + * Normalize the array of keys to a array of only strings. + * + * @param string|string[]|array $keyOrKeys + * + * @return string[] + */ + private function normalizeKeyOrKeys($keyOrKeys): array + { + if (is_string($keyOrKeys)) { + return [$keyOrKeys]; + } + + return collect($keyOrKeys)->map(function ($value, $key) { + return is_string($key) ? $key : $value; + })->values()->all(); + } } diff --git a/test/Sentry/Features/CacheIntegrationTest.php b/test/Sentry/Features/CacheIntegrationTest.php index 991ba950..52ed81f3 100644 --- a/test/Sentry/Features/CacheIntegrationTest.php +++ b/test/Sentry/Features/CacheIntegrationTest.php @@ -78,6 +78,19 @@ public function testCacheGetSpanIsRecordedForBatchOperation(): void $this->assertEquals(['foo', 'bar'], $span->getData()['cache.key']); } + public function testCacheGetSpanIsRecordedForMultipleOperation(): void + { + $this->markSkippedIfTracingEventsNotAvailable(); + + $span = $this->executeAndReturnMostRecentSpan(function () { + Cache::getMultiple(['foo', 'bar']); + }); + + $this->assertEquals('cache.get', $span->getOp()); + $this->assertEquals('foo, bar', $span->getDescription()); + $this->assertEquals(['foo', 'bar'], $span->getData()['cache.key']); + } + public function testCacheGetSpanIsRecordedWithCorrectHitData(): void { $this->markSkippedIfTracingEventsNotAvailable(); @@ -107,7 +120,7 @@ public function testCachePutSpanIsRecorded(): void $this->assertEquals(99, $span->getData()['cache.ttl']); } - public function testCachePutSpanIsRecordedForBatchOperation(): void + public function testCachePutSpanIsRecordedForManyOperation(): void { $this->markSkippedIfTracingEventsNotAvailable();