Skip to content

Commit ef00e7d

Browse files
authored
Merge a3b5eb4 into bbbb702
2 parents bbbb702 + a3b5eb4 commit ef00e7d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ androidx-browser = { module = "androidx.browser:browser", version = "1.8.0" }
100100
async-profiler = { module = "tools.profiler:async-profiler", version.ref = "asyncProfiler" }
101101
async-profiler-jfr-converter = { module = "tools.profiler:jfr-converter", version.ref = "asyncProfiler" }
102102
caffeine = { module = "com.github.ben-manes.caffeine:caffeine" }
103+
caffeine-jcache = { module = "com.github.ben-manes.caffeine:jcache", version = "3.2.0" }
103104
coil-compose = { module = "io.coil-kt:coil-compose", version = "2.6.0" }
104105
commons-compress = {module = "org.apache.commons:commons-compress", version = "1.25.0"}
105106
context-propagation = { module = "io.micrometer:context-propagation", version = "1.1.0" }

sentry-samples/sentry-samples-console/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ tasks.withType<KotlinCompile>().configureEach {
3535
dependencies {
3636
implementation(projects.sentry)
3737
implementation(projects.sentryAsyncProfiler)
38+
implementation(projects.sentryJcache)
39+
implementation(libs.jcache)
40+
implementation(libs.caffeine.jcache)
3841

3942
testImplementation(kotlin(Config.kotlinStdLib))
4043
testImplementation(projects.sentry)

sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
import io.sentry.*;
44
import io.sentry.clientreport.DiscardReason;
5+
import io.sentry.jcache.SentryJCacheWrapper;
56
import io.sentry.protocol.Message;
67
import io.sentry.protocol.User;
78
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;
813

914
public class Main {
1015

@@ -88,6 +93,9 @@ public static void main(String[] args) throws InterruptedException {
8893
// Set what percentage of traces should be collected
8994
options.setTracesSampleRate(1.0); // set 0.5 to send 50% of traces
9095

96+
// Enable cache tracing to create spans for cache operations
97+
options.setEnableCacheTracing(true);
98+
9199
// Determine traces sample rate based on the sampling context
92100
// options.setTracesSampler(
93101
// context -> {
@@ -162,6 +170,12 @@ public static void main(String[] args) throws InterruptedException {
162170
Sentry.captureEvent(event, hint);
163171
}
164172

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+
165179
// Performance feature
166180
//
167181
// 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 {
189203
// Sentry.close();
190204
}
191205

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+
192242
private static void captureMetrics() {
193243
Sentry.metrics().count("countMetric");
194244
Sentry.metrics().gauge("gaugeMetric", 5.0);

0 commit comments

Comments
 (0)