Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics not getting populated correctly on CosmosException #846

Merged
merged 11 commits into from
Sep 27, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,61 @@ public void RecordAddressResolutionEnd(string identifier)
}
}

public override string ToString()
ealsur marked this conversation as resolved.
Show resolved Hide resolved
{
StringBuilder sb = new StringBuilder();

//need to lock in case of concurrent operations. this should be extremely rare since ToString()
//should only be called at the end of request.
lock (this.lockObject)
{
//first trace request start time, as well as total non-head/headfeed requests made.
sb.AppendLine(string.Format(CultureInfo.InvariantCulture,
"RequestStartTime: {0}, RequestEndTime: {1}, Number of regions attempted: {2}",
this.requestStartTime.ToString("o", System.Globalization.CultureInfo.InvariantCulture),
this.requestEndTime.ToString("o", System.Globalization.CultureInfo.InvariantCulture),
this.RegionsContacted.Count == 0 ? 1 : this.RegionsContacted.Count));

//take all responses here - this should be limited in number and each one contains relevant information.
foreach (StoreResponseStatistics item in this.responseStatisticsList)
{
sb.AppendLine(item.ToString());
}

//take all responses here - this should be limited in number and each one is important.
foreach (AddressResolutionStatistics item in this.addressResolutionStatistics.Values)
{
sb.AppendLine(item.ToString());
}

//only take last 10 responses from this list - this has potential of having large number of entries.
//since this is for establishing consistency, we can make do with the last responses to paint a meaningful picture.
int supplementalResponseStatisticsListCount = this.supplementalResponseStatisticsList.Count;
int initialIndex = Math.Max(supplementalResponseStatisticsListCount - CosmosClientSideRequestStatistics.MaxSupplementalRequestsForToString, 0);

if (initialIndex != 0)
{
sb.AppendLine(string.Format(CultureInfo.InvariantCulture,
" -- Displaying only the last {0} head/headfeed requests. Total head/headfeed requests: {1}",
CosmosClientSideRequestStatistics.MaxSupplementalRequestsForToString,
supplementalResponseStatisticsListCount));
}

for (int i = initialIndex; i < supplementalResponseStatisticsListCount; i++)
{
sb.AppendLine(this.supplementalResponseStatisticsList[i].ToString());
}
}

string requestStatsString = sb.ToString();
if (requestStatsString.Length > 0)
{
return Environment.NewLine + requestStatsString;
}

return string.Empty;
}

internal struct StoreResponseStatistics
{
public DateTime RequestResponseTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,24 @@ public async Task VerifySessionTokenPassThrough()
Assert.AreEqual(sessionToken, readResponse.Headers.Session);
}

[TestMethod]
public async Task VerifySessionNotFoundStatistics()
{
ToDoActivity temp = ToDoActivity.CreateRandomToDoActivity("TBD");

string invalidSessionToken = "0:-1#20";

try
{
ItemResponse<ToDoActivity> readResponse = await this.Container.ReadItemAsync<ToDoActivity>(temp.id, new Cosmos.PartitionKey(temp.status), new ItemRequestOptions() { SessionToken = invalidSessionToken });
Assert.Fail("Should had thrown ReadSessionNotAvailable");
}
catch (CosmosException cosmosException)
{
Assert.IsTrue(cosmosException.Message.Contains("RequestStartTime:"));
}
}

/// <summary>
/// Stateless container re-create test.
/// Create two client instances and do meta data operations through a single client
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#814](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/814) Ability to limit to configured endpoint only
- [#822](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/822) GROUP BY query support.

### Fixed

- [#846](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/846) Statistics not getting populated correctly on CosmosException.

## <a name="3.2.0"/> [3.2.0](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.2.0) - 2019-09-17

### Added
Expand Down