From 8434b4ebbcb318d00ac738e3296a427ea2408418 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 10 Mar 2026 10:27:29 +0100 Subject: [PATCH] feat(samples): Add JCache cache tracing demo to console sample Co-Authored-By: Claude Opus 4.6 --- gradle/libs.versions.toml | 1 + .../sentry-samples-console/build.gradle.kts | 3 ++ .../java/io/sentry/samples/console/Main.java | 50 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1e57b961fda..54731df1f24 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -100,6 +100,7 @@ androidx-browser = { module = "androidx.browser:browser", version = "1.8.0" } async-profiler = { module = "tools.profiler:async-profiler", version.ref = "asyncProfiler" } async-profiler-jfr-converter = { module = "tools.profiler:jfr-converter", version.ref = "asyncProfiler" } caffeine = { module = "com.github.ben-manes.caffeine:caffeine" } +caffeine-jcache = { module = "com.github.ben-manes.caffeine:jcache", version = "3.2.0" } coil-compose = { module = "io.coil-kt:coil-compose", version = "2.6.0" } commons-compress = {module = "org.apache.commons:commons-compress", version = "1.25.0"} context-propagation = { module = "io.micrometer:context-propagation", version = "1.1.0" } diff --git a/sentry-samples/sentry-samples-console/build.gradle.kts b/sentry-samples/sentry-samples-console/build.gradle.kts index 5737e8effe0..0dc6183b4fc 100644 --- a/sentry-samples/sentry-samples-console/build.gradle.kts +++ b/sentry-samples/sentry-samples-console/build.gradle.kts @@ -35,6 +35,9 @@ tasks.withType().configureEach { dependencies { implementation(projects.sentry) implementation(projects.sentryAsyncProfiler) + implementation(projects.sentryJcache) + implementation(libs.jcache) + implementation(libs.caffeine.jcache) testImplementation(kotlin(Config.kotlinStdLib)) testImplementation(projects.sentry) diff --git a/sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java b/sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java index fd21476f402..15888cb5702 100644 --- a/sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java +++ b/sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java @@ -2,9 +2,14 @@ import io.sentry.*; import io.sentry.clientreport.DiscardReason; +import io.sentry.jcache.SentryJCacheWrapper; import io.sentry.protocol.Message; import io.sentry.protocol.User; import java.util.Collections; +import javax.cache.Cache; +import javax.cache.CacheManager; +import javax.cache.Caching; +import javax.cache.configuration.MutableConfiguration; public class Main { @@ -88,6 +93,9 @@ public static void main(String[] args) throws InterruptedException { // Set what percentage of traces should be collected options.setTracesSampleRate(1.0); // set 0.5 to send 50% of traces + // Enable cache tracing to create spans for cache operations + options.setEnableCacheTracing(true); + // Determine traces sample rate based on the sampling context // options.setTracesSampler( // context -> { @@ -162,6 +170,12 @@ public static void main(String[] args) throws InterruptedException { Sentry.captureEvent(event, hint); } + // Cache tracing with JCache (JSR-107) + // + // Wrapping a JCache Cache with SentryJCacheWrapper creates cache.get, cache.put, + // cache.remove, and cache.flush spans as children of the active transaction. + demonstrateCacheTracing(); + // Performance feature // // 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 { // Sentry.close(); } + private static void demonstrateCacheTracing() { + // Create a JCache CacheManager and Cache using standard JSR-107 API + CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(); + MutableConfiguration config = + new MutableConfiguration().setTypes(String.class, String.class); + Cache rawCache = cacheManager.createCache("myCache", config); + + // Wrap with SentryJCacheWrapper to enable cache tracing + Cache cache = new SentryJCacheWrapper<>(rawCache, Sentry.getCurrentScopes()); + + // All cache operations inside a transaction produce child spans + ITransaction transaction = Sentry.startTransaction("cache-demo", "demo"); + try (ISentryLifecycleToken ignored = transaction.makeCurrent()) { + // cache.put span + cache.put("greeting", "hello"); + + // cache.get span (hit — returns "hello", cache.hit = true) + cache.get("greeting"); + + // cache.get span (miss — returns null, cache.hit = false) + cache.get("nonexistent"); + + // cache.remove span + cache.remove("greeting"); + + // cache.flush span + cache.clear(); + } finally { + transaction.finish(); + } + + // Clean up + cacheManager.destroyCache("myCache"); + cacheManager.close(); + } + private static void captureMetrics() { Sentry.metrics().count("countMetric"); Sentry.metrics().gauge("gaugeMetric", 5.0);