Skip to content

Commit 92ea118

Browse files
committed
fix(nitro): Address review feedback for unstorage instrumentation
- hasItem: use boolean result directly instead of generic isCacheHit - isCacheHit: handle both string and already-deserialized cache values - cache tests: replace silent if-guards with hard assertions
1 parent cae896c commit 92ea118

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

dev-packages/e2e-tests/test-applications/nitro-3/tests/cache.test.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ test.describe('Cache Instrumentation', () => {
3838
span.data[SEMANTIC_ATTRIBUTE_CACHE_KEY].includes('user:123') &&
3939
!span.data?.[SEMANTIC_ATTRIBUTE_CACHE_HIT],
4040
);
41-
if (cacheMissSpan) {
42-
expect(cacheMissSpan.data).toMatchObject({
43-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item',
44-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nitro',
45-
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: false,
46-
'db.operation.name': 'getItem',
47-
});
48-
}
41+
expect(cacheMissSpan).toBeDefined();
42+
expect(cacheMissSpan?.data).toMatchObject({
43+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item',
44+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nitro',
45+
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: false,
46+
'db.operation.name': 'getItem',
47+
});
4948

5049
// Find cache hit (second call to getCachedUser('123'))
5150
const cacheHitSpan = getItemSpans.find(
@@ -54,14 +53,13 @@ test.describe('Cache Instrumentation', () => {
5453
span.data[SEMANTIC_ATTRIBUTE_CACHE_KEY].includes('user:123') &&
5554
span.data?.[SEMANTIC_ATTRIBUTE_CACHE_HIT],
5655
);
57-
if (cacheHitSpan) {
58-
expect(cacheHitSpan.data).toMatchObject({
59-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item',
60-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nitro',
61-
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: true,
62-
'db.operation.name': 'getItem',
63-
});
64-
}
56+
expect(cacheHitSpan).toBeDefined();
57+
expect(cacheHitSpan?.data).toMatchObject({
58+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item',
59+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nitro',
60+
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: true,
61+
'db.operation.name': 'getItem',
62+
});
6563

6664
// setItem spans for cachedFunction - when cache miss occurs, value is set
6765
const setItemSpans = findSpansByOp('cache.set_item');
@@ -72,13 +70,12 @@ test.describe('Cache Instrumentation', () => {
7270
typeof span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === 'string' &&
7371
span.data[SEMANTIC_ATTRIBUTE_CACHE_KEY].includes('user:123'),
7472
);
75-
if (cacheSetSpan) {
76-
expect(cacheSetSpan.data).toMatchObject({
77-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.set_item',
78-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nitro',
79-
'db.operation.name': 'setItem',
80-
});
81-
}
73+
expect(cacheSetSpan).toBeDefined();
74+
expect(cacheSetSpan?.data).toMatchObject({
75+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.set_item',
76+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nitro',
77+
'db.operation.name': 'setItem',
78+
});
8279

8380
// Spans for different cached functions
8481
const dataKeySpans = getItemSpans.filter(

packages/nitro/src/runtime/hooks/captureStorageEvents.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ function setupStorageTracingChannel(operation: TracedOperation): void {
7979
channel.subscribe({
8080
asyncEnd(data: TracingChannelContextWithSpan<TraceContext & { result?: unknown }>) {
8181
if (data._sentrySpan && CACHE_HIT_OPERATIONS.has(operation)) {
82-
data._sentrySpan.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_HIT, isCacheHit(data.keys?.[0], data.result));
82+
const hit =
83+
operation === 'hasItem' ? Boolean(data.result) : isCacheHit(data.keys?.[0], data.result);
84+
data._sentrySpan.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_HIT, hit);
8385
}
8486

8587
data._sentrySpan?.setStatus({ code: SPAN_STATUS_OK });
@@ -126,7 +128,9 @@ function isCacheHit(key: unknown, value: unknown): boolean {
126128
return !isEmpty;
127129
}
128130

129-
return validateCacheEntry(key, JSON.parse(String(value)) as CacheEntry);
131+
const entry = typeof value === 'string' ? (JSON.parse(value) as CacheEntry) : (value as CacheEntry);
132+
133+
return validateCacheEntry(key, entry);
130134
} catch {
131135
return false;
132136
}

0 commit comments

Comments
 (0)