|
| 1 | +--- |
| 2 | +title: Instrument Caches |
| 3 | +sidebar_order: 1000 |
| 4 | +description: "Learn how to manually instrument your code to use Sentry's Cache module." |
| 5 | +supported: |
| 6 | + - javascript.node |
| 7 | + - javascript.aws-lambda |
| 8 | + - javascript.azure-functions |
| 9 | + - javascript.connect |
| 10 | + - javascript.express |
| 11 | + - javascript.fastify |
| 12 | + - javascript.gcp-functions |
| 13 | + - javascript.hapi |
| 14 | + - javascript.koa |
| 15 | + - javascript.nestjs |
| 16 | + - javascript.bun |
| 17 | + - javascript.deno |
| 18 | + - javascript.nextjs |
| 19 | + - javascript.nuxt |
| 20 | + - javascript.astro |
| 21 | + - javascript.solidstart |
| 22 | + - javascript.sveltekit |
| 23 | + - javascript.remix |
| 24 | + - javascript.cloudflare |
| 25 | +--- |
| 26 | + |
| 27 | +A cache can be used to speed up data retrieval, thereby improving application performance. Because instead of getting data from a potentially slow data layer, your application will be getting data from memory (in a best case scenario). Caching can speed up read-heavy workloads for applications like Q&A portals, gaming, media sharing, and social networking. |
| 28 | + |
| 29 | +Sentry offers a [cache-monitoring dashboard](https://sentry.io/orgredirect/organizations/:orgslug/insights/backend/caches/) that can be auto-instrumented using Sentry's Redis integration (more coming soon). |
| 30 | + |
| 31 | +## Instrumentation with Redis Clients |
| 32 | + |
| 33 | +If you're using a Redis client like `ioredis` or `redis` to cache your data, it is necessary to specify the `cachePrefixes` within the `redisIntegration` options. This configuration allows Sentry to categorize accesses to keys with the defined prefixes as "cache operations". |
| 34 | + |
| 35 | +```javascript |
| 36 | +Sentry.init({ |
| 37 | + integrations: [ |
| 38 | + redisIntegration({ |
| 39 | + cachePrefixes: ["posts:", "authors:"], |
| 40 | + }), |
| 41 | + ], |
| 42 | +}); |
| 43 | +``` |
| 44 | + |
| 45 | +## Manual Instrumentation |
| 46 | + |
| 47 | +If you're using anything other than Sentry's Redis integration, you'll need to manually instrument the [Cache Module](https://sentry.io/orgredirect/organizations/:orgslug/insights/backend/caches/) by following the steps below. |
| 48 | + |
| 49 | +You'll need to create two spans - one indicating that something is being put into the cache, and a second one indicating that something is being fetched from the cache. |
| 50 | + |
| 51 | +<PlatformSection supported={["javascript.node", "javascript.deno", "javascript.bun"]}> |
| 52 | + |
| 53 | +Make sure that there's an active span before you create your cache spans. If you're using a web framework like Express, a span will be created for you automatically. See <PlatformLink to="/tracing/">Tracing</PlatformLink> for more information. |
| 54 | + |
| 55 | +</PlatformSection> |
| 56 | + |
| 57 | +For detailed information about which data can be set, see the [Cache Module Developer Specification](https://develop.sentry.dev/sdk/performance/modules/caches/). |
| 58 | + |
| 59 | +### Step 1: Add Span When Putting Data Into the Cache |
| 60 | + |
| 61 | +Follow these custom instrumentation instructions to emit cache setting spans: |
| 62 | + |
| 63 | +1. Set the cache value with whatever cache library you happen to be using. |
| 64 | +2. Wrap the part of your application that uses the cached value with `Sentry.startSpan(...)`. |
| 65 | +3. Set the `name` to something descriptive like "Setting auth cache". |
| 66 | +4. Set `op` to `cache.set`. |
| 67 | +5. Set `cache.key` to a string array representing the key(s) you're setting. |
| 68 | +6. Optionally, you can set other attributes such as `cache.item_size`. (See [Cache Module Span Data Conventions](https://develop.sentry.dev/sdk/performance/modules/caches/#span-data) for more information.) |
| 69 | + |
| 70 | +(The steps described above are also documented in the snippet.) |
| 71 | + |
| 72 | +```javascript {filename: my-cache.js} |
| 73 | +const key = "myCacheKey123"; |
| 74 | +const value = "The value I want to cache."; |
| 75 | + |
| 76 | +Sentry.startSpan( |
| 77 | + { |
| 78 | + name: key, |
| 79 | + attributes: { |
| 80 | + "cache.key": [key], |
| 81 | + "cache.item_size": JSON.stringify(value).length, // Warning: if value is very big this could use lots of memory |
| 82 | + "network.peer.address": "cache.example.com/supercache", |
| 83 | + "network.peer.port": 9000, |
| 84 | + }, |
| 85 | + op: "cache.put", |
| 86 | + }, |
| 87 | + (span) => { |
| 88 | + // Set a key in your caching using your custom caching solution |
| 89 | + my_caching.set(key, value); |
| 90 | + } |
| 91 | +); |
| 92 | +``` |
| 93 | + |
| 94 | +### Step 2: Add Span When Retrieving Data From the Cache |
| 95 | + |
| 96 | +If the cache you’re using isn’t supported by the auto instrumentation mentioned above, you can use the custom instrumentation instructions below to emit cache spans instead: |
| 97 | + |
| 98 | +1. Get the cached value from whatever cache library you happen to be using. |
| 99 | +2. Wrap the part of your application that fetches from the cache with `Sentry.startSpan(...)`. |
| 100 | +3. Set the `name` to something descriptive like "Getting auth cache". |
| 101 | +4. Set `op` to `cache.get`. |
| 102 | +5. Set `cache.key` to a string array representing the key(s) you're setting. |
| 103 | +6. Set `cache.hit` to a boolean value representing whether the value was successfully fetched from the cache or not. |
| 104 | +7. Optionally, you can set other attributes such as `cache.item_size`. (See [Cache Module Span Data Conventions](https://develop.sentry.dev/sdk/performance/modules/caches/#span-data) for more information.) |
| 105 | + (The steps described above are also documented in the snippet.) |
| 106 | + |
| 107 | +```javascript {filename: my-cache.js} |
| 108 | +const key = "myCacheKey123"; |
| 109 | + |
| 110 | +Sentry.startSpan( |
| 111 | + { |
| 112 | + name: key, |
| 113 | + attributes: { |
| 114 | + "cache.key": [key], |
| 115 | + "network.peer.address": "cache.example.com/supercache", |
| 116 | + "network.peer.port": 9000, |
| 117 | + }, |
| 118 | + op: "cache.get", |
| 119 | + }, |
| 120 | + (span) => { |
| 121 | + // Set a key in your caching using your custom caching solution |
| 122 | + const value = my_caching.get(key); |
| 123 | + const cacheHit = Boolean(value); |
| 124 | + if (cacheHit) { |
| 125 | + span.setAttribute("cache.item_size", JSON.stringify(value).length, // Warning: if value is very big this could use lots of memory); |
| 126 | + } |
| 127 | + span.setAttribute("cache.hit", cacheHit); |
| 128 | + } |
| 129 | +); |
| 130 | +``` |
| 131 | +
|
| 132 | +You should now have the right spans in place. Head over to the [Cache dashboard](https://sentry.io/orgredirect/organizations/:orgslug/insights/backend/caches/) to see how your cache is performing. |
0 commit comments