Skip to content

Commit c9d2b2c

Browse files
authored
feat: Set cache keys as span descriptions (#677)
1 parent 6a1ad81 commit c9d2b2c

6 files changed

+19
-10
lines changed

src/Tracing/Cache/TraceableCacheAdapterForV2.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ public function get(string $key, callable $callback, float $beta = null, array &
4848
}
4949

5050
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
51-
});
51+
}, $key);
5252
}
5353
}

src/Tracing/Cache/TraceableCacheAdapterForV3.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public function get(string $key, callable $callback, float $beta = null, array &
4646
}
4747

4848
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
49-
});
49+
}, $key);
5050
}
5151
}

src/Tracing/Cache/TraceableCacheAdapterTrait.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function getItem($key): CacheItem
4040
{
4141
return $this->traceFunction('cache.get_item', function () use ($key): CacheItem {
4242
return $this->decoratedAdapter->getItem($key);
43-
});
43+
}, $key);
4444
}
4545

4646
/**
@@ -60,7 +60,7 @@ public function clear(string $prefix = ''): bool
6060
{
6161
return $this->traceFunction('cache.clear', function () use ($prefix): bool {
6262
return $this->decoratedAdapter->clear($prefix);
63-
});
63+
}, $prefix);
6464
}
6565

6666
/**
@@ -74,7 +74,7 @@ public function delete(string $key): bool
7474
}
7575

7676
return $this->decoratedAdapter->delete($key);
77-
});
77+
}, $key);
7878
}
7979

8080
/**
@@ -84,7 +84,7 @@ public function hasItem($key): bool
8484
{
8585
return $this->traceFunction('cache.has_item', function () use ($key): bool {
8686
return $this->decoratedAdapter->hasItem($key);
87-
});
87+
}, $key);
8888
}
8989

9090
/**
@@ -94,7 +94,7 @@ public function deleteItem($key): bool
9494
{
9595
return $this->traceFunction('cache.delete_item', function () use ($key): bool {
9696
return $this->decoratedAdapter->deleteItem($key);
97-
});
97+
}, $key);
9898
}
9999

100100
/**
@@ -168,13 +168,16 @@ public function reset(): void
168168
*
169169
* @phpstan-return TResult
170170
*/
171-
private function traceFunction(string $spanOperation, \Closure $callback)
171+
private function traceFunction(string $spanOperation, \Closure $callback, string $spanDescription = null)
172172
{
173173
$span = $this->hub->getSpan();
174174

175175
if (null !== $span) {
176176
$spanContext = new SpanContext();
177177
$spanContext->setOp($spanOperation);
178+
if (null !== $spanDescription) {
179+
$spanContext->setDescription(urldecode($spanDescription));
180+
}
178181

179182
$span = $span->startChild($spanContext);
180183
}

src/Tracing/Cache/TraceableTagAwareCacheAdapterForV2.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
4949
}
5050

5151
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
52-
});
52+
}, $key);
5353
}
5454

5555
/**

src/Tracing/Cache/TraceableTagAwareCacheAdapterForV3.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
4747
}
4848

4949
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
50-
});
50+
}, $key);
5151
}
5252

5353
/**

tests/Tracing/Cache/AbstractTraceableCacheAdapterTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testGetItem(): void
6464

6565
$this->assertCount(2, $spans);
6666
$this->assertSame('cache.get_item', $spans[1]->getOp());
67+
$this->assertSame('foo', $spans[1]->getDescription());
6768
$this->assertNotNull($spans[1]->getEndTimestamp());
6869
}
6970

@@ -119,6 +120,7 @@ public function testClear(): void
119120

120121
$this->assertCount(2, $spans);
121122
$this->assertSame('cache.clear', $spans[1]->getOp());
123+
$this->assertSame('foo', $spans[1]->getDescription());
122124
$this->assertNotNull($spans[1]->getEndTimestamp());
123125
}
124126

@@ -148,6 +150,7 @@ public function testGet(): void
148150

149151
$this->assertCount(2, $spans);
150152
$this->assertSame('cache.get_item', $spans[1]->getOp());
153+
$this->assertSame('foo', $spans[1]->getDescription());
151154
$this->assertNotNull($spans[1]->getEndTimestamp());
152155
}
153156

@@ -185,6 +188,7 @@ public function testDelete(): void
185188

186189
$this->assertCount(2, $spans);
187190
$this->assertSame('cache.delete_item', $spans[1]->getOp());
191+
$this->assertSame('foo', $spans[1]->getDescription());
188192
$this->assertNotNull($spans[1]->getEndTimestamp());
189193
}
190194

@@ -222,6 +226,7 @@ public function testHasItem(): void
222226

223227
$this->assertCount(2, $spans);
224228
$this->assertSame('cache.has_item', $spans[1]->getOp());
229+
$this->assertSame('foo', $spans[1]->getDescription());
225230
$this->assertNotNull($spans[1]->getEndTimestamp());
226231
}
227232

@@ -249,6 +254,7 @@ public function testDeleteItem(): void
249254

250255
$this->assertCount(2, $spans);
251256
$this->assertSame('cache.delete_item', $spans[1]->getOp());
257+
$this->assertSame('foo', $spans[1]->getDescription());
252258
$this->assertNotNull($spans[1]->getEndTimestamp());
253259
}
254260

0 commit comments

Comments
 (0)