4
4
import org .dataloader .annotations .Internal ;
5
5
import org .dataloader .impl .CompletableFutureKit ;
6
6
import org .dataloader .stats .StatisticsCollector ;
7
+ import org .dataloader .stats .context .IncrementBatchLoadCountByStatisticsContext ;
8
+ import org .dataloader .stats .context .IncrementBatchLoadExceptionCountStatisticsContext ;
9
+ import org .dataloader .stats .context .IncrementCacheHitCountStatisticsContext ;
10
+ import org .dataloader .stats .context .IncrementLoadCountStatisticsContext ;
11
+ import org .dataloader .stats .context .IncrementLoadErrorCountStatisticsContext ;
7
12
8
13
import java .time .Clock ;
9
14
import java .time .Instant ;
@@ -105,7 +110,7 @@ Optional<CompletableFuture<V>> getIfPresent(K key) {
105
110
if (cachingEnabled ) {
106
111
Object cacheKey = getCacheKey (nonNull (key ));
107
112
if (futureCache .containsKey (cacheKey )) {
108
- stats .incrementCacheHitCount ();
113
+ stats .incrementCacheHitCount (new IncrementCacheHitCountStatisticsContext <>( key ) );
109
114
return Optional .of (futureCache .get (cacheKey ));
110
115
}
111
116
}
@@ -132,7 +137,7 @@ CompletableFuture<V> load(K key, Object loadContext) {
132
137
boolean batchingEnabled = loaderOptions .batchingEnabled ();
133
138
boolean cachingEnabled = loaderOptions .cachingEnabled ();
134
139
135
- stats .incrementLoadCount ();
140
+ stats .incrementLoadCount (new IncrementLoadCountStatisticsContext <>( key , loadContext ) );
136
141
137
142
if (cachingEnabled ) {
138
143
return loadFromCache (key , loadContext , batchingEnabled );
@@ -223,18 +228,20 @@ private CompletableFuture<List<V>> sliceIntoBatchesOfBatches(List<K> keys, List<
223
228
224
229
@ SuppressWarnings ("unchecked" )
225
230
private CompletableFuture <List <V >> dispatchQueueBatch (List <K > keys , List <Object > callContexts , List <CompletableFuture <V >> queuedFutures ) {
226
- stats .incrementBatchLoadCountBy (keys .size ());
231
+ stats .incrementBatchLoadCountBy (keys .size (), new IncrementBatchLoadCountByStatisticsContext <>( keys , callContexts ) );
227
232
CompletableFuture <List <V >> batchLoad = invokeLoader (keys , callContexts , loaderOptions .cachingEnabled ());
228
233
return batchLoad
229
234
.thenApply (values -> {
230
235
assertResultSize (keys , values );
231
236
232
237
List <K > clearCacheKeys = new ArrayList <>();
233
238
for (int idx = 0 ; idx < queuedFutures .size (); idx ++) {
239
+ K key = keys .get (idx );
234
240
V value = values .get (idx );
241
+ Object callContext = callContexts .get (idx );
235
242
CompletableFuture <V > future = queuedFutures .get (idx );
236
243
if (value instanceof Throwable ) {
237
- stats .incrementLoadErrorCount ();
244
+ stats .incrementLoadErrorCount (new IncrementLoadErrorCountStatisticsContext <>( key , callContext ) );
238
245
future .completeExceptionally ((Throwable ) value );
239
246
clearCacheKeys .add (keys .get (idx ));
240
247
} else if (value instanceof Try ) {
@@ -244,7 +251,7 @@ private CompletableFuture<List<V>> dispatchQueueBatch(List<K> keys, List<Object>
244
251
if (tryValue .isSuccess ()) {
245
252
future .complete (tryValue .get ());
246
253
} else {
247
- stats .incrementLoadErrorCount ();
254
+ stats .incrementLoadErrorCount (new IncrementLoadErrorCountStatisticsContext <>( key , callContext ) );
248
255
future .completeExceptionally (tryValue .getThrowable ());
249
256
clearCacheKeys .add (keys .get (idx ));
250
257
}
@@ -255,7 +262,7 @@ private CompletableFuture<List<V>> dispatchQueueBatch(List<K> keys, List<Object>
255
262
possiblyClearCacheEntriesOnExceptions (clearCacheKeys );
256
263
return values ;
257
264
}).exceptionally (ex -> {
258
- stats .incrementBatchLoadExceptionCount ();
265
+ stats .incrementBatchLoadExceptionCount (new IncrementBatchLoadExceptionCountStatisticsContext <>( keys , callContexts ) );
259
266
if (ex instanceof CompletionException ) {
260
267
ex = ex .getCause ();
261
268
}
@@ -294,7 +301,7 @@ private CompletableFuture<V> loadFromCache(K key, Object loadContext, boolean ba
294
301
295
302
if (futureCache .containsKey (cacheKey )) {
296
303
// We already have a promise for this key, no need to check value cache or queue up load
297
- stats .incrementCacheHitCount ();
304
+ stats .incrementCacheHitCount (new IncrementCacheHitCountStatisticsContext <>( key , loadContext ) );
298
305
return futureCache .get (cacheKey );
299
306
}
300
307
@@ -310,7 +317,7 @@ private CompletableFuture<V> queueOrInvokeLoader(K key, Object loadContext, bool
310
317
loaderQueue .add (new LoaderQueueEntry <>(key , loadCallFuture , loadContext ));
311
318
return loadCallFuture ;
312
319
} else {
313
- stats .incrementBatchLoadCountBy (1 );
320
+ stats .incrementBatchLoadCountBy (1 , new IncrementBatchLoadCountByStatisticsContext <>( key , loadContext ) );
314
321
// immediate execution of batch function
315
322
return invokeLoaderImmediately (key , loadContext , cachingEnabled );
316
323
}
0 commit comments