|
| 1 | +--- |
| 2 | +title: JCache Integration |
| 3 | +description: "Learn how to trace cache operations using the Sentry JCache (JSR-107) integration." |
| 4 | +notSupported: |
| 5 | + - java.spring-boot |
| 6 | +--- |
| 7 | + |
| 8 | +Sentry's [JCache (JSR-107)](https://www.jcp.org/en/jsr/detail?id=107) integration automatically creates spans for cache operations like `get`, `put`, `remove`, and `clear`. It works with any JCache provider (Caffeine, Ehcache, Hazelcast, etc.). |
| 9 | + |
| 10 | +Cache spans appear in Sentry's [Caches dashboard](https://sentry.io/orgredirect/organizations/:orgslug/insights/backend/caches/). |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```groovy {tabTitle:Gradle} |
| 15 | +implementation 'io.sentry:sentry-jcache:{{@inject packages.version('sentry.java.jcache', '8.35.0') }}' |
| 16 | +``` |
| 17 | + |
| 18 | +```xml {tabTitle:Maven} |
| 19 | +<dependency> |
| 20 | + <groupId>io.sentry</groupId> |
| 21 | + <artifactId>sentry-jcache</artifactId> |
| 22 | + <version>{{@inject packages.version('sentry.java.jcache', '8.35.0') }}</version> |
| 23 | +</dependency> |
| 24 | +``` |
| 25 | + |
| 26 | +```scala {tabTitle: SBT} |
| 27 | +libraryDependencies += "io.sentry" % "sentry-jcache" % "{{@inject packages.version('sentry.java.jcache', '8.35.0') }}" |
| 28 | +``` |
| 29 | + |
| 30 | +For other dependency managers, check out the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-jcache). |
| 31 | + |
| 32 | +## Set Up |
| 33 | + |
| 34 | +Enable cache tracing in your Sentry configuration: |
| 35 | + |
| 36 | +```java {tabTitle:Java} |
| 37 | +Sentry.init(options -> { |
| 38 | + options.setDsn("___DSN___"); |
| 39 | + options.setTracesSampleRate(1.0); |
| 40 | + options.setEnableCacheTracing(true); |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +```kotlin {tabTitle:Kotlin} |
| 45 | +Sentry.init { options -> |
| 46 | + options.dsn = "___DSN___" |
| 47 | + options.tracesSampleRate = 1.0 |
| 48 | + options.isEnableCacheTracing = true |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Wrap your `javax.cache.Cache` instance with `SentryJCacheWrapper`: |
| 53 | + |
| 54 | +```java {tabTitle:Java} |
| 55 | +import io.sentry.jcache.SentryJCacheWrapper; |
| 56 | +import javax.cache.Cache; |
| 57 | +import javax.cache.CacheManager; |
| 58 | +import javax.cache.Caching; |
| 59 | +import javax.cache.spi.CachingProvider; |
| 60 | + |
| 61 | +CachingProvider provider = Caching.getCachingProvider(); |
| 62 | +CacheManager manager = provider.getCacheManager(); |
| 63 | + |
| 64 | +Cache<String, String> cache = manager.getCache("myCache"); |
| 65 | +Cache<String, String> sentryCache = new SentryJCacheWrapper<>(cache); |
| 66 | + |
| 67 | +// Use sentryCache — all operations produce Sentry spans |
| 68 | +sentryCache.put("key", "value"); // cache.put span |
| 69 | +String val = sentryCache.get("key"); // cache.get span (cache.hit = true) |
| 70 | +sentryCache.remove("key"); // cache.remove span |
| 71 | +sentryCache.clear(); // cache.clear span |
| 72 | +``` |
| 73 | + |
| 74 | +```kotlin {tabTitle:Kotlin} |
| 75 | +import io.sentry.jcache.SentryJCacheWrapper |
| 76 | +import javax.cache.Caching |
| 77 | + |
| 78 | +val provider = Caching.getCachingProvider() |
| 79 | +val manager = provider.cacheManager |
| 80 | + |
| 81 | +val cache = manager.getCache<String, String>("myCache") |
| 82 | +val sentryCache = SentryJCacheWrapper(cache) |
| 83 | + |
| 84 | +// Use sentryCache — all operations produce Sentry spans |
| 85 | +sentryCache.put("key", "value") // cache.put span |
| 86 | +val value = sentryCache.get("key") // cache.get span (cache.hit = true) |
| 87 | +sentryCache.remove("key") // cache.remove span |
| 88 | +sentryCache.clear() // cache.clear span |
| 89 | +``` |
| 90 | + |
| 91 | +## Traced Operations |
| 92 | + |
| 93 | +All JCache operations are traced. Each method creates a span with the operation `cache.<methodName>` (e.g. `cache.get`, `cache.putIfAbsent`, `cache.removeAll`). |
| 94 | + |
| 95 | +## Span Data |
| 96 | + |
| 97 | +| Key | Type | Description | |
| 98 | +|---|---|---| |
| 99 | +| `cache.hit` | boolean | Whether the cache lookup returned a value (read spans only) | |
| 100 | +| `cache.key` | list of strings | The cache key(s) involved in the operation | |
| 101 | +| `cache.operation` | string | The JCache method name (e.g. `get`, `putIfAbsent`, `removeAll`) | |
| 102 | +| `cache.write` | boolean | Whether the operation modified the cache. Always `true` for unconditional writes (`put`, `putAll`, `remove`, `clear`); reflects the actual outcome for conditional operations (`putIfAbsent`, `replace`, `getAndReplace`) and value-matched removes | |
| 103 | + |
| 104 | +Bulk operations (`getAll`, `putAll`, `removeAll`) create a single span with all keys listed in `cache.key`. |
| 105 | + |
| 106 | +## Verify |
| 107 | + |
| 108 | +To verify, trigger a cache operation within an active transaction and check the [Caches dashboard](https://sentry.io/orgredirect/organizations/:orgslug/insights/backend/caches/) or the trace view in Sentry. |
| 109 | + |
| 110 | +```java {tabTitle:Java} |
| 111 | +import io.sentry.ISentryLifecycleToken; |
| 112 | +import io.sentry.ITransaction; |
| 113 | +import io.sentry.Sentry; |
| 114 | + |
| 115 | +ITransaction tx = Sentry.startTransaction("test-cache", "task"); |
| 116 | +try (ISentryLifecycleToken ignored = tx.makeCurrent()) { |
| 117 | + sentryCache.put("greeting", "hello"); |
| 118 | + String value = sentryCache.get("greeting"); |
| 119 | +} finally { |
| 120 | + tx.finish(); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +```kotlin {tabTitle:Kotlin} |
| 125 | +import io.sentry.Sentry |
| 126 | + |
| 127 | +val tx = Sentry.startTransaction("test-cache", "task") |
| 128 | +try { |
| 129 | + tx.makeCurrent().use { |
| 130 | + sentryCache.put("greeting", "hello") |
| 131 | + val value = sentryCache.get("greeting") |
| 132 | + } |
| 133 | +} finally { |
| 134 | + tx.finish() |
| 135 | +} |
| 136 | +``` |
0 commit comments