|
2 | 2 |
|
3 | 3 | import io.sentry.*; |
4 | 4 | import io.sentry.clientreport.DiscardReason; |
| 5 | +import io.sentry.jcache.SentryJCacheWrapper; |
5 | 6 | import io.sentry.protocol.Message; |
6 | 7 | import io.sentry.protocol.User; |
7 | 8 | import java.util.Collections; |
| 9 | +import javax.cache.Cache; |
| 10 | +import javax.cache.CacheManager; |
| 11 | +import javax.cache.Caching; |
| 12 | +import javax.cache.configuration.MutableConfiguration; |
8 | 13 |
|
9 | 14 | public class Main { |
10 | 15 |
|
@@ -88,6 +93,9 @@ public static void main(String[] args) throws InterruptedException { |
88 | 93 | // Set what percentage of traces should be collected |
89 | 94 | options.setTracesSampleRate(1.0); // set 0.5 to send 50% of traces |
90 | 95 |
|
| 96 | + // Enable cache tracing to create spans for cache operations |
| 97 | + options.setEnableCacheTracing(true); |
| 98 | + |
91 | 99 | // Determine traces sample rate based on the sampling context |
92 | 100 | // options.setTracesSampler( |
93 | 101 | // context -> { |
@@ -162,6 +170,12 @@ public static void main(String[] args) throws InterruptedException { |
162 | 170 | Sentry.captureEvent(event, hint); |
163 | 171 | } |
164 | 172 |
|
| 173 | + // Cache tracing with JCache (JSR-107) |
| 174 | + // |
| 175 | + // Wrapping a JCache Cache with SentryJCacheWrapper creates cache.get, cache.put, |
| 176 | + // cache.remove, and cache.flush spans as children of the active transaction. |
| 177 | + demonstrateCacheTracing(); |
| 178 | + |
165 | 179 | // Performance feature |
166 | 180 | // |
167 | 181 | // Transactions collect execution time of the piece of code that's executed between the start |
@@ -189,6 +203,42 @@ public static void main(String[] args) throws InterruptedException { |
189 | 203 | // Sentry.close(); |
190 | 204 | } |
191 | 205 |
|
| 206 | + private static void demonstrateCacheTracing() { |
| 207 | + // Create a JCache CacheManager and Cache using standard JSR-107 API |
| 208 | + CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(); |
| 209 | + MutableConfiguration<String, String> config = |
| 210 | + new MutableConfiguration<String, String>().setTypes(String.class, String.class); |
| 211 | + Cache<String, String> rawCache = cacheManager.createCache("myCache", config); |
| 212 | + |
| 213 | + // Wrap with SentryJCacheWrapper to enable cache tracing |
| 214 | + Cache<String, String> cache = new SentryJCacheWrapper<>(rawCache, Sentry.getCurrentScopes()); |
| 215 | + |
| 216 | + // All cache operations inside a transaction produce child spans |
| 217 | + ITransaction transaction = Sentry.startTransaction("cache-demo", "demo"); |
| 218 | + try (ISentryLifecycleToken ignored = transaction.makeCurrent()) { |
| 219 | + // cache.put span |
| 220 | + cache.put("greeting", "hello"); |
| 221 | + |
| 222 | + // cache.get span (hit — returns "hello", cache.hit = true) |
| 223 | + cache.get("greeting"); |
| 224 | + |
| 225 | + // cache.get span (miss — returns null, cache.hit = false) |
| 226 | + cache.get("nonexistent"); |
| 227 | + |
| 228 | + // cache.remove span |
| 229 | + cache.remove("greeting"); |
| 230 | + |
| 231 | + // cache.flush span |
| 232 | + cache.clear(); |
| 233 | + } finally { |
| 234 | + transaction.finish(); |
| 235 | + } |
| 236 | + |
| 237 | + // Clean up |
| 238 | + cacheManager.destroyCache("myCache"); |
| 239 | + cacheManager.close(); |
| 240 | + } |
| 241 | + |
192 | 242 | private static void captureMetrics() { |
193 | 243 | Sentry.metrics().count("countMetric"); |
194 | 244 | Sentry.metrics().gauge("gaugeMetric", 5.0); |
|
0 commit comments