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

Normalize array of key names before converting to string #923

Merged
merged 2 commits into from
Jul 17, 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
34 changes: 28 additions & 6 deletions src/Sentry/Laravel/Features/CacheIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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<array-key, mixed> $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();
}
}
15 changes: 14 additions & 1 deletion test/Sentry/Features/CacheIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand Down