-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nuxt): Instrument server cache API #17886
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
Open
logaretm
wants to merge
2
commits into
awad/js-1016-nuxtnitro-instrument-kv-storage-instrumenting-unstorage
Choose a base branch
from
awad/js-1023-nuxtnitro-instrument-cache
base: awad/js-1016-nuxtnitro-instrument-kv-storage-instrumenting-unstorage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+498
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
dev-packages/e2e-tests/test-applications/nuxt-3/server/api/cache-test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { cachedFunction, defineCachedEventHandler, defineEventHandler, getQuery } from '#imports'; | ||
|
||
// Test cachedFunction | ||
const getCachedUser = cachedFunction( | ||
async (userId: string) => { | ||
return { | ||
id: userId, | ||
name: `User ${userId}`, | ||
email: `user${userId}@example.com`, | ||
timestamp: Date.now(), | ||
}; | ||
}, | ||
{ | ||
maxAge: 60, | ||
name: 'getCachedUser', | ||
getKey: (userId: string) => `user:${userId}`, | ||
}, | ||
); | ||
|
||
// Test cachedFunction with different options | ||
const getCachedData = cachedFunction( | ||
async (key: string) => { | ||
return { | ||
key, | ||
value: `cached-value-${key}`, | ||
timestamp: Date.now(), | ||
}; | ||
}, | ||
{ | ||
maxAge: 120, | ||
name: 'getCachedData', | ||
getKey: (key: string) => `data:${key}`, | ||
}, | ||
); | ||
|
||
// Test defineCachedEventHandler | ||
const cachedHandler = defineCachedEventHandler( | ||
async event => { | ||
return { | ||
message: 'This response is cached', | ||
timestamp: Date.now(), | ||
path: event.path, | ||
}; | ||
}, | ||
{ | ||
maxAge: 60, | ||
name: 'cachedHandler', | ||
}, | ||
); | ||
|
||
export default defineEventHandler(async event => { | ||
const results: Record<string, unknown> = {}; | ||
const testKey = String(getQuery(event).user ?? ''); | ||
const dataKey = String(getQuery(event).data ?? ''); | ||
|
||
// Test cachedFunction - first call (cache miss) | ||
const user1 = await getCachedUser(testKey); | ||
results.cachedUser1 = user1; | ||
|
||
// Test cachedFunction - second call (cache hit) | ||
const user2 = await getCachedUser(testKey); | ||
results.cachedUser2 = user2; | ||
|
||
// Test cachedFunction with different key (cache miss) | ||
const user3 = await getCachedUser(`${testKey}456`); | ||
results.cachedUser3 = user3; | ||
|
||
// Test another cachedFunction | ||
const data1 = await getCachedData(dataKey); | ||
results.cachedData1 = data1; | ||
|
||
// Test cachedFunction - cache hit | ||
const data2 = await getCachedData(dataKey); | ||
results.cachedData2 = data2; | ||
|
||
// Test cachedEventHandler by calling it | ||
const cachedResponse = await cachedHandler(event); | ||
results.cachedResponse = cachedResponse; | ||
|
||
return { | ||
success: true, | ||
results, | ||
}; | ||
}); |
161 changes: 161 additions & 0 deletions
161
dev-packages/e2e-tests/test-applications/nuxt-3/tests/cache.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/nuxt'; | ||
|
||
test.describe('Cache Instrumentation', () => { | ||
const SEMANTIC_ATTRIBUTE_CACHE_KEY = 'cache.key'; | ||
const SEMANTIC_ATTRIBUTE_CACHE_HIT = 'cache.hit'; | ||
|
||
test('instruments cachedFunction and cachedEventHandler calls and creates spans with correct attributes', async ({ | ||
request, | ||
}) => { | ||
const transactionPromise = waitForTransaction('nuxt-3', transactionEvent => { | ||
return transactionEvent.transaction?.includes('GET /api/cache-test') ?? false; | ||
}); | ||
|
||
const response = await request.get('/api/cache-test?user=123&data=test-key'); | ||
expect(response.status()).toBe(200); | ||
|
||
const transaction = await transactionPromise; | ||
|
||
// Helper to find spans by operation | ||
const findSpansByOp = (op: string) => { | ||
return transaction.spans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === op) || []; | ||
}; | ||
|
||
// Test that we have cache operations from cachedFunction and cachedEventHandler | ||
const allCacheSpans = transaction.spans?.filter( | ||
span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.cache.nuxt', | ||
); | ||
expect(allCacheSpans?.length).toBeGreaterThan(0); | ||
|
||
// Test getItem spans for cachedFunction - should have both cache miss and cache hit | ||
const getItemSpans = findSpansByOp('cache.get_item'); | ||
expect(getItemSpans.length).toBeGreaterThan(0); | ||
|
||
// Find cache miss (first call to getCachedUser('123')) | ||
const cacheMissSpan = getItemSpans.find( | ||
span => | ||
typeof span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === 'string' && | ||
span.data[SEMANTIC_ATTRIBUTE_CACHE_KEY].includes('user:123') && | ||
!span.data?.[SEMANTIC_ATTRIBUTE_CACHE_HIT], | ||
); | ||
if (cacheMissSpan) { | ||
expect(cacheMissSpan.data).toMatchObject({ | ||
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item', | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', | ||
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: false, | ||
'nuxt.storage.op': 'getItem', | ||
'nuxt.storage.mount': expect.stringMatching(/^(cache:)?$/), | ||
}); | ||
} | ||
|
||
// Find cache hit (second call to getCachedUser('123')) | ||
const cacheHitSpan = getItemSpans.find( | ||
span => | ||
typeof span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === 'string' && | ||
span.data[SEMANTIC_ATTRIBUTE_CACHE_KEY].includes('user:123') && | ||
span.data?.[SEMANTIC_ATTRIBUTE_CACHE_HIT], | ||
); | ||
if (cacheHitSpan) { | ||
expect(cacheHitSpan.data).toMatchObject({ | ||
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item', | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', | ||
[SEMANTIC_ATTRIBUTE_CACHE_HIT]: true, | ||
'nuxt.storage.op': 'getItem', | ||
'nuxt.storage.mount': expect.stringMatching(/^(cache:)?$/), | ||
}); | ||
} | ||
|
||
// Test setItem spans for cachedFunction - when cache miss occurs, value is set | ||
const setItemSpans = findSpansByOp('cache.set_item'); | ||
expect(setItemSpans.length).toBeGreaterThan(0); | ||
|
||
const cacheSetSpan = setItemSpans.find( | ||
span => | ||
typeof span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === 'string' && | ||
span.data[SEMANTIC_ATTRIBUTE_CACHE_KEY].includes('user:123'), | ||
); | ||
if (cacheSetSpan) { | ||
expect(cacheSetSpan.data).toMatchObject({ | ||
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.set_item', | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', | ||
'nuxt.storage.op': 'setItem', | ||
'nuxt.storage.mount': expect.stringMatching(/^(cache:)?$/), | ||
}); | ||
} | ||
|
||
// Test that we have spans for different cached functions | ||
const dataKeySpans = getItemSpans.filter( | ||
span => | ||
typeof span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === 'string' && | ||
span.data[SEMANTIC_ATTRIBUTE_CACHE_KEY].includes('data:test-key'), | ||
); | ||
expect(dataKeySpans.length).toBeGreaterThan(0); | ||
|
||
// Test that we have spans for cachedEventHandler | ||
const cachedHandlerSpans = getItemSpans.filter( | ||
span => | ||
typeof span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === 'string' && | ||
span.data[SEMANTIC_ATTRIBUTE_CACHE_KEY].includes('cachedHandler'), | ||
); | ||
expect(cachedHandlerSpans.length).toBeGreaterThan(0); | ||
|
||
// Verify all cache spans have OK status | ||
allCacheSpans?.forEach(span => { | ||
expect(span.status).toBe('ok'); | ||
}); | ||
|
||
// Verify cache spans are properly nested under the transaction | ||
allCacheSpans?.forEach(span => { | ||
expect(span.parent_span_id).toBeDefined(); | ||
}); | ||
}); | ||
|
||
test('correctly tracks cache hits and misses for cachedFunction', async ({ request }) => { | ||
// Use a unique key for this test to ensure fresh cache state | ||
const uniqueUser = `test-${Date.now()}`; | ||
const uniqueData = `data-${Date.now()}`; | ||
|
||
const transactionPromise = waitForTransaction('nuxt-3', transactionEvent => { | ||
return transactionEvent.transaction?.includes('GET /api/cache-test') ?? false; | ||
}); | ||
|
||
await request.get(`/api/cache-test?user=${uniqueUser}&data=${uniqueData}`); | ||
const transaction1 = await transactionPromise; | ||
|
||
// Get all cache-related spans | ||
const allCacheSpans = transaction1.spans?.filter( | ||
span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.cache.nuxt', | ||
); | ||
|
||
// We should have cache operations | ||
expect(allCacheSpans?.length).toBeGreaterThan(0); | ||
|
||
// Get all getItem operations | ||
const allGetItemSpans = allCacheSpans?.filter( | ||
span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'cache.get_item', | ||
); | ||
|
||
// Get all setItem operations | ||
const allSetItemSpans = allCacheSpans?.filter( | ||
span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'cache.set_item', | ||
); | ||
|
||
// We should have both get and set operations | ||
expect(allGetItemSpans?.length).toBeGreaterThan(0); | ||
expect(allSetItemSpans?.length).toBeGreaterThan(0); | ||
|
||
// Check for cache misses (cache.hit = false) | ||
const cacheMissSpans = allGetItemSpans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_HIT] === false); | ||
|
||
// Check for cache hits (cache.hit = true) | ||
const cacheHitSpans = allGetItemSpans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_HIT] === true); | ||
|
||
// We should have at least one cache miss (first calls to getCachedUser and getCachedData) | ||
expect(cacheMissSpans?.length).toBeGreaterThanOrEqual(1); | ||
|
||
// We should have at least one cache hit (second calls to getCachedUser and getCachedData) | ||
expect(cacheHitSpans?.length).toBeGreaterThanOrEqual(1); | ||
}); | ||
}); |
84 changes: 84 additions & 0 deletions
84
dev-packages/e2e-tests/test-applications/nuxt-4/server/api/cache-test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { cachedFunction, defineCachedEventHandler, defineEventHandler, getQuery } from '#imports'; | ||
|
||
// Test cachedFunction | ||
const getCachedUser = cachedFunction( | ||
async (userId: string) => { | ||
return { | ||
id: userId, | ||
name: `User ${userId}`, | ||
email: `user${userId}@example.com`, | ||
timestamp: Date.now(), | ||
}; | ||
}, | ||
{ | ||
maxAge: 60, | ||
name: 'getCachedUser', | ||
getKey: (userId: string) => `user:${userId}`, | ||
}, | ||
); | ||
|
||
// Test cachedFunction with different options | ||
const getCachedData = cachedFunction( | ||
async (key: string) => { | ||
return { | ||
key, | ||
value: `cached-value-${key}`, | ||
timestamp: Date.now(), | ||
}; | ||
}, | ||
{ | ||
maxAge: 120, | ||
name: 'getCachedData', | ||
getKey: (key: string) => `data:${key}`, | ||
}, | ||
); | ||
|
||
// Test defineCachedEventHandler | ||
const cachedHandler = defineCachedEventHandler( | ||
async event => { | ||
return { | ||
message: 'This response is cached', | ||
timestamp: Date.now(), | ||
path: event.path, | ||
}; | ||
}, | ||
{ | ||
maxAge: 60, | ||
name: 'cachedHandler', | ||
}, | ||
); | ||
|
||
export default defineEventHandler(async event => { | ||
const results: Record<string, unknown> = {}; | ||
const testKey = String(getQuery(event).user ?? '123'); | ||
const dataKey = String(getQuery(event).data ?? 'test-key'); | ||
|
||
// Test cachedFunction - first call (cache miss) | ||
const user1 = await getCachedUser(testKey); | ||
results.cachedUser1 = user1; | ||
|
||
// Test cachedFunction - second call (cache hit) | ||
const user2 = await getCachedUser(testKey); | ||
results.cachedUser2 = user2; | ||
|
||
// Test cachedFunction with different key (cache miss) | ||
const user3 = await getCachedUser(`${testKey}456`); | ||
results.cachedUser3 = user3; | ||
|
||
// Test another cachedFunction | ||
const data1 = await getCachedData(dataKey); | ||
results.cachedData1 = data1; | ||
|
||
// Test cachedFunction - cache hit | ||
const data2 = await getCachedData(dataKey); | ||
results.cachedData2 = data2; | ||
|
||
// Test cachedEventHandler by calling it | ||
const cachedResponse = await cachedHandler(event); | ||
results.cachedResponse = cachedResponse; | ||
|
||
return { | ||
success: true, | ||
results, | ||
}; | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.