@@ -95,6 +95,7 @@ public class Mediator {
95
95
public static final String CRUD_MSG_PREFIX = "CRUD controller={}" ;
96
96
97
97
private static final Logger LOGGER = LoggerFactory .getLogger (Mediator .class );
98
+ private static final Logger METRICS = LoggerFactory .getLogger ("metrics." +Mediator .class .getName ());
98
99
99
100
private static final Path OBJECT_TYPE_PATH = new Path ("objectType" );
100
101
@@ -120,8 +121,10 @@ public Response insert(InsertionRequest req) {
120
121
LOGGER .debug ("insert {}" , req .getEntityVersion ());
121
122
Error .push ("insert(" + req .getEntityVersion ().toString () + ")" );
122
123
Response response = new Response (factory .getNodeFactory ());
124
+ OperationContext ctx =null ;
123
125
try {
124
- OperationContext ctx = newCtx (req , CRUDOperation .INSERT );
126
+ ctx = newCtx (req , CRUDOperation .INSERT );
127
+ ctx .measure .begin ("insert" );
125
128
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
126
129
EntityMetadata md = ctx .getTopLevelEntityMetadata ();
127
130
if (!md .getAccess ().getInsert ().hasAccess (ctx .getCallerRoles ())) {
@@ -136,9 +139,11 @@ public Response insert(InsertionRequest req) {
136
139
LOGGER .debug (CRUD_MSG_PREFIX , controller .getClass ().getName ());
137
140
CRUDInsertionResponse ir =controller .insert (ctx , req .getReturnFields ());
138
141
ctx .getHookManager ().queueMediatorHooks (ctx );
142
+ ctx .measure .begin ("postProcessInsertedDocs" );
139
143
response .setModifiedCount (ir .getNumInserted ());
140
144
List <DataError > dataErrors =setResponseResults (ctx ,req ,response );
141
145
response .getDataErrors ().addAll (dataErrors );
146
+ ctx .measure .begin ("postProcessInsertedDocs" );
142
147
if (!ctx .hasErrors () && dataErrors .isEmpty () && ctx .getInputDocuments ().size ()==ir .getNumInserted ()) {
143
148
ctx .setStatus (OperationStatus .COMPLETE );
144
149
} else if (ir .getNumInserted ()>0 ) {
@@ -164,6 +169,10 @@ public Response insert(InsertionRequest req) {
164
169
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
165
170
response .setStatus (OperationStatus .ERROR );
166
171
} finally {
172
+ if (ctx !=null ) {
173
+ ctx .measure .end ("insert" );
174
+ METRICS .debug ("insert" ,ctx .measure );
175
+ }
167
176
Error .pop ();
168
177
}
169
178
return response ;
@@ -185,8 +194,10 @@ public Response save(SaveRequest req) {
185
194
LOGGER .debug ("save {}" , req .getEntityVersion ());
186
195
Error .push ("save(" + req .getEntityVersion ().toString () + ")" );
187
196
Response response = new Response (factory .getNodeFactory ());
197
+ OperationContext ctx =null ;
188
198
try {
189
- OperationContext ctx = newCtx (req , CRUDOperation .SAVE );
199
+ ctx = newCtx (req , CRUDOperation .SAVE );
200
+ ctx .measure .begin ("save" );
190
201
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
191
202
EntityMetadata md = ctx .getTopLevelEntityMetadata ();
192
203
if (!md .getAccess ().getUpdate ().hasAccess (ctx .getCallerRoles ())
@@ -202,9 +213,11 @@ public Response save(SaveRequest req) {
202
213
LOGGER .debug (CRUD_MSG_PREFIX , controller .getClass ().getName ());
203
214
CRUDSaveResponse sr =controller .save (ctx , req .isUpsert (), req .getReturnFields ());
204
215
ctx .getHookManager ().queueMediatorHooks (ctx );
216
+ ctx .measure .begin ("postProcessSavedDocs" );
205
217
response .setModifiedCount (sr .getNumSaved ());
206
218
List <DataError > dataErrors =setResponseResults (ctx ,req ,response );
207
219
response .getDataErrors ().addAll (dataErrors );
220
+ ctx .measure .end ("postProcessSavedDocs" );
208
221
if (!ctx .hasErrors () && dataErrors .isEmpty () && ctx .getInputDocuments ().size ()==sr .getNumSaved ()) {
209
222
ctx .setStatus (OperationStatus .COMPLETE );
210
223
} else if (sr .getNumSaved ()>0 ) {
@@ -229,6 +242,10 @@ public Response save(SaveRequest req) {
229
242
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
230
243
response .setStatus (OperationStatus .ERROR );
231
244
} finally {
245
+ if (ctx !=null ) {
246
+ ctx .measure .end ("save" );
247
+ METRICS .debug ("save" ,ctx .measure );
248
+ }
232
249
Error .pop ();
233
250
}
234
251
return response ;
@@ -251,8 +268,10 @@ public Response update(UpdateRequest req) {
251
268
LOGGER .debug ("update {}" , req .getEntityVersion ());
252
269
Error .push ("update(" + req .getEntityVersion ().toString () + ")" );
253
270
Response response = new Response (factory .getNodeFactory ());
271
+ OperationContext ctx =null ;
254
272
try {
255
- OperationContext ctx = newCtx (req , CRUDOperation .UPDATE );
273
+ ctx = newCtx (req , CRUDOperation .UPDATE );
274
+ ctx .measure .begin ("update" );
256
275
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
257
276
CompositeMetadata md = ctx .getTopLevelEntityMetadata ();
258
277
if (!md .getAccess ().getUpdate ().hasAccess (ctx .getCallerRoles ())) {
@@ -282,11 +301,13 @@ public Response update(UpdateRequest req) {
282
301
}
283
302
}
284
303
ctx .getHookManager ().queueMediatorHooks (ctx );
285
- LOGGER .debug ("# Updated" , updateResponse .getNumUpdated ());
304
+ ctx .measure .begin ("postProcessUpdatedDocs" );
305
+ LOGGER .debug ("# Updated" , updateResponse .getNumUpdated ());
286
306
response .setModifiedCount (updateResponse .getNumUpdated ());
287
307
response .setMatchCount (updateResponse .getNumMatched ());
288
308
List <DataError > dataErrors =setResponseResults (ctx ,req ,response );
289
309
response .getDataErrors ().addAll (dataErrors );
310
+ ctx .measure .end ("postProcessUpdatedDocs" );
290
311
if (ctx .hasErrors ()) {
291
312
ctx .setStatus (OperationStatus .ERROR );
292
313
} else if (!dataErrors .isEmpty ()) {
@@ -307,7 +328,11 @@ public Response update(UpdateRequest req) {
307
328
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
308
329
response .setStatus (OperationStatus .ERROR );
309
330
} finally {
310
- Error .pop ();
331
+ if (ctx !=null ) {
332
+ ctx .measure .end ("update" );
333
+ METRICS .debug ("update" ,ctx .measure );
334
+ }
335
+ Error .pop ();
311
336
}
312
337
return response ;
313
338
}
@@ -316,8 +341,10 @@ public Response delete(DeleteRequest req) {
316
341
LOGGER .debug ("delete {}" , req .getEntityVersion ());
317
342
Error .push ("delete(" + req .getEntityVersion ().toString () + ")" );
318
343
Response response = new Response (factory .getNodeFactory ());
344
+ OperationContext ctx =null ;
319
345
try {
320
- OperationContext ctx = newCtx (req , CRUDOperation .DELETE );
346
+ ctx = newCtx (req , CRUDOperation .DELETE );
347
+ ctx .measure .begin ("delete" );
321
348
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
322
349
CompositeMetadata md = ctx .getTopLevelEntityMetadata ();
323
350
if (!md .getAccess ().getDelete ().hasAccess (ctx .getCallerRoles ())) {
@@ -363,6 +390,10 @@ public Response delete(DeleteRequest req) {
363
390
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
364
391
response .setStatus (OperationStatus .ERROR );
365
392
} finally {
393
+ if (ctx !=null ) {
394
+ ctx .measure .end ("delete" );
395
+ METRICS .debug ("delete" ,ctx .measure );
396
+ }
366
397
Error .pop ();
367
398
}
368
399
return response ;
@@ -441,8 +472,10 @@ public Response find(FindRequest req) {
441
472
Error .push ("find(" + req .getEntityVersion ().toString () + ")" );
442
473
Response response = new Response (factory .getNodeFactory ());
443
474
response .setStatus (OperationStatus .ERROR );
475
+ OperationContext ctx =null ;
444
476
try {
445
- OperationContext ctx = newCtx (req , CRUDOperation .FIND );
477
+ ctx = newCtx (req , CRUDOperation .FIND );
478
+ ctx .measure .begin ("find" );
446
479
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
447
480
CompositeMetadata md = ctx .getTopLevelEntityMetadata ();
448
481
if (!md .getAccess ().getFind ().hasAccess (ctx .getCallerRoles ())) {
@@ -462,7 +495,11 @@ public Response find(FindRequest req) {
462
495
((CompositeFindImpl ) finder ).setParallelism (9 );
463
496
}
464
497
498
+ ctx .measure .begin ("finder.find" );
465
499
CRUDFindResponse result = finder .find (ctx , req .getCRUDFindRequest ());
500
+ ctx .measure .end ("finder.find" );
501
+
502
+ ctx .measure .begin ("postProcessFound" );
466
503
DocumentStream <DocCtx > docStream =ctx .getDocumentStream ();
467
504
List <ResultMetadata > rmd =new ArrayList <>();
468
505
response .setEntityData (factory .getNodeFactory ().arrayNode ());
@@ -479,12 +516,12 @@ public Response find(FindRequest req) {
479
516
}
480
517
docStream .close ();
481
518
response .setResultMetadata (rmd );
519
+ ctx .measure .end ("postProcessFound" );
482
520
if (!ctx .hasErrors ()) {
483
521
ctx .setStatus (OperationStatus .COMPLETE );
484
522
} else {
485
523
ctx .setStatus (OperationStatus .ERROR );
486
524
}
487
-
488
525
response .setMatchCount (result == null ? 0 : result .getSize ());
489
526
}
490
527
// call any queued up hooks (regardless of status)
@@ -501,6 +538,10 @@ public Response find(FindRequest req) {
501
538
LOGGER .debug ("Exception during find:{}" , e );
502
539
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
503
540
} finally {
541
+ if (ctx !=null ) {
542
+ ctx .measure .end ("find" );
543
+ METRICS .debug ("find" ,ctx .measure );
544
+ }
504
545
Error .pop ();
505
546
}
506
547
return response ;
@@ -676,6 +717,7 @@ protected OperationContext newCtx(Request request, CRUDOperation CRUDOperation)
676
717
*/
677
718
private void runBulkConstraintValidation (OperationContext ctx ) {
678
719
LOGGER .debug ("Bulk constraint validation" );
720
+ ctx .measure .begin ("runBulkConstraintValidation" );
679
721
EntityMetadata md = ctx .getTopLevelEntityMetadata ();
680
722
ConstraintValidator constraintValidator = factory .getConstraintValidator (md );
681
723
List <DocCtx > docs = ctx .getInputDocumentsWithoutErrors ();
@@ -693,9 +735,11 @@ private void runBulkConstraintValidation(OperationContext ctx) {
693
735
ctx .addErrors (errors );
694
736
}
695
737
LOGGER .debug ("Constraint validation complete" );
738
+ ctx .measure .end ("runBulkConstraintValidation" );
696
739
}
697
740
698
741
private void updatePredefinedFields (OperationContext ctx , CRUDController controller , String entity ) {
742
+ ctx .measure .begin ("updatePredefinedFields" );
699
743
for (JsonDoc doc : ctx .getInputDocuments ()) {
700
744
PredefinedFields .updateArraySizes (ctx .getTopLevelEntityMetadata (), factory .getNodeFactory (), doc );
701
745
JsonNode node = doc .get (OBJECT_TYPE_PATH );
@@ -706,6 +750,7 @@ private void updatePredefinedFields(OperationContext ctx, CRUDController control
706
750
}
707
751
controller .updatePredefinedFields (ctx , doc );
708
752
}
753
+ ctx .measure .end ("updatePredefinedFields" );
709
754
}
710
755
711
756
/**
0 commit comments