22// Licensed under the MIT License.
33package com .azure .cosmos .implementation ;
44
5- import com .azure .cosmos .ConnectionMode ;
65import com .azure .cosmos .CosmosException ;
76import com .azure .cosmos .implementation .apachecommons .lang .StringUtils ;
87import com .azure .cosmos .implementation .cpu .CpuMemoryMonitor ;
3029@ JsonSerialize (using = ClientSideRequestStatistics .ClientSideRequestStatisticsSerializer .class )
3130public class ClientSideRequestStatistics {
3231 private static final int MAX_SUPPLEMENTAL_REQUESTS_FOR_TO_STRING = 10 ;
33- private final DiagnosticsClientContext clientContext ;
3432 private final DiagnosticsClientContext diagnosticsClientContext ;
3533
3634 private List <StoreResponseStatistics > responseStatisticsList ;
@@ -50,7 +48,6 @@ public class ClientSideRequestStatistics {
5048
5149 public ClientSideRequestStatistics (DiagnosticsClientContext diagnosticsClientContext ) {
5250 this .diagnosticsClientContext = diagnosticsClientContext ;
53- this .clientContext = null ;
5451 this .requestStartTimeUTC = Instant .now ();
5552 this .requestEndTimeUTC = Instant .now ();
5653 this .responseStatisticsList = new ArrayList <>();
@@ -68,6 +65,14 @@ public Duration getDuration() {
6865 return Duration .between (requestStartTimeUTC , requestEndTimeUTC );
6966 }
7067
68+ public Instant getRequestStartTimeUTC () {
69+ return requestStartTimeUTC ;
70+ }
71+
72+ public DiagnosticsClientContext getDiagnosticsClientContext () {
73+ return diagnosticsClientContext ;
74+ }
75+
7176 public void recordResponse (RxDocumentServiceRequest request , StoreResult storeResult ) {
7277 Objects .requireNonNull (request , "request is required and cannot be null." );
7378 Instant responseTime = Instant .now ();
@@ -135,6 +140,7 @@ public void recordGatewayResponse(
135140 this .gatewayStatistics .requestCharge = storeResponse
136141 .getHeaderValue (HttpConstants .HttpHeaders .REQUEST_CHARGE );
137142 this .gatewayStatistics .requestTimeline = DirectBridgeInternal .getRequestTimeline (storeResponse );
143+ this .gatewayStatistics .partitionKeyRangeId = storeResponse .getPartitionKeyRangeId ();
138144 } else if (exception != null ) {
139145 this .gatewayStatistics .statusCode = exception .getStatusCode ();
140146 this .gatewayStatistics .subStatusCode = exception .getSubStatusCode ();
@@ -225,6 +231,22 @@ public RetryContext getRetryContext() {
225231 return retryContext ;
226232 }
227233
234+ public List <StoreResponseStatistics > getResponseStatisticsList () {
235+ return responseStatisticsList ;
236+ }
237+
238+ public List <StoreResponseStatistics > getSupplementalResponseStatisticsList () {
239+ return supplementalResponseStatisticsList ;
240+ }
241+
242+ public Map <String , AddressResolutionStatistics > getAddressResolutionStatistics () {
243+ return addressResolutionStatistics ;
244+ }
245+
246+ public GatewayStatistics getGatewayStatistics () {
247+ return gatewayStatistics ;
248+ }
249+
228250 public static class StoreResponseStatistics {
229251 @ JsonSerialize (using = StoreResult .StoreResultSerializer .class )
230252 StoreResult storeResult ;
@@ -234,6 +256,14 @@ public static class StoreResponseStatistics {
234256 ResourceType requestResourceType ;
235257 @ JsonSerialize
236258 OperationType requestOperationType ;
259+
260+ public StoreResult getStoreResult () {
261+ return storeResult ;
262+ }
263+
264+ public Instant getRequestResponseTimeUTC () {
265+ return requestResponseTimeUTC ;
266+ }
237267 }
238268
239269 private static class SystemInformation {
@@ -278,20 +308,7 @@ public void serialize(
278308 generator .writeStringField ("requestStartTimeUTC" , DiagnosticsInstantSerializer .fromInstant (statistics .requestStartTimeUTC ));
279309 generator .writeStringField ("requestEndTimeUTC" , DiagnosticsInstantSerializer .fromInstant (statistics .requestEndTimeUTC ));
280310 generator .writeObjectField ("responseStatisticsList" , statistics .responseStatisticsList );
281- int supplementalResponseStatisticsListCount = statistics .supplementalResponseStatisticsList .size ();
282- int initialIndex =
283- Math .max (supplementalResponseStatisticsListCount - MAX_SUPPLEMENTAL_REQUESTS_FOR_TO_STRING , 0 );
284- if (initialIndex != 0 ) {
285- List <StoreResponseStatistics > subList = statistics .supplementalResponseStatisticsList
286- .subList (initialIndex ,
287- supplementalResponseStatisticsListCount );
288- generator .writeObjectField ("supplementalResponseStatisticsList" , subList );
289- } else {
290- generator
291- .writeObjectField ("supplementalResponseStatisticsList" ,
292- statistics .supplementalResponseStatisticsList );
293- }
294-
311+ generator .writeObjectField ("supplementalResponseStatisticsList" , getCappedSupplementalResponseStatisticsList (statistics .supplementalResponseStatisticsList ));
295312 generator .writeObjectField ("addressResolutionStatistics" , statistics .addressResolutionStatistics );
296313 generator .writeObjectField ("regionsContacted" , statistics .regionsContacted );
297314 generator .writeObjectField ("retryContext" , statistics .retryContext );
@@ -300,17 +317,7 @@ public void serialize(
300317 generator .writeObjectField ("gatewayStatistics" , statistics .gatewayStatistics );
301318
302319 try {
303- SystemInformation systemInformation = new SystemInformation ();
304- Runtime runtime = Runtime .getRuntime ();
305- long totalMemory = runtime .totalMemory () / 1024 ;
306- long freeMemory = runtime .freeMemory () / 1024 ;
307- long maxMemory = runtime .maxMemory () / 1024 ;
308- systemInformation .usedMemory = totalMemory - freeMemory + " KB" ;
309- systemInformation .availableMemory = (maxMemory - (totalMemory - freeMemory )) + " KB" ;
310- systemInformation .availableProcessors = runtime .availableProcessors ();
311-
312- // TODO: other system related info also can be captured using a similar approach
313- systemInformation .systemCpuLoad = CpuMemoryMonitor .getCpuLoad ().toString ();
320+ SystemInformation systemInformation = fetchSystemInformation ();
314321 generator .writeObjectField ("systemInformation" , systemInformation );
315322 } catch (Exception e ) {
316323 // Error while evaluating system information, do nothing
@@ -321,7 +328,20 @@ public void serialize(
321328 }
322329 }
323330
324- private static class AddressResolutionStatistics {
331+ public static List <StoreResponseStatistics > getCappedSupplementalResponseStatisticsList (List <StoreResponseStatistics > supplementalResponseStatisticsList ) {
332+ int supplementalResponseStatisticsListCount = supplementalResponseStatisticsList .size ();
333+ int initialIndex =
334+ Math .max (supplementalResponseStatisticsListCount - MAX_SUPPLEMENTAL_REQUESTS_FOR_TO_STRING , 0 );
335+ if (initialIndex != 0 ) {
336+ List <StoreResponseStatistics > subList = supplementalResponseStatisticsList
337+ .subList (initialIndex ,
338+ supplementalResponseStatisticsListCount );
339+ return subList ;
340+ }
341+ return supplementalResponseStatisticsList ;
342+ }
343+
344+ public static class AddressResolutionStatistics {
325345 @ JsonSerialize (using = DiagnosticsInstantSerializer .class )
326346 Instant startTimeUTC ;
327347 @ JsonSerialize (using = DiagnosticsInstantSerializer .class )
@@ -336,16 +356,21 @@ private static class AddressResolutionStatistics {
336356 // indicating background addressResolution is still inflight
337357 @ JsonSerialize
338358 boolean inflightRequest = true ;
359+
360+ public Instant getStartTimeUTC () {
361+ return startTimeUTC ;
362+ }
339363 }
340364
341- private static class GatewayStatistics {
365+ public static class GatewayStatistics {
342366 String sessionToken ;
343367 OperationType operationType ;
344368 ResourceType resourceType ;
345369 int statusCode ;
346370 int subStatusCode ;
347371 String requestCharge ;
348372 RequestTimeline requestTimeline ;
373+ String partitionKeyRangeId ;
349374
350375 public String getSessionToken () {
351376 return sessionToken ;
@@ -374,5 +399,24 @@ public RequestTimeline getRequestTimeline() {
374399 public ResourceType getResourceType () {
375400 return resourceType ;
376401 }
402+
403+ public String getPartitionKeyRangeId () {
404+ return partitionKeyRangeId ;
405+ }
406+ }
407+
408+ public static SystemInformation fetchSystemInformation () {
409+ SystemInformation systemInformation = new SystemInformation ();
410+ Runtime runtime = Runtime .getRuntime ();
411+ long totalMemory = runtime .totalMemory () / 1024 ;
412+ long freeMemory = runtime .freeMemory () / 1024 ;
413+ long maxMemory = runtime .maxMemory () / 1024 ;
414+ systemInformation .usedMemory = totalMemory - freeMemory + " KB" ;
415+ systemInformation .availableMemory = (maxMemory - (totalMemory - freeMemory )) + " KB" ;
416+ systemInformation .availableProcessors = runtime .availableProcessors ();
417+
418+ // TODO: other system related info also can be captured using a similar approach
419+ systemInformation .systemCpuLoad = CpuMemoryMonitor .getCpuLoad ().toString ();
420+ return systemInformation ;
377421 }
378422}
0 commit comments