@@ -90,6 +90,7 @@ public class Mediator {
90
90
public static final String CRUD_MSG_PREFIX = "CRUD controller={}" ;
91
91
92
92
private static final Logger LOGGER = LoggerFactory .getLogger (Mediator .class );
93
+ private static final Logger METRICS = LoggerFactory .getLogger ("metrics." +Mediator .class .getName ());
93
94
94
95
private static final Path OBJECT_TYPE_PATH = new Path ("objectType" );
95
96
@@ -115,8 +116,10 @@ public Response insert(InsertionRequest req) {
115
116
LOGGER .debug ("insert {}" , req .getEntityVersion ());
116
117
Error .push ("insert(" + req .getEntityVersion ().toString () + ")" );
117
118
Response response = new Response (factory .getNodeFactory ());
119
+ OperationContext ctx =null ;
118
120
try {
119
- OperationContext ctx = newCtx (req , CRUDOperation .INSERT );
121
+ ctx = newCtx (req , CRUDOperation .INSERT );
122
+ ctx .measure .begin ("insert" );
120
123
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
121
124
EntityMetadata md = ctx .getTopLevelEntityMetadata ();
122
125
if (!md .getAccess ().getInsert ().hasAccess (ctx .getCallerRoles ())) {
@@ -131,12 +134,14 @@ public Response insert(InsertionRequest req) {
131
134
LOGGER .debug (CRUD_MSG_PREFIX , controller .getClass ().getName ());
132
135
controller .insert (ctx , req .getReturnFields ());
133
136
ctx .getHookManager ().queueMediatorHooks (ctx );
137
+ ctx .measure .begin ("postProcessInsertedDocs" );
134
138
List <JsonDoc > insertedDocuments = ctx .getOutputDocumentsWithoutErrors ();
135
139
if (insertedDocuments != null && !insertedDocuments .isEmpty ()) {
136
140
response .setEntityData (JsonDoc .listToDoc (applyRange (req , insertedDocuments ), factory .getNodeFactory ()));
137
141
response .setResultMetadata (ctx .getOutputDocumentMetadataWithoutErrors ());
138
142
response .setModifiedCount (insertedDocuments .size ());
139
143
}
144
+ ctx .measure .end ("postProcessInsertedDocs" );
140
145
if (!ctx .hasErrors () && !ctx .hasDocumentErrors ()
141
146
&& insertedDocuments != null && insertedDocuments .size () == ctx .getDocuments ().size ()) {
142
147
ctx .setStatus (OperationStatus .COMPLETE );
@@ -163,6 +168,10 @@ public Response insert(InsertionRequest req) {
163
168
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
164
169
response .setStatus (OperationStatus .ERROR );
165
170
} finally {
171
+ if (ctx !=null ) {
172
+ ctx .measure .end ("insert" );
173
+ METRICS .debug ("insert" ,ctx .measure );
174
+ }
166
175
Error .pop ();
167
176
}
168
177
return response ;
@@ -184,8 +193,10 @@ public Response save(SaveRequest req) {
184
193
LOGGER .debug ("save {}" , req .getEntityVersion ());
185
194
Error .push ("save(" + req .getEntityVersion ().toString () + ")" );
186
195
Response response = new Response (factory .getNodeFactory ());
196
+ OperationContext ctx =null ;
187
197
try {
188
- OperationContext ctx = newCtx (req , CRUDOperation .SAVE );
198
+ ctx = newCtx (req , CRUDOperation .SAVE );
199
+ ctx .measure .begin ("save" );
189
200
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
190
201
EntityMetadata md = ctx .getTopLevelEntityMetadata ();
191
202
if (!md .getAccess ().getUpdate ().hasAccess (ctx .getCallerRoles ())
@@ -201,12 +212,14 @@ public Response save(SaveRequest req) {
201
212
LOGGER .debug (CRUD_MSG_PREFIX , controller .getClass ().getName ());
202
213
controller .save (ctx , req .isUpsert (), req .getReturnFields ());
203
214
ctx .getHookManager ().queueMediatorHooks (ctx );
215
+ ctx .measure .begin ("postProcessSavedDocs" );
204
216
List <JsonDoc > updatedDocuments = ctx .getOutputDocumentsWithoutErrors ();
205
217
if (updatedDocuments != null && !updatedDocuments .isEmpty ()) {
206
218
response .setEntityData (JsonDoc .listToDoc (applyRange (req , updatedDocuments ), factory .getNodeFactory ()));
207
219
response .setResultMetadata (ctx .getOutputDocumentMetadataWithoutErrors ());
208
220
response .setModifiedCount (updatedDocuments .size ());
209
221
}
222
+ ctx .measure .end ("postProcessSavedDocs" );
210
223
if (!ctx .hasErrors () && !ctx .hasDocumentErrors ()
211
224
&& updatedDocuments != null && updatedDocuments .size () == ctx .getDocuments ().size ()) {
212
225
ctx .setStatus (OperationStatus .COMPLETE );
@@ -231,6 +244,10 @@ public Response save(SaveRequest req) {
231
244
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
232
245
response .setStatus (OperationStatus .ERROR );
233
246
} finally {
247
+ if (ctx !=null ) {
248
+ ctx .measure .end ("save" );
249
+ METRICS .debug ("save" ,ctx .measure );
250
+ }
234
251
Error .pop ();
235
252
}
236
253
return response ;
@@ -253,8 +270,10 @@ public Response update(UpdateRequest req) {
253
270
LOGGER .debug ("update {}" , req .getEntityVersion ());
254
271
Error .push ("update(" + req .getEntityVersion ().toString () + ")" );
255
272
Response response = new Response (factory .getNodeFactory ());
273
+ OperationContext ctx =null ;
256
274
try {
257
- OperationContext ctx = newCtx (req , CRUDOperation .UPDATE );
275
+ ctx = newCtx (req , CRUDOperation .UPDATE );
276
+ ctx .measure .begin ("update" );
258
277
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
259
278
CompositeMetadata md = ctx .getTopLevelEntityMetadata ();
260
279
if (!md .getAccess ().getUpdate ().hasAccess (ctx .getCallerRoles ())) {
@@ -284,14 +303,16 @@ public Response update(UpdateRequest req) {
284
303
}
285
304
}
286
305
ctx .getHookManager ().queueMediatorHooks (ctx );
287
- LOGGER .debug ("# Updated" , updateResponse .getNumUpdated ());
306
+ ctx .measure .begin ("postProcessUpdatedDocs" );
307
+ LOGGER .debug ("# Updated" , updateResponse .getNumUpdated ());
288
308
response .setModifiedCount (updateResponse .getNumUpdated ());
289
309
response .setMatchCount (updateResponse .getNumMatched ());
290
310
List <JsonDoc > updatedDocuments = ctx .getOutputDocumentsWithoutErrors ();
291
311
if (updatedDocuments != null && !updatedDocuments .isEmpty ()) {
292
312
response .setEntityData (JsonDoc .listToDoc (applyRange (req , updatedDocuments ), factory .getNodeFactory ()));
293
313
response .setResultMetadata (ctx .getOutputDocumentMetadataWithoutErrors ());
294
314
}
315
+ ctx .measure .end ("postProcessUpdatedDocs" );
295
316
if (ctx .hasErrors ()) {
296
317
ctx .setStatus (OperationStatus .ERROR );
297
318
} else if (ctx .hasDocumentErrors ()) {
@@ -314,7 +335,11 @@ public Response update(UpdateRequest req) {
314
335
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
315
336
response .setStatus (OperationStatus .ERROR );
316
337
} finally {
317
- Error .pop ();
338
+ if (ctx !=null ) {
339
+ ctx .measure .end ("update" );
340
+ METRICS .debug ("update" ,ctx .measure );
341
+ }
342
+ Error .pop ();
318
343
}
319
344
return response ;
320
345
}
@@ -323,8 +348,10 @@ public Response delete(DeleteRequest req) {
323
348
LOGGER .debug ("delete {}" , req .getEntityVersion ());
324
349
Error .push ("delete(" + req .getEntityVersion ().toString () + ")" );
325
350
Response response = new Response (factory .getNodeFactory ());
351
+ OperationContext ctx =null ;
326
352
try {
327
- OperationContext ctx = newCtx (req , CRUDOperation .DELETE );
353
+ ctx = newCtx (req , CRUDOperation .DELETE );
354
+ ctx .measure .begin ("delete" );
328
355
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
329
356
CompositeMetadata md = ctx .getTopLevelEntityMetadata ();
330
357
if (!md .getAccess ().getDelete ().hasAccess (ctx .getCallerRoles ())) {
@@ -371,6 +398,10 @@ public Response delete(DeleteRequest req) {
371
398
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
372
399
response .setStatus (OperationStatus .ERROR );
373
400
} finally {
401
+ if (ctx !=null ) {
402
+ ctx .measure .end ("delete" );
403
+ METRICS .debug ("delete" ,ctx .measure );
404
+ }
374
405
Error .pop ();
375
406
}
376
407
return response ;
@@ -446,8 +477,10 @@ public Response find(FindRequest req) {
446
477
Error .push ("find(" + req .getEntityVersion ().toString () + ")" );
447
478
Response response = new Response (factory .getNodeFactory ());
448
479
response .setStatus (OperationStatus .ERROR );
480
+ OperationContext ctx =null ;
449
481
try {
450
- OperationContext ctx = newCtx (req , CRUDOperation .FIND );
482
+ ctx = newCtx (req , CRUDOperation .FIND );
483
+ ctx .measure .begin ("find" );
451
484
response .setEntity (ctx .getTopLevelEntityName (),ctx .getTopLevelEntityVersion ());
452
485
CompositeMetadata md = ctx .getTopLevelEntityMetadata ();
453
486
if (!md .getAccess ().getFind ().hasAccess (ctx .getCallerRoles ())) {
@@ -467,8 +500,11 @@ public Response find(FindRequest req) {
467
500
((CompositeFindImpl ) finder ).setParallelism (9 );
468
501
}
469
502
503
+ ctx .measure .begin ("finder.find" );
470
504
CRUDFindResponse result = finder .find (ctx , req .getCRUDFindRequest ());
505
+ ctx .measure .end ("finder.find" );
471
506
507
+ ctx .measure .begin ("postProcessFound" );
472
508
List <JsonDoc > foundDocuments = ctx .getOutputDocumentsWithoutErrors ();
473
509
if (foundDocuments != null && foundDocuments .size () == ctx .getDocuments ().size ()) {
474
510
ctx .setStatus (OperationStatus .COMPLETE );
@@ -490,6 +526,7 @@ public Response find(FindRequest req) {
490
526
response .setEntityData (JsonDoc .listToDoc (resultList , factory .getNodeFactory ()));
491
527
response .setResultMetadata (resultMetadata );
492
528
}
529
+ ctx .measure .end ("postProcessFound" );
493
530
494
531
factory .getInterceptors ().callInterceptors (InterceptPoint .POST_MEDIATOR_FIND , ctx );
495
532
}
@@ -508,6 +545,10 @@ public Response find(FindRequest req) {
508
545
LOGGER .debug ("Exception during find:{}" , e );
509
546
response .getErrors ().add (Error .get (CrudConstants .ERR_CRUD , e ));
510
547
} finally {
548
+ if (ctx !=null ) {
549
+ ctx .measure .end ("find" );
550
+ METRICS .debug ("find" ,ctx .measure );
551
+ }
511
552
Error .pop ();
512
553
}
513
554
return response ;
@@ -687,6 +728,7 @@ protected OperationContext newCtx(Request request, CRUDOperation CRUDOperation)
687
728
*/
688
729
private void runBulkConstraintValidation (OperationContext ctx ) {
689
730
LOGGER .debug ("Bulk constraint validation" );
731
+ ctx .measure .begin ("runBulkConstraintValidation" );
690
732
EntityMetadata md = ctx .getTopLevelEntityMetadata ();
691
733
ConstraintValidator constraintValidator = factory .getConstraintValidator (md );
692
734
List <DocCtx > docs = ctx .getDocumentsWithoutErrors ();
@@ -704,9 +746,11 @@ private void runBulkConstraintValidation(OperationContext ctx) {
704
746
ctx .addErrors (errors );
705
747
}
706
748
LOGGER .debug ("Constraint validation complete" );
749
+ ctx .measure .end ("runBulkConstraintValidation" );
707
750
}
708
751
709
752
private void updatePredefinedFields (OperationContext ctx , CRUDController controller , String entity ) {
753
+ ctx .measure .begin ("updatePredefinedFields" );
710
754
for (JsonDoc doc : ctx .getDocuments ()) {
711
755
PredefinedFields .updateArraySizes (ctx .getTopLevelEntityMetadata (), factory .getNodeFactory (), doc );
712
756
JsonNode node = doc .get (OBJECT_TYPE_PATH );
@@ -717,6 +761,7 @@ private void updatePredefinedFields(OperationContext ctx, CRUDController control
717
761
}
718
762
controller .updatePredefinedFields (ctx , doc );
719
763
}
764
+ ctx .measure .end ("updatePredefinedFields" );
720
765
}
721
766
722
767
/**
0 commit comments