Skip to content

Commit abf561e

Browse files
authored
Normalize array of key names before converting to string (#923)
1 parent bfc7401 commit abf561e

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/Sentry/Laravel/Features/CacheIntegration.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void
103103

104104
$this->withParentSpanIfSampled(function (Span $parentSpan) use ($event) {
105105
if ($event instanceof Events\RetrievingKey || $event instanceof Events\RetrievingManyKeys) {
106-
$keys = $event instanceof Events\RetrievingKey
107-
? [$event->key]
108-
: $event->keys;
106+
$keys = $this->normalizeKeyOrKeys(
107+
$event instanceof Events\RetrievingKey
108+
? [$event->key]
109+
: $event->keys
110+
);
109111

110112
$this->pushSpan(
111113
$parentSpan->startChild(
@@ -120,9 +122,11 @@ public function handleCacheEventsForTracing(Events\CacheEvent $event): void
120122
}
121123

122124
if ($event instanceof Events\WritingKey || $event instanceof Events\WritingManyKeys) {
123-
$keys = $event instanceof Events\WritingKey
124-
? [$event->key]
125-
: $event->keys;
125+
$keys = $this->normalizeKeyOrKeys(
126+
$event instanceof Events\WritingKey
127+
? [$event->key]
128+
: $event->keys
129+
);
126130

127131
$this->pushSpan(
128132
$parentSpan->startChild(
@@ -236,4 +240,22 @@ private function maybeHandleCacheEventAsEndOfSpan(Events\CacheEvent $event): boo
236240

237241
return false;
238242
}
243+
244+
/**
245+
* Normalize the array of keys to a array of only strings.
246+
*
247+
* @param string|string[]|array<array-key, mixed> $keyOrKeys
248+
*
249+
* @return string[]
250+
*/
251+
private function normalizeKeyOrKeys($keyOrKeys): array
252+
{
253+
if (is_string($keyOrKeys)) {
254+
return [$keyOrKeys];
255+
}
256+
257+
return collect($keyOrKeys)->map(function ($value, $key) {
258+
return is_string($key) ? $key : $value;
259+
})->values()->all();
260+
}
239261
}

test/Sentry/Features/CacheIntegrationTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ public function testCacheGetSpanIsRecordedForBatchOperation(): void
7878
$this->assertEquals(['foo', 'bar'], $span->getData()['cache.key']);
7979
}
8080

81+
public function testCacheGetSpanIsRecordedForMultipleOperation(): void
82+
{
83+
$this->markSkippedIfTracingEventsNotAvailable();
84+
85+
$span = $this->executeAndReturnMostRecentSpan(function () {
86+
Cache::getMultiple(['foo', 'bar']);
87+
});
88+
89+
$this->assertEquals('cache.get', $span->getOp());
90+
$this->assertEquals('foo, bar', $span->getDescription());
91+
$this->assertEquals(['foo', 'bar'], $span->getData()['cache.key']);
92+
}
93+
8194
public function testCacheGetSpanIsRecordedWithCorrectHitData(): void
8295
{
8396
$this->markSkippedIfTracingEventsNotAvailable();
@@ -107,7 +120,7 @@ public function testCachePutSpanIsRecorded(): void
107120
$this->assertEquals(99, $span->getData()['cache.ttl']);
108121
}
109122

110-
public function testCachePutSpanIsRecordedForBatchOperation(): void
123+
public function testCachePutSpanIsRecordedForManyOperation(): void
111124
{
112125
$this->markSkippedIfTracingEventsNotAvailable();
113126

0 commit comments

Comments
 (0)