-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(nitro): Add unstorage tracing channel instrumentation #20615
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
3
commits into
develop
Choose a base branch
from
awad/js-1089-nitro-use-tracing-channels-for-unstorage
base: develop
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.
Open
Changes from 2 commits
Commits
Show all changes
3 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
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
82 changes: 82 additions & 0 deletions
82
dev-packages/e2e-tests/test-applications/nitro-3/server/api/test-cache.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,82 @@ | ||
| import { defineCachedFunction, defineCachedHandler } from 'nitro/cache'; | ||
| import { defineHandler, getQuery } from 'nitro/h3'; | ||
|
|
||
| const getCachedUser = defineCachedFunction( | ||
| 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}`, | ||
| }, | ||
| ); | ||
|
|
||
| const getCachedData = defineCachedFunction( | ||
| async (key: string) => { | ||
| return { | ||
| key, | ||
| value: `cached-value-${key}`, | ||
| timestamp: Date.now(), | ||
| }; | ||
| }, | ||
| { | ||
| maxAge: 120, | ||
| name: 'getCachedData', | ||
| getKey: (key: string) => `data:${key}`, | ||
| }, | ||
| ); | ||
|
|
||
| const cachedHandler = defineCachedHandler( | ||
| async event => { | ||
| return { | ||
| message: 'This response is cached', | ||
| timestamp: Date.now(), | ||
| path: event.path, | ||
| }; | ||
| }, | ||
| { | ||
| maxAge: 60, | ||
| name: 'cachedHandler', | ||
| }, | ||
| ); | ||
|
|
||
| export default defineHandler(async event => { | ||
| const results: Record<string, unknown> = {}; | ||
| const testKey = String(getQuery(event).user ?? '123'); | ||
| const dataKey = String(getQuery(event).data ?? 'test-key'); | ||
|
|
||
| // cachedFunction - first call (cache miss) | ||
| const user1 = await getCachedUser(testKey); | ||
| results.cachedUser1 = user1; | ||
|
|
||
| // cachedFunction - second call (cache hit) | ||
| const user2 = await getCachedUser(testKey); | ||
| results.cachedUser2 = user2; | ||
|
|
||
| // cachedFunction with different key (cache miss) | ||
| const user3 = await getCachedUser(`${testKey}456`); | ||
| results.cachedUser3 = user3; | ||
|
|
||
| // another cachedFunction | ||
| const data1 = await getCachedData(dataKey); | ||
| results.cachedData1 = data1; | ||
|
|
||
| // cachedFunction - cache hit | ||
| const data2 = await getCachedData(dataKey); | ||
| results.cachedData2 = data2; | ||
|
|
||
| // cachedEventHandler | ||
| const cachedResponse = await cachedHandler(event); | ||
| results.cachedResponse = cachedResponse; | ||
|
|
||
| return { | ||
| success: true, | ||
| results, | ||
| }; | ||
| }); |
45 changes: 45 additions & 0 deletions
45
dev-packages/e2e-tests/test-applications/nitro-3/server/api/test-storage-aliases.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,45 @@ | ||
| import { defineHandler } from 'nitro/h3'; | ||
| import { useStorage } from 'nitro/storage'; | ||
|
|
||
| export default defineHandler(async () => { | ||
| const storage = useStorage('cache'); | ||
|
|
||
| const results: Record<string, unknown> = {}; | ||
|
|
||
| // Test set (alias for setItem) | ||
| await storage.set('alias:user', { name: 'Jane Doe', role: 'admin' }); | ||
| results.set = 'success'; | ||
|
|
||
| // Test get (alias for getItem) | ||
| const user = await storage.get('alias:user'); | ||
| results.get = user; | ||
|
|
||
| // Test has (alias for hasItem) | ||
| const hasUser = await storage.has('alias:user'); | ||
| results.has = hasUser; | ||
|
|
||
| // Setup for delete tests | ||
| await storage.set('alias:temp1', 'temp1'); | ||
| await storage.set('alias:temp2', 'temp2'); | ||
|
|
||
| // Test del (alias for removeItem) | ||
| await storage.del('alias:temp1'); | ||
| results.del = 'success'; | ||
|
|
||
| // Test remove (alias for removeItem) | ||
| await storage.remove('alias:temp2'); | ||
| results.remove = 'success'; | ||
|
|
||
| // Verify deletions worked | ||
| const hasTemp1 = await storage.has('alias:temp1'); | ||
| const hasTemp2 = await storage.has('alias:temp2'); | ||
| results.verifyDeletions = !hasTemp1 && !hasTemp2; | ||
|
|
||
| // Clean up | ||
| await storage.clear(); | ||
|
|
||
| return { | ||
| success: true, | ||
| results, | ||
| }; | ||
| }); |
53 changes: 53 additions & 0 deletions
53
dev-packages/e2e-tests/test-applications/nitro-3/server/api/test-storage.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,53 @@ | ||
| import { defineHandler } from 'nitro/h3'; | ||
| import { useStorage } from 'nitro/storage'; | ||
|
|
||
| export default defineHandler(async () => { | ||
| const storage = useStorage('cache'); | ||
|
|
||
| const results: Record<string, unknown> = {}; | ||
|
|
||
| // Test setItem | ||
| await storage.setItem('user:123', { name: 'John Doe', email: '[email protected]' }); | ||
| results.setItem = 'success'; | ||
|
|
||
| // Test setItemRaw | ||
| await storage.setItemRaw('raw:data', Buffer.from('raw data')); | ||
| results.setItemRaw = 'success'; | ||
|
|
||
| // Manually set batch items | ||
| await storage.setItem('batch:1', 'value1'); | ||
| await storage.setItem('batch:2', 'value2'); | ||
|
|
||
| // Test hasItem | ||
| const hasUser = await storage.hasItem('user:123'); | ||
| results.hasItem = hasUser; | ||
|
|
||
| // Test getItem | ||
| const user = await storage.getItem('user:123'); | ||
| results.getItem = user; | ||
|
|
||
| // Test getItemRaw | ||
| const rawData = await storage.getItemRaw('raw:data'); | ||
| results.getItemRaw = rawData?.toString(); | ||
|
|
||
| // Test getKeys | ||
| const keys = await storage.getKeys('batch:'); | ||
| results.getKeys = keys; | ||
|
|
||
| // Test removeItem | ||
| await storage.removeItem('batch:1'); | ||
| results.removeItem = 'success'; | ||
|
|
||
| // Test clear | ||
| await storage.clear(); | ||
| results.clear = 'success'; | ||
|
|
||
| // Verify clear worked | ||
| const keysAfterClear = await storage.getKeys(); | ||
| results.keysAfterClear = keysAfterClear; | ||
|
|
||
| return { | ||
| success: true, | ||
| results, | ||
| }; | ||
| }); |
145 changes: 145 additions & 0 deletions
145
dev-packages/e2e-tests/test-applications/nitro-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,145 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
| import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/nitro'; | ||
|
|
||
| test.describe('Cache Instrumentation', () => { | ||
| const SEMANTIC_ATTRIBUTE_CACHE_KEY = 'cache.key'; | ||
| const SEMANTIC_ATTRIBUTE_CACHE_HIT = 'cache.hit'; | ||
|
|
||
| test('instruments cachedFunction and cachedHandler calls and creates spans with correct attributes', async ({ | ||
| request, | ||
| }) => { | ||
| const transactionPromise = waitForTransaction('nitro-3', transactionEvent => { | ||
| return transactionEvent.transaction?.includes('GET /api/test-cache') ?? false; | ||
| }); | ||
|
|
||
| const response = await request.get('/api/test-cache'); | ||
| expect(response.status()).toBe(200); | ||
|
|
||
| const transaction = await transactionPromise; | ||
|
|
||
| const findSpansByOp = (op: string) => { | ||
| return transaction.spans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === op) || []; | ||
| }; | ||
|
|
||
| const allCacheSpans = transaction.spans?.filter( | ||
| span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.cache.nitro', | ||
| ); | ||
| expect(allCacheSpans?.length).toBeGreaterThan(0); | ||
|
|
||
| // 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.nitro', | ||
| [SEMANTIC_ATTRIBUTE_CACHE_HIT]: false, | ||
| 'db.operation.name': 'getItem', | ||
| }); | ||
| } | ||
|
|
||
| // 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.nitro', | ||
| [SEMANTIC_ATTRIBUTE_CACHE_HIT]: true, | ||
| 'db.operation.name': 'getItem', | ||
| }); | ||
| } | ||
|
|
||
| // 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.nitro', | ||
| 'db.operation.name': 'setItem', | ||
| }); | ||
| } | ||
|
|
||
| // 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); | ||
|
|
||
| // Spans for cachedHandler | ||
| 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 }) => { | ||
| const uniqueUser = `test-${Date.now()}`; | ||
| const uniqueData = `data-${Date.now()}`; | ||
|
|
||
| const transactionPromise = waitForTransaction('nitro-3', transactionEvent => { | ||
| return transactionEvent.transaction?.includes('GET /api/test-cache') ?? false; | ||
| }); | ||
|
|
||
| await request.get(`/api/test-cache?user=${uniqueUser}&data=${uniqueData}`); | ||
| const transaction = await transactionPromise; | ||
|
|
||
| const allCacheSpans = transaction.spans?.filter( | ||
| span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.cache.nitro', | ||
| ); | ||
| expect(allCacheSpans?.length).toBeGreaterThan(0); | ||
|
|
||
| const allGetItemSpans = allCacheSpans?.filter( | ||
| span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'cache.get_item', | ||
| ); | ||
| const allSetItemSpans = allCacheSpans?.filter( | ||
| span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === 'cache.set_item', | ||
| ); | ||
|
|
||
| expect(allGetItemSpans?.length).toBeGreaterThan(0); | ||
| expect(allSetItemSpans?.length).toBeGreaterThan(0); | ||
|
|
||
| const cacheMissSpans = allGetItemSpans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_HIT] === false); | ||
| const cacheHitSpans = allGetItemSpans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_HIT] === true); | ||
|
|
||
| // At least one cache miss (first calls to getCachedUser and getCachedData) | ||
| expect(cacheMissSpans?.length).toBeGreaterThanOrEqual(1); | ||
|
|
||
| // At least one cache hit (second calls to getCachedUser and getCachedData) | ||
| expect(cacheHitSpans?.length).toBeGreaterThanOrEqual(1); | ||
| }); | ||
| }); | ||
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.