@@ -34,6 +34,11 @@ trait TraceableCacheAdapterTrait
3434 */
3535 private $ decoratedAdapter ;
3636
37+ /**
38+ * @var string|null
39+ */
40+ protected $ namespace ;
41+
3742 /**
3843 * {@inheritdoc}
3944 */
@@ -200,16 +205,23 @@ private function traceFunction(string $spanOperation, \Closure $callback, ?strin
200205 try {
201206 $ result = $ callback ();
202207
203- // Necessary for static analysis. Otherwise, the TResult type is assumed to be CacheItemInterface.
204- if (!$ result instanceof CacheItemInterface) {
205- return $ result ;
208+ $ data = [];
209+
210+ if ($ result instanceof CacheItemInterface) {
211+ $ data ['cache.hit ' ] = $ result ->isHit ();
212+ if ($ result ->isHit ()) {
213+ $ data ['cache.item_size ' ] = static ::getCacheItemSize ($ result ->get ());
214+ }
215+ }
216+
217+ $ namespace = $ this ->getCacheNamespace ();
218+ if (null !== $ namespace ) {
219+ $ data ['cache.namespace ' ] = $ namespace ;
206220 }
207221
208- $ data = ['cache.hit ' => $ result ->isHit ()];
209- if ($ result ->isHit ()) {
210- $ data ['cache.item_size ' ] = static ::getCacheItemSize ($ result ->get ());
222+ if ([] !== $ data ) {
223+ $ span ->setData ($ data );
211224 }
212- $ span ->setData ($ data );
213225
214226 return $ result ;
215227 } finally {
@@ -282,10 +294,15 @@ private function traceGet(string $key, callable $callback, ?float $beta = null,
282294
283295 $ now = microtime (true );
284296
285- $ getSpan -> setData ( [
297+ $ getData = [
286298 'cache.hit ' => !$ wasMiss ,
287299 'cache.item_size ' => self ::getCacheItemSize ($ value ),
288- ]);
300+ ];
301+ $ namespace = $ this ->getCacheNamespace ();
302+ if (null !== $ namespace ) {
303+ $ getData ['cache.namespace ' ] = $ namespace ;
304+ }
305+ $ getSpan ->setData ($ getData );
289306
290307 // If we got a timestamp here we know that we missed
291308 if (null !== $ saveStartTimestamp ) {
@@ -296,9 +313,13 @@ private function traceGet(string $key, callable $callback, ?float $beta = null,
296313 ->setDescription (urldecode ($ key ));
297314 $ saveSpan = $ parentSpan ->startChild ($ saveContext );
298315 $ saveSpan ->setStartTimestamp ($ saveStartTimestamp );
299- $ saveSpan -> setData ( [
316+ $ saveData = [
300317 'cache.item_size ' => self ::getCacheItemSize ($ value ),
301- ]);
318+ ];
319+ if (null !== $ namespace ) {
320+ $ saveData ['cache.namespace ' ] = $ namespace ;
321+ }
322+ $ saveSpan ->setData ($ saveData );
302323 $ saveSpan ->finish ($ now );
303324 } else {
304325 $ getSpan ->finish ();
@@ -343,4 +364,12 @@ private function setCallbackWrapper(callable $callback, string $key): callable
343364 return $ callback ($ this ->decoratedAdapter ->getItem ($ key ));
344365 };
345366 }
367+
368+ /**
369+ * @return string|null
370+ */
371+ protected function getCacheNamespace (): ?string
372+ {
373+ return $ this ->namespace ;
374+ }
346375}
0 commit comments