|
| 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', () => { |
| 7 | + const prefixKey = (key: string) => `test-storage:${key}`; |
| 8 | + |
| 9 | + test('instruments all storage operations and creates spans with correct attributes', async ({ request }) => { |
| 10 | + const transactionPromise = waitForTransaction('nuxt-3', transactionEvent => { |
| 11 | + return transactionEvent.transaction?.includes('GET /api/storage-test') ?? false; |
| 12 | + }); |
| 13 | + |
| 14 | + const response = await request.get('/api/storage-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 setItem spans |
| 25 | + const setItemSpans = findSpansByOp('cache.set_item'); |
| 26 | + expect(setItemSpans.length).toBeGreaterThanOrEqual(1); |
| 27 | + const setItemSpan = setItemSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('user:123')); |
| 28 | + expect(setItemSpan).toBeDefined(); |
| 29 | + expect(setItemSpan?.data).toMatchObject({ |
| 30 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.set_item', |
| 31 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 32 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('user:123'), |
| 33 | + 'nuxt.storage.op': 'setItem', |
| 34 | + 'nuxt.storage.mount': 'test-storage:', |
| 35 | + 'nuxt.storage.driver': 'memory', |
| 36 | + }); |
| 37 | + expect(setItemSpan?.description).toBe(prefixKey('user:123')); |
| 38 | + |
| 39 | + // Test setItemRaw spans |
| 40 | + const setItemRawSpans = findSpansByOp('cache.set_item_raw'); |
| 41 | + expect(setItemRawSpans.length).toBeGreaterThanOrEqual(1); |
| 42 | + const setItemRawSpan = setItemRawSpans.find( |
| 43 | + span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('raw:data'), |
| 44 | + ); |
| 45 | + expect(setItemRawSpan).toBeDefined(); |
| 46 | + expect(setItemRawSpan?.data).toMatchObject({ |
| 47 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.set_item_raw', |
| 48 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 49 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('raw:data'), |
| 50 | + 'nuxt.storage.op': 'setItemRaw', |
| 51 | + 'nuxt.storage.mount': 'test-storage:', |
| 52 | + 'nuxt.storage.driver': 'memory', |
| 53 | + }); |
| 54 | + |
| 55 | + // Test hasItem spans - should have cache hit attribute |
| 56 | + const hasItemSpans = findSpansByOp('cache.has_item'); |
| 57 | + expect(hasItemSpans.length).toBeGreaterThanOrEqual(1); |
| 58 | + const hasItemSpan = hasItemSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('user:123')); |
| 59 | + expect(hasItemSpan).toBeDefined(); |
| 60 | + expect(hasItemSpan?.data).toMatchObject({ |
| 61 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.has_item', |
| 62 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 63 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('user:123'), |
| 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 getItem spans - should have cache hit attribute |
| 71 | + const getItemSpans = findSpansByOp('cache.get_item'); |
| 72 | + expect(getItemSpans.length).toBeGreaterThanOrEqual(1); |
| 73 | + const getItemSpan = getItemSpans.find(span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('user:123')); |
| 74 | + expect(getItemSpan).toBeDefined(); |
| 75 | + expect(getItemSpan?.data).toMatchObject({ |
| 76 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item', |
| 77 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 78 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('user:123'), |
| 79 | + [SEMANTIC_ATTRIBUTE_CACHE_HIT]: true, |
| 80 | + 'nuxt.storage.op': 'getItem', |
| 81 | + 'nuxt.storage.mount': 'test-storage:', |
| 82 | + 'nuxt.storage.driver': 'memory', |
| 83 | + }); |
| 84 | + expect(getItemSpan?.description).toBe(prefixKey('user:123')); |
| 85 | + |
| 86 | + // Test getItemRaw spans - should have cache hit attribute |
| 87 | + const getItemRawSpans = findSpansByOp('cache.get_item_raw'); |
| 88 | + expect(getItemRawSpans.length).toBeGreaterThanOrEqual(1); |
| 89 | + const getItemRawSpan = getItemRawSpans.find( |
| 90 | + span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('raw:data'), |
| 91 | + ); |
| 92 | + expect(getItemRawSpan).toBeDefined(); |
| 93 | + expect(getItemRawSpan?.data).toMatchObject({ |
| 94 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_item_raw', |
| 95 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 96 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('raw:data'), |
| 97 | + [SEMANTIC_ATTRIBUTE_CACHE_HIT]: true, |
| 98 | + 'nuxt.storage.op': 'getItemRaw', |
| 99 | + 'nuxt.storage.mount': 'test-storage:', |
| 100 | + 'nuxt.storage.driver': 'memory', |
| 101 | + }); |
| 102 | + |
| 103 | + // Test getKeys spans |
| 104 | + const getKeysSpans = findSpansByOp('cache.get_keys'); |
| 105 | + expect(getKeysSpans.length).toBeGreaterThanOrEqual(1); |
| 106 | + expect(getKeysSpans[0]?.data).toMatchObject({ |
| 107 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.get_keys', |
| 108 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 109 | + 'nuxt.storage.op': 'getKeys', |
| 110 | + 'nuxt.storage.mount': 'test-storage:', |
| 111 | + 'nuxt.storage.driver': 'memory', |
| 112 | + }); |
| 113 | + |
| 114 | + // Test removeItem spans |
| 115 | + const removeItemSpans = findSpansByOp('cache.remove_item'); |
| 116 | + expect(removeItemSpans.length).toBeGreaterThanOrEqual(1); |
| 117 | + const removeItemSpan = removeItemSpans.find( |
| 118 | + span => span.data?.[SEMANTIC_ATTRIBUTE_CACHE_KEY] === prefixKey('batch:1'), |
| 119 | + ); |
| 120 | + expect(removeItemSpan).toBeDefined(); |
| 121 | + expect(removeItemSpan?.data).toMatchObject({ |
| 122 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.remove_item', |
| 123 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 124 | + [SEMANTIC_ATTRIBUTE_CACHE_KEY]: prefixKey('batch:1'), |
| 125 | + 'nuxt.storage.op': 'removeItem', |
| 126 | + 'nuxt.storage.mount': 'test-storage:', |
| 127 | + 'nuxt.storage.driver': 'memory', |
| 128 | + }); |
| 129 | + |
| 130 | + // Test clear spans |
| 131 | + const clearSpans = findSpansByOp('cache.clear'); |
| 132 | + expect(clearSpans.length).toBeGreaterThanOrEqual(1); |
| 133 | + expect(clearSpans[0]?.data).toMatchObject({ |
| 134 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'cache.clear', |
| 135 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.cache.nuxt', |
| 136 | + 'nuxt.storage.op': 'clear', |
| 137 | + 'nuxt.storage.mount': 'test-storage:', |
| 138 | + 'nuxt.storage.driver': 'memory', |
| 139 | + }); |
| 140 | + |
| 141 | + // Verify all spans have OK status |
| 142 | + const allStorageSpans = transaction.spans?.filter( |
| 143 | + span => span.data?.[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] === 'auto.cache.nuxt', |
| 144 | + ); |
| 145 | + expect(allStorageSpans?.length).toBeGreaterThan(0); |
| 146 | + allStorageSpans?.forEach(span => { |
| 147 | + expect(span.status).toBe('ok'); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments