88import {
99 flushIfServerless ,
1010 GLOBAL_OBJ ,
11+ isObjectLike ,
1112 SEMANTIC_ATTRIBUTE_CACHE_HIT ,
1213 SEMANTIC_ATTRIBUTE_CACHE_KEY ,
1314 SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
@@ -37,7 +38,7 @@ const TRACED_OPERATIONS = [
3738
3839type TracedOperation = ( typeof TRACED_OPERATIONS ) [ number ] ;
3940
40- const CACHE_HIT_OPERATIONS = new Set < TracedOperation > ( [ 'hasItem' , 'getItem' , 'getItemRaw' ] ) ;
41+ const CACHE_HIT_OPERATIONS = new Set < TracedOperation > ( [ 'hasItem' , 'getItem' , 'getItemRaw' , 'getItems' ] ) ;
4142
4243/**
4344 * Maps each unstorage operation to a convention cache op. Reads (including existence and key
@@ -107,8 +108,7 @@ function setupStorageTracingChannel(operation: TracedOperation): void {
107108 if ( ! ( 'error' in data ) ) {
108109 const result = ( data as { result ?: unknown } ) . result ;
109110 if ( CACHE_HIT_OPERATIONS . has ( operation ) ) {
110- const hit = operation === 'hasItem' ? Boolean ( result ) : isCacheHit ( data . keys ?. [ 0 ] , result ) ;
111- span . setAttribute ( SEMANTIC_ATTRIBUTE_CACHE_HIT , hit ) ;
111+ span . setAttribute ( SEMANTIC_ATTRIBUTE_CACHE_HIT , resolveCacheHit ( operation , data . keys ?. [ 0 ] , result ) ) ;
112112 }
113113 }
114114
@@ -118,6 +118,23 @@ function setupStorageTracingChannel(operation: TracedOperation): void {
118118 ) ;
119119}
120120
121+ /**
122+ * Resolves the `cache.hit` value for a read operation. `hasItem` returns a boolean directly,
123+ * `getItems` returns a `{ key, value }[]` where a hit means at least one entry has a value,
124+ * and single-key reads fall back to the value-based `isCacheHit` check.
125+ */
126+ function resolveCacheHit ( operation : TracedOperation , key : unknown , result : unknown ) : boolean {
127+ if ( operation === 'hasItem' ) {
128+ return Boolean ( result ) ;
129+ }
130+
131+ if ( operation === 'getItems' ) {
132+ return Array . isArray ( result ) && result . some ( item => isObjectLike ( item ) && item . value != null ) ;
133+ }
134+
135+ return isCacheHit ( key , result ) ;
136+ }
137+
121138interface CacheEntry < T = unknown > {
122139 value ?: T ;
123140 expires ?: number ;
0 commit comments