|
| 1 | +--- |
| 2 | +title: Instrument Caches |
| 3 | +sidebar_order: 1000 |
| 4 | +description: "Learn how to manually instrument your code to use Sentry's Caches module. " |
| 5 | +--- |
| 6 | +Sentry offers a cache-monitoring dashboard that can be auto-instrumented by some SDKs. If you're Go, you can manually instrument your cache operations and use Sentry to monitor your caching performance to get a look into how your caching solution is performing by following the setup instructions below. |
| 7 | + |
| 8 | +To allow Sentry to track cache performance, you'll need to create two spans: |
| 9 | + |
| 10 | +- One for when an item is put into the cache. |
| 11 | +- Another for when an item is fetched from the cache. |
| 12 | + |
| 13 | +Ensure there is an active transaction when creating the spans. For more information, see [Tracing](../..). |
| 14 | + |
| 15 | +For detailed information about the available data, see the Cache Module developer specification. |
| 16 | + |
| 17 | +## Custom Instrumentation |
| 18 | + |
| 19 | +As the GO SDK does not support auto cache instrumentation, you'll need to manually instrument the Cache Module as described below. |
| 20 | + |
| 21 | +### Add Span When Putting Data Into the Cache |
| 22 | + |
| 23 | +Use custom instrumentation to emit spans for cache data. |
| 24 | + |
| 25 | +- Set the cache value using your preferred cache library. |
| 26 | +- Wrap the part of your application that adds data to the cache in a span. |
| 27 | +- Set `op` to `cache.put`. |
| 28 | +- Optionally, set `cache.item_size` to the size of the cached item. |
| 29 | + |
| 30 | +#### Example (Go) |
| 31 | + |
| 32 | +```go |
| 33 | +key := "cache_key" |
| 34 | +value := "cache_value" |
| 35 | + |
| 36 | +parentSpan := sentry.StartSpan(context.Background(), "parent_span") |
| 37 | + |
| 38 | +if parentSpan != nil { |
| 39 | + span := parentSpan.StartChild("cache.put") |
| 40 | + defer span.Finish() |
| 41 | + |
| 42 | + // Perform the cache operation |
| 43 | + err := cache.Put(key, value) |
| 44 | + |
| 45 | + span.SetData("network.peer.address", "127.0.0.1") |
| 46 | + span.SetData("network.peer.port", 9000) |
| 47 | + span.SetData("cache.key", key) |
| 48 | + span.SetData("cache.item_size", len(value)) |
| 49 | + |
| 50 | + if err != nil { |
| 51 | + span.SetTag("error", err.Error()) |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Add Span When Retrieving Data From the Cache |
| 57 | + |
| 58 | +For unsupported cache solutions, use custom instrumentation to emit cache spans: |
| 59 | + |
| 60 | +Fetch the cached value using your preferred cache library. |
| 61 | +Wrap the part of your application that retrieves data from the cache in a span. |
| 62 | +Set op to cache.get. |
| 63 | +Set cache.hit to true or false, depending on whether the value was found. |
| 64 | +Optionally, set cache.item_size to the size of the retrieved item. |
| 65 | + |
| 66 | + |
| 67 | +Example: |
| 68 | +```go |
| 69 | +key := "cache_key" |
| 70 | +var value string |
| 71 | + |
| 72 | +parentSpan := sentry.StartSpan(context.Background(), "parent_span") |
| 73 | + |
| 74 | +if parentSpan != nil { |
| 75 | + span := parentSpan.StartChild("cache.get") |
| 76 | + defer span.Finish() |
| 77 | + |
| 78 | + // Perform the cache operation |
| 79 | + value, err := cache.Get(key) |
| 80 | + |
| 81 | + span.SetData("network.peer.address", "127.0.0.1") |
| 82 | + span.SetData("network.peer.port", 9000) |
| 83 | + span.SetData("cache.key", key) |
| 84 | + |
| 85 | + if err == nil && value != "" { |
| 86 | + span.SetData("cache.hit", true) |
| 87 | + span.SetData("cache.item_size", len(value)) |
| 88 | + } else { |
| 89 | + span.SetData("cache.hit", false) |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +You should now have the right spans in place. Head over to the [Cache dashboard](https://sentry.io/orgredirect/organizations/:orgslug/performance/caches/) to see how your cache is performing. |
0 commit comments