Skip to content

Commit cb8de20

Browse files
authored
Merge pull request #5204 from getsentry/ref/cache-tracing-method-specific-ops
ref(spring): [Cache Tracing 15] Use method-specific span operations for cache spans
2 parents a06700e + f1dd736 commit cb8de20

File tree

8 files changed

+89
-98
lines changed

8 files changed

+89
-98
lines changed

sentry-jcache/src/main/java/io/sentry/jcache/SentryJCacheWrapper.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public SentryJCacheWrapper(final @NotNull Cache<K, V> delegate, final @NotNull I
5151

5252
@Override
5353
public V get(final K key) {
54-
final ISpan span = startSpan("cache.get", key, "get");
54+
final ISpan span = startSpan(key, "get");
5555
if (span == null) {
5656
return delegate.get(key);
5757
}
@@ -71,7 +71,7 @@ public V get(final K key) {
7171

7272
@Override
7373
public Map<K, V> getAll(final Set<? extends K> keys) {
74-
final ISpan span = startSpanForKeys("cache.get", keys, "getAll");
74+
final ISpan span = startSpanForKeys(keys, "getAll");
7575
if (span == null) {
7676
return delegate.getAll(keys);
7777
}
@@ -98,7 +98,7 @@ public boolean containsKey(final K key) {
9898

9999
@Override
100100
public void put(final K key, final V value) {
101-
final ISpan span = startSpan("cache.put", key, "put");
101+
final ISpan span = startSpan(key, "put");
102102
if (span == null) {
103103
delegate.put(key, value);
104104
return;
@@ -117,7 +117,7 @@ public void put(final K key, final V value) {
117117

118118
@Override
119119
public V getAndPut(final K key, final V value) {
120-
final ISpan span = startSpan("cache.put", key, "getAndPut");
120+
final ISpan span = startSpan(key, "getAndPut");
121121
if (span == null) {
122122
return delegate.getAndPut(key, value);
123123
}
@@ -136,7 +136,7 @@ public V getAndPut(final K key, final V value) {
136136

137137
@Override
138138
public void putAll(final Map<? extends K, ? extends V> map) {
139-
final ISpan span = startSpanForKeys("cache.put", map.keySet(), "putAll");
139+
final ISpan span = startSpanForKeys(map.keySet(), "putAll");
140140
if (span == null) {
141141
delegate.putAll(map);
142142
return;
@@ -155,7 +155,7 @@ public void putAll(final Map<? extends K, ? extends V> map) {
155155

156156
@Override
157157
public boolean putIfAbsent(final K key, final V value) {
158-
final ISpan span = startSpan("cache.put", key, "putIfAbsent");
158+
final ISpan span = startSpan(key, "putIfAbsent");
159159
if (span == null) {
160160
return delegate.putIfAbsent(key, value);
161161
}
@@ -174,7 +174,7 @@ public boolean putIfAbsent(final K key, final V value) {
174174

175175
@Override
176176
public boolean replace(final K key, final V oldValue, final V newValue) {
177-
final ISpan span = startSpan("cache.put", key, "replace");
177+
final ISpan span = startSpan(key, "replace");
178178
if (span == null) {
179179
return delegate.replace(key, oldValue, newValue);
180180
}
@@ -193,7 +193,7 @@ public boolean replace(final K key, final V oldValue, final V newValue) {
193193

194194
@Override
195195
public boolean replace(final K key, final V value) {
196-
final ISpan span = startSpan("cache.put", key, "replace");
196+
final ISpan span = startSpan(key, "replace");
197197
if (span == null) {
198198
return delegate.replace(key, value);
199199
}
@@ -212,7 +212,7 @@ public boolean replace(final K key, final V value) {
212212

213213
@Override
214214
public V getAndReplace(final K key, final V value) {
215-
final ISpan span = startSpan("cache.put", key, "getAndReplace");
215+
final ISpan span = startSpan(key, "getAndReplace");
216216
if (span == null) {
217217
return delegate.getAndReplace(key, value);
218218
}
@@ -233,7 +233,7 @@ public V getAndReplace(final K key, final V value) {
233233

234234
@Override
235235
public boolean remove(final K key) {
236-
final ISpan span = startSpan("cache.remove", key, "remove");
236+
final ISpan span = startSpan(key, "remove");
237237
if (span == null) {
238238
return delegate.remove(key);
239239
}
@@ -252,7 +252,7 @@ public boolean remove(final K key) {
252252

253253
@Override
254254
public boolean remove(final K key, final V oldValue) {
255-
final ISpan span = startSpan("cache.remove", key, "remove");
255+
final ISpan span = startSpan(key, "remove");
256256
if (span == null) {
257257
return delegate.remove(key, oldValue);
258258
}
@@ -271,7 +271,7 @@ public boolean remove(final K key, final V oldValue) {
271271

272272
@Override
273273
public V getAndRemove(final K key) {
274-
final ISpan span = startSpan("cache.remove", key, "getAndRemove");
274+
final ISpan span = startSpan(key, "getAndRemove");
275275
if (span == null) {
276276
return delegate.getAndRemove(key);
277277
}
@@ -290,7 +290,7 @@ public V getAndRemove(final K key) {
290290

291291
@Override
292292
public void removeAll(final Set<? extends K> keys) {
293-
final ISpan span = startSpanForKeys("cache.remove", keys, "removeAll");
293+
final ISpan span = startSpanForKeys(keys, "removeAll");
294294
if (span == null) {
295295
delegate.removeAll(keys);
296296
return;
@@ -309,7 +309,7 @@ public void removeAll(final Set<? extends K> keys) {
309309

310310
@Override
311311
public void removeAll() {
312-
final ISpan span = startSpan("cache.flush", null, "removeAll");
312+
final ISpan span = startSpan(null, "removeAll");
313313
if (span == null) {
314314
delegate.removeAll();
315315
return;
@@ -330,7 +330,7 @@ public void removeAll() {
330330

331331
@Override
332332
public void clear() {
333-
final ISpan span = startSpan("cache.flush", null, "clear");
333+
final ISpan span = startSpan(null, "clear");
334334
if (span == null) {
335335
delegate.clear();
336336
return;
@@ -358,7 +358,7 @@ public void close() {
358358
public <T> T invoke(
359359
final K key, final EntryProcessor<K, V, T> entryProcessor, final Object... arguments)
360360
throws EntryProcessorException {
361-
final ISpan span = startSpan("cache.get", key, "invoke");
361+
final ISpan span = startSpan(key, "invoke");
362362
if (span == null) {
363363
return delegate.invoke(key, entryProcessor, arguments);
364364
}
@@ -380,7 +380,7 @@ public <T> Map<K, EntryProcessorResult<T>> invokeAll(
380380
final Set<? extends K> keys,
381381
final EntryProcessor<K, V, T> entryProcessor,
382382
final Object... arguments) {
383-
final ISpan span = startSpanForKeys("cache.get", keys, "invokeAll");
383+
final ISpan span = startSpanForKeys(keys, "invokeAll");
384384
if (span == null) {
385385
return delegate.invokeAll(keys, entryProcessor, arguments);
386386
}
@@ -453,9 +453,7 @@ public Iterator<Entry<K, V>> iterator() {
453453
// -- span helpers --
454454

455455
private @Nullable ISpan startSpan(
456-
final @NotNull String operation,
457-
final @Nullable Object key,
458-
final @NotNull String operationName) {
456+
final @Nullable Object key, final @NotNull String operationName) {
459457
if (!scopes.getOptions().isEnableCacheTracing()) {
460458
return null;
461459
}
@@ -468,7 +466,7 @@ public Iterator<Entry<K, V>> iterator() {
468466
final SpanOptions spanOptions = new SpanOptions();
469467
spanOptions.setOrigin(TRACE_ORIGIN);
470468
final String keyString = key != null ? String.valueOf(key) : null;
471-
final ISpan span = activeSpan.startChild(operation, keyString, spanOptions);
469+
final ISpan span = activeSpan.startChild("cache." + operationName, keyString, spanOptions);
472470
if (span.isNoOp()) {
473471
return null;
474472
}
@@ -480,9 +478,7 @@ public Iterator<Entry<K, V>> iterator() {
480478
}
481479

482480
private @Nullable ISpan startSpanForKeys(
483-
final @NotNull String operation,
484-
final @NotNull Set<?> keys,
485-
final @NotNull String operationName) {
481+
final @NotNull Set<?> keys, final @NotNull String operationName) {
486482
if (!scopes.getOptions().isEnableCacheTracing()) {
487483
return null;
488484
}
@@ -494,7 +490,8 @@ public Iterator<Entry<K, V>> iterator() {
494490

495491
final SpanOptions spanOptions = new SpanOptions();
496492
spanOptions.setOrigin(TRACE_ORIGIN);
497-
final ISpan span = activeSpan.startChild(operation, delegate.getName(), spanOptions);
493+
final ISpan span =
494+
activeSpan.startChild("cache." + operationName, delegate.getName(), spanOptions);
498495
if (span.isNoOp()) {
499496
return null;
500497
}

sentry-jcache/src/test/kotlin/io/sentry/jcache/SentryJCacheWrapperTest.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class SentryJCacheWrapperTest {
9494
assertEquals(mapOf("k1" to "v1"), result)
9595
assertEquals(1, tx.spans.size)
9696
val span = tx.spans.first()
97-
assertEquals("cache.get", span.operation)
97+
assertEquals("cache.getAll", span.operation)
9898
assertEquals("testCache", span.description)
9999
assertEquals(true, span.getData(SpanDataConvention.CACHE_HIT_KEY))
100100
val cacheKeys = span.getData(SpanDataConvention.CACHE_KEY_KEY) as List<*>
@@ -144,7 +144,7 @@ class SentryJCacheWrapperTest {
144144

145145
assertEquals("oldValue", result)
146146
assertEquals(1, tx.spans.size)
147-
assertEquals("cache.put", tx.spans.first().operation)
147+
assertEquals("cache.getAndPut", tx.spans.first().operation)
148148
assertEquals("getAndPut", tx.spans.first().getData("db.operation.name"))
149149
}
150150

@@ -161,7 +161,7 @@ class SentryJCacheWrapperTest {
161161
verify(delegate).putAll(entries)
162162
assertEquals(1, tx.spans.size)
163163
val span = tx.spans.first()
164-
assertEquals("cache.put", span.operation)
164+
assertEquals("cache.putAll", span.operation)
165165
assertEquals("testCache", span.description)
166166
val cacheKeys = span.getData(SpanDataConvention.CACHE_KEY_KEY) as List<*>
167167
assertTrue(cacheKeys.containsAll(listOf("k1", "k2")))
@@ -182,7 +182,7 @@ class SentryJCacheWrapperTest {
182182
verify(delegate).putIfAbsent("myKey", "myValue")
183183
assertEquals(1, tx.spans.size)
184184
val span = tx.spans.first()
185-
assertEquals("cache.put", span.operation)
185+
assertEquals("cache.putIfAbsent", span.operation)
186186
assertEquals(SpanStatus.OK, span.status)
187187
assertEquals(listOf("myKey"), span.getData(SpanDataConvention.CACHE_KEY_KEY))
188188
assertEquals("putIfAbsent", span.getData("db.operation.name"))
@@ -202,7 +202,7 @@ class SentryJCacheWrapperTest {
202202
verify(delegate).replace("myKey", "old", "new")
203203
assertEquals(1, tx.spans.size)
204204
val span = tx.spans.first()
205-
assertEquals("cache.put", span.operation)
205+
assertEquals("cache.replace", span.operation)
206206
assertEquals(SpanStatus.OK, span.status)
207207
assertEquals("replace", span.getData("db.operation.name"))
208208
}
@@ -219,7 +219,7 @@ class SentryJCacheWrapperTest {
219219
verify(delegate).replace("myKey", "value")
220220
assertEquals(1, tx.spans.size)
221221
val span = tx.spans.first()
222-
assertEquals("cache.put", span.operation)
222+
assertEquals("cache.replace", span.operation)
223223
assertEquals(SpanStatus.OK, span.status)
224224
assertEquals("replace", span.getData("db.operation.name"))
225225
}
@@ -238,7 +238,7 @@ class SentryJCacheWrapperTest {
238238
verify(delegate).getAndReplace("myKey", "newValue")
239239
assertEquals(1, tx.spans.size)
240240
val span = tx.spans.first()
241-
assertEquals("cache.put", span.operation)
241+
assertEquals("cache.getAndReplace", span.operation)
242242
assertEquals(SpanStatus.OK, span.status)
243243
assertEquals("getAndReplace", span.getData("db.operation.name"))
244244
}
@@ -289,7 +289,7 @@ class SentryJCacheWrapperTest {
289289

290290
assertEquals("value", result)
291291
assertEquals(1, tx.spans.size)
292-
assertEquals("cache.remove", tx.spans.first().operation)
292+
assertEquals("cache.getAndRemove", tx.spans.first().operation)
293293
assertEquals("getAndRemove", tx.spans.first().getData("db.operation.name"))
294294
}
295295

@@ -306,7 +306,7 @@ class SentryJCacheWrapperTest {
306306
verify(delegate).removeAll(keys)
307307
assertEquals(1, tx.spans.size)
308308
val span = tx.spans.first()
309-
assertEquals("cache.remove", span.operation)
309+
assertEquals("cache.removeAll", span.operation)
310310
assertEquals("testCache", span.description)
311311
assertEquals("removeAll", span.getData("db.operation.name"))
312312
}
@@ -322,7 +322,7 @@ class SentryJCacheWrapperTest {
322322

323323
verify(delegate).removeAll()
324324
assertEquals(1, tx.spans.size)
325-
assertEquals("cache.flush", tx.spans.first().operation)
325+
assertEquals("cache.removeAll", tx.spans.first().operation)
326326
assertEquals("removeAll", tx.spans.first().getData("db.operation.name"))
327327
}
328328

@@ -338,7 +338,7 @@ class SentryJCacheWrapperTest {
338338
verify(delegate).clear()
339339
assertEquals(1, tx.spans.size)
340340
val span = tx.spans.first()
341-
assertEquals("cache.flush", span.operation)
341+
assertEquals("cache.clear", span.operation)
342342
assertEquals(SpanStatus.OK, span.status)
343343
assertNull(span.getData(SpanDataConvention.CACHE_KEY_KEY))
344344
assertEquals("clear", span.getData("db.operation.name"))
@@ -357,7 +357,7 @@ class SentryJCacheWrapperTest {
357357

358358
assertEquals("result", result)
359359
assertEquals(1, tx.spans.size)
360-
assertEquals("cache.get", tx.spans.first().operation)
360+
assertEquals("cache.invoke", tx.spans.first().operation)
361361
assertEquals("invoke", tx.spans.first().getData("db.operation.name"))
362362
}
363363

@@ -376,7 +376,7 @@ class SentryJCacheWrapperTest {
376376

377377
assertEquals(resultMap, result)
378378
assertEquals(1, tx.spans.size)
379-
assertEquals("cache.get", tx.spans.first().operation)
379+
assertEquals("cache.invokeAll", tx.spans.first().operation)
380380
assertEquals("invokeAll", tx.spans.first().getData("db.operation.name"))
381381
}
382382

0 commit comments

Comments
 (0)