Skip to content

Commit

Permalink
Wrapped Client Side Request Statistics (Azure#216)
Browse files Browse the repository at this point in the history
* Wrapped Client Side Request Statistics in Cosmos Response Diagnostic Statistics. Exposed only toString and request latency

* Code review comments for Cosmos Response Diagnostics

* Wrapped Query Metrics Map in Feed Response Diagnostics. Exposed toString through Feed response diagnostics

* Reducing scope of setter to package private
  • Loading branch information
kushagraThapar authored and christopheranderson committed Jun 28, 2019
1 parent 9c73dd9 commit 42665d2
Show file tree
Hide file tree
Showing 27 changed files with 255 additions and 109 deletions.
42 changes: 40 additions & 2 deletions sdk/src/main/java/com/azure/data/cosmos/BridgeInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import com.azure.data.cosmos.internal.QueryMetrics;
import com.azure.data.cosmos.internal.ReplicationPolicy;
import com.azure.data.cosmos.internal.ResourceResponse;
import com.azure.data.cosmos.internal.RxDocumentServiceRequest;
import com.azure.data.cosmos.internal.RxDocumentServiceResponse;
import com.azure.data.cosmos.internal.StoredProcedureResponse;
import com.azure.data.cosmos.internal.Strings;
import com.azure.data.cosmos.internal.directconnectivity.StoreResult;
import com.azure.data.cosmos.internal.query.metrics.ClientSideMetrics;
import com.azure.data.cosmos.internal.routing.PartitionKeyInternal;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -43,6 +45,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

import static com.azure.data.cosmos.internal.Constants.QueryExecutionContext.INCREMENTAL_FEED_HEADER_VALUE;
Expand Down Expand Up @@ -332,8 +335,8 @@ public static Object getValue(JsonNode value) {
return JsonSerializable.getValue(value);
}

public static CosmosClientException setClientSideRequestStatistics(CosmosClientException cosmosClientException, ClientSideRequestStatistics clientSideRequestStatistics) {
return cosmosClientException.clientSideRequestStatistics(clientSideRequestStatistics);
public static CosmosClientException setCosmosResponseDiagnostics(CosmosClientException cosmosClientException, CosmosResponseDiagnostics cosmosResponseDiagnostics) {
return cosmosClientException.cosmosResponseDiagnostics(cosmosResponseDiagnostics);
}

public static CosmosClientException createCosmosClientException(int statusCode) {
Expand Down Expand Up @@ -367,4 +370,39 @@ public static CosmosClientException createCosmosClientException(String message,
cosmosClientException.resourceAddress = resourceAddress;
return cosmosClientException;
}

public static CosmosResponseDiagnostics createCosmosResponseDiagnostics() {
return new CosmosResponseDiagnostics();
}

public static void recordResponse(CosmosResponseDiagnostics cosmosResponseDiagnostics,
RxDocumentServiceRequest request, StoreResult storeResult) {
cosmosResponseDiagnostics.clientSideRequestStatistics().recordResponse(request, storeResult);
}

public static String recordAddressResolutionStart(CosmosResponseDiagnostics cosmosResponseDiagnostics,
URI targetEndpoint) {
return cosmosResponseDiagnostics.clientSideRequestStatistics().recordAddressResolutionStart(targetEndpoint);
}

public static void recordAddressResolutionEnd(CosmosResponseDiagnostics cosmosResponseDiagnostics,
String identifier) {
cosmosResponseDiagnostics.clientSideRequestStatistics().recordAddressResolutionEnd(identifier);
}

public static List<URI> getContactedReplicas(CosmosResponseDiagnostics cosmosResponseDiagnostics) {
return cosmosResponseDiagnostics.clientSideRequestStatistics().getContactedReplicas();
}

public static void setContactedReplicas(CosmosResponseDiagnostics cosmosResponseDiagnostics, List<URI> contactedReplicas) {
cosmosResponseDiagnostics.clientSideRequestStatistics().setContactedReplicas(contactedReplicas);
}

public static Set<URI> getFailedReplicas(CosmosResponseDiagnostics cosmosResponseDiagnostics) {
return cosmosResponseDiagnostics.clientSideRequestStatistics().getFailedReplicas();
}

public static ConcurrentMap<String, QueryMetrics> queryMetricsFromFeedResponse(FeedResponse feedResponse) {
return feedResponse.queryMetrics();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import java.util.Map;
import java.util.Set;

public class ClientSideRequestStatistics {
class ClientSideRequestStatistics {

private final static int MAX_SUPPLEMENTAL_REQUESTS_FOR_TO_STRING = 10;

Expand All @@ -61,7 +61,7 @@ public class ClientSideRequestStatistics {
private Set<URI> failedReplicas;
private Set<URI> regionsContacted;

public ClientSideRequestStatistics() {
ClientSideRequestStatistics() {
this.requestStartTime = ZonedDateTime.now(ZoneOffset.UTC);
this.requestEndTime = ZonedDateTime.now(ZoneOffset.UTC);
this.responseStatisticsList = new ArrayList<>();
Expand All @@ -72,7 +72,7 @@ public ClientSideRequestStatistics() {
this.regionsContacted = new HashSet<>();
}

public Duration getRequestLatency() {
Duration getRequestLatency() {
return Duration.between(requestStartTime, requestEndTime);
}

Expand All @@ -81,7 +81,7 @@ private boolean isCPUOverloaded() {
return false;
}

public void recordResponse(RxDocumentServiceRequest request, StoreResult storeResult) {
void recordResponse(RxDocumentServiceRequest request, StoreResult storeResult) {
ZonedDateTime responseTime = ZonedDateTime.now(ZoneOffset.UTC);

StoreResponseStatistics storeResponseStatistics = new StoreResponseStatistics();
Expand Down Expand Up @@ -117,7 +117,7 @@ public void recordResponse(RxDocumentServiceRequest request, StoreResult storeRe
}
}

public String recordAddressResolutionStart(URI targetEndpoint) {
String recordAddressResolutionStart(URI targetEndpoint) {
String identifier = Utils.randomUUID().toString();

AddressResolutionStatistics resolutionStatistics = new AddressResolutionStatistics();
Expand All @@ -133,7 +133,7 @@ public String recordAddressResolutionStart(URI targetEndpoint) {
return identifier;
}

public void recordAddressResolutionEnd(String identifier) {
void recordAddressResolutionEnd(String identifier) {
if (StringUtils.isEmpty(identifier)) {
return;
}
Expand Down Expand Up @@ -206,27 +206,27 @@ public String toString() {
return StringUtils.EMPTY;
}

public List<URI> getContactedReplicas() {
List<URI> getContactedReplicas() {
return contactedReplicas;
}

public void setContactedReplicas(List<URI> contactedReplicas) {
void setContactedReplicas(List<URI> contactedReplicas) {
this.contactedReplicas = contactedReplicas;
}

public Set<URI> getFailedReplicas() {
Set<URI> getFailedReplicas() {
return failedReplicas;
}

public void setFailedReplicas(Set<URI> failedReplicas) {
void setFailedReplicas(Set<URI> failedReplicas) {
this.failedReplicas = failedReplicas;
}

public Set<URI> getRegionsContacted() {
Set<URI> getRegionsContacted() {
return regionsContacted;
}

public void setRegionsContacted(Set<URI> regionsContacted) {
void setRegionsContacted(Set<URI> regionsContacted) {
this.regionsContacted = regionsContacted;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class CosmosClientException extends Exception {
private final int statusCode;
private final Map<String, String> responseHeaders;

private ClientSideRequestStatistics clientSideRequestStatistics;
private CosmosResponseDiagnostics cosmosResponseDiagnostics;
private CosmosError cosmosError;

long lsn;
Expand Down Expand Up @@ -141,10 +141,10 @@ public class CosmosClientException extends Exception {

@Override
public String getMessage() {
if (clientSideRequestStatistics == null) {
if (cosmosResponseDiagnostics == null) {
return innerErrorMessage();
}
return innerErrorMessage() + ", " + clientSideRequestStatistics.toString();
return innerErrorMessage() + ", " + cosmosResponseDiagnostics.toString();
}

/**
Expand Down Expand Up @@ -250,16 +250,16 @@ String getResourceAddress() {
}

/**
* Gets the Client side request statistics associated with this exception.
* Gets the Cosmos Response Diagnostic Statistics associated with this exception.
*
* @return Client side request statistics associated with this exception.
* @return Cosmos Response Diagnostic Statistics associated with this exception.
*/
public ClientSideRequestStatistics clientSideRequestStatistics() {
return clientSideRequestStatistics;
public CosmosResponseDiagnostics cosmosResponseDiagnostics() {
return cosmosResponseDiagnostics;
}

CosmosClientException clientSideRequestStatistics(ClientSideRequestStatistics clientSideRequestStatistics) {
this.clientSideRequestStatistics = clientSideRequestStatistics;
CosmosClientException cosmosResponseDiagnostics(CosmosResponseDiagnostics cosmosResponseDiagnostics) {
this.cosmosResponseDiagnostics = cosmosResponseDiagnostics;
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/src/main/java/com/azure/data/cosmos/CosmosResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public Map<String, String> responseHeaders() {
*
* @return diagnostics information for the current request to Azure Cosmos DB service.
*/
public String requestDiagnosticsString() {
return resourceResponseWrapper.getRequestDiagnosticsString();
public CosmosResponseDiagnostics cosmosResponseDiagnosticsString() {
return resourceResponseWrapper.getCosmosResponseDiagnostics();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License (MIT)
* Copyright (c) 2018 Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.azure.data.cosmos;

import java.time.Duration;

/**
* This class represents response diagnostic statistics associated with a request to Azure Cosmos DB
*/
public class CosmosResponseDiagnostics {

private ClientSideRequestStatistics clientSideRequestStatistics;

CosmosResponseDiagnostics() {
this.clientSideRequestStatistics = new ClientSideRequestStatistics();
}

ClientSideRequestStatistics clientSideRequestStatistics() {
return clientSideRequestStatistics;
}

CosmosResponseDiagnostics clientSideRequestStatistics(ClientSideRequestStatistics clientSideRequestStatistics) {
this.clientSideRequestStatistics = clientSideRequestStatistics;
return this;
}

/**
* Retrieves Response Diagnostic String
* @return Response Diagnostic String
*/
@Override
public String toString() {
return this.clientSideRequestStatistics.toString();
}

/**
* Retrieves latency related to the completion of the request
* @return request completion latency
*/
public Duration requestLatency() {
return this.clientSideRequestStatistics.getRequestLatency();
}
}
13 changes: 9 additions & 4 deletions sdk/src/main/java/com/azure/data/cosmos/FeedResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class FeedResponse<T> {
boolean nochanges;
private final ConcurrentMap<String, QueryMetrics> queryMetricsMap;
private final String DefaultPartition = "0";
private final FeedResponseDiagnostics feedResponseDiagnostics;

FeedResponse(List<T> results, Map<String, String> headers) {
this(results, headers, false, false, new ConcurrentHashMap<>());
Expand Down Expand Up @@ -73,6 +74,7 @@ private FeedResponse(
this.useEtagAsContinuation = useEtagAsContinuation;
this.nochanges = nochanges;
this.queryMetricsMap = new ConcurrentHashMap<>(queryMetricsMap);
this.feedResponseDiagnostics = new FeedResponseDiagnostics(queryMetricsMap);
}

/**
Expand Down Expand Up @@ -309,11 +311,14 @@ private String queryMetricsString(){
}

/**
* Gets the QueryMetrics for each partition.
*
* @return the QueryMetrics for each partition.
* Gets the feed response diagnostics
* @return Feed response diagnostics
*/
public ConcurrentMap<String, QueryMetrics> queryMetrics() {
public FeedResponseDiagnostics feedResponseDiagnostics() {
return this.feedResponseDiagnostics;
}

ConcurrentMap<String, QueryMetrics> queryMetrics() {
if (queryMetricsMap != null && !queryMetricsMap.isEmpty()) {
return queryMetricsMap;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.azure.data.cosmos;

import com.azure.data.cosmos.internal.QueryMetrics;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class FeedResponseDiagnostics {

private Map<String, QueryMetrics> queryMetricsMap;

FeedResponseDiagnostics(Map<String, QueryMetrics> queryMetricsMap) {
this.queryMetricsMap = queryMetricsMap;
}

Map<String, QueryMetrics> queryMetricsMap() {
return queryMetricsMap;
}

FeedResponseDiagnostics queryMetricsMap(Map<String, QueryMetrics> queryMetricsMap) {
this.queryMetricsMap = queryMetricsMap;
return this;
}

/**
* Returns the textual representation of feed response metrics
* @return Textual representation of feed response metrics
*/
@Override
public String toString() {
if (queryMetricsMap == null || queryMetricsMap.isEmpty()) {
return StringUtils.EMPTY;
}
StringBuilder stringBuilder = new StringBuilder();
queryMetricsMap.forEach((key, value) -> stringBuilder.append(key).append("=").append(value.toString()).append("\n"));
return stringBuilder.toString();
}
}
Loading

0 comments on commit 42665d2

Please sign in to comment.