|
| 1 | +import { expect, test } from '@playwright/test'; |
| 2 | +import { waitForTransaction } from '@sentry-internal/test-utils'; |
| 3 | +import { SEMANTIC_ATTRIBUTE_CACHE_HIT, SEMANTIC_ATTRIBUTE_CACHE_KEY } from '@sentry/core'; |
| 4 | +import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/nuxt'; |
| 5 | + |
| 6 | +test.describe('Storage Instrumentation - Aliases', () => { |
| 7 | + const prefixKey = (key: string) => `test-storage:${key}`; |
| 8 | + |
| 9 | + test('instruments storage alias methods (get, set, has, del, remove) and creates spans', async ({ request }) => { |
| 10 | + const transactionPromise = waitForTransaction('nuxt-3', transactionEvent => { |
| 11 | + return transactionEvent.transaction?.includes('GET /api/storage-aliases-test') ?? false; |
| 12 | + }); |
| 13 | + |
| 14 | + const response = await request.get('/api/storage-aliases-test'); |
| 15 | + expect(response.status()).toBe(200); |
| 16 | + |
| 17 | + const transaction = await transactionPromise; |
| 18 | + |
| 19 | + // Helper to find spans by operation |
| 20 | + const findSpansByOp = (op: string) => { |
| 21 | + return transaction.spans?.filter(span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_OP] === op) || []; |
| 22 | + }; |
| 23 | + |
| 24 | + // Test set (alias for setItem) |
| 25 | + const setSpans = findSpansByOp('cache.set_item'); |
| 26 | + expect(setSpans.length).toBeGreaterThanOrEqual(1); |
| 27 | + const setSpan = setSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user')); |
| 28 | + expect(setSpan).toBeDefined(); |
| 29 | + expect(setSpan?.data).toMatchObject({ |
| 30 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.set_item', |
| 31 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 32 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'), |
| 33 | + 'nuxt.storage.op': 'setItem', |
| 34 | + 'nuxt.storage.mount': 'test-storage:', |
| 35 | + 'nuxt.storage.driver': 'memory', |
| 36 | + }); |
| 37 | + expect(setSpan?.description).toBe(prefixKey('alias:user')); |
| 38 | + |
| 39 | + // Test get (alias for getItem) |
| 40 | + const getSpans = findSpansByOp('cache.get_item'); |
| 41 | + expect(getSpans.length).toBeGreaterThanOrEqual(1); |
| 42 | + const getSpan = getSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user')); |
| 43 | + expect(getSpan).toBeDefined(); |
| 44 | + expect(getSpan?.data).toMatchObject({ |
| 45 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item', |
| 46 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 47 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'), |
| 48 | + [SEMANTIC_ATTRIBUTE_CACHE_HIT]: true, |
| 49 | + 'nuxt.storage.op': 'getItem', |
| 50 | + 'nuxt.storage.mount': 'test-storage:', |
| 51 | + 'nuxt.storage.driver': 'memory', |
| 52 | + }); |
| 53 | + expect(getSpan?.description).toBe(prefixKey('alias:user')); |
| 54 | + |
| 55 | + // Test has (alias for hasItem) |
| 56 | + const hasSpans = findSpansByOp('cache.has_item'); |
| 57 | + expect(hasSpans.length).toBeGreaterThanOrEqual(1); |
| 58 | + const hasSpan = hasSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:user')); |
| 59 | + expect(hasSpan).toBeDefined(); |
| 60 | + expect(hasSpan?.data).toMatchObject({ |
| 61 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.has_item', |
| 62 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 63 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:user'), |
| 64 | + [SEMANTIC_ATTRIBUTE_CACHE_HIT]: true, |
| 65 | + 'nuxt.storage.op': 'hasItem', |
| 66 | + 'nuxt.storage.mount': 'test-storage:', |
| 67 | + 'nuxt.storage.driver': 'memory', |
| 68 | + }); |
| 69 | + |
| 70 | + // Test del and remove (both aliases for removeItem) |
| 71 | + const removeSpans = findSpansByOp('cache.remove_item'); |
| 72 | + expect(removeSpans.length).toBeGreaterThanOrEqual(2); // Should have both del and remove calls |
| 73 | + |
| 74 | + const delSpan = removeSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:temp1')); |
| 75 | + expect(delSpan).toBeDefined(); |
| 76 | + expect(delSpan?.data).toMatchObject({ |
| 77 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.remove_item', |
| 78 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 79 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:temp1'), |
| 80 | + 'nuxt.storage.op': 'removeItem', |
| 81 | + 'nuxt.storage.mount': 'test-storage:', |
| 82 | + 'nuxt.storage.driver': 'memory', |
| 83 | + }); |
| 84 | + expect(delSpan?.description).toBe(prefixKey('alias:temp1')); |
| 85 | + |
| 86 | + const removeSpan = removeSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('alias:temp2')); |
| 87 | + expect(removeSpan).toBeDefined(); |
| 88 | + expect(removeSpan?.data).toMatchObject({ |
| 89 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.remove_item', |
| 90 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 91 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('alias:temp2'), |
| 92 | + 'nuxt.storage.op': 'removeItem', |
| 93 | + 'nuxt.storage.mount': 'test-storage:', |
| 94 | + 'nuxt.storage.driver': 'memory', |
| 95 | + }); |
| 96 | + expect(removeSpan?.description).toBe(prefixKey('alias:temp2')); |
| 97 | + |
| 98 | + // Verify all spans have OK status |
| 99 | + const allStorageSpans = transaction.spans?.filter( |
| 100 | + span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.cache.nuxt', |
| 101 | + ); |
| 102 | + expect(allStorageSpans?.length).toBeGreaterThan(0); |
| 103 | + allStorageSpans?.forEach(span => { |
| 104 | + expect(span.status).toBe('ok'); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments