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

Diagnostics: Fixes duration for gateway diagnostics #2599

Merged
merged 7 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private async Task<HttpResponseMessage> SendHttpHelperAsync(
// If the default cancellation token is passed then use the timeout policy
using CancellationTokenSource cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cancellationTokenSource.CancelAfter(requestTimeout);

DateTime requestStartTime = DateTime.UtcNow;
try
{
HttpResponseMessage responseMessage = await this.ExecuteHttpHelperAsync(
Expand All @@ -254,7 +254,7 @@ private async Task<HttpResponseMessage> SendHttpHelperAsync(

if (clientSideRequestStatistics is ClientSideRequestStatisticsTraceDatum datum)
{
datum.RecordHttpResponse(requestMessage, responseMessage, resourceType, DateTime.UtcNow);
datum.RecordHttpResponse(requestMessage, responseMessage, resourceType, requestStartTime);
}

return responseMessage;
Expand All @@ -263,7 +263,7 @@ private async Task<HttpResponseMessage> SendHttpHelperAsync(
{
if (clientSideRequestStatistics is ClientSideRequestStatisticsTraceDatum datum)
{
datum.RecordHttpException(requestMessage, e, resourceType, DateTime.UtcNow);
datum.RecordHttpException(requestMessage, e, resourceType, requestStartTime);
}

bool isOutOfRetries = (DateTime.UtcNow - startDateTimeUtc) > timeoutPolicy.MaximumRetryTimeLimit || // Maximum of time for all retries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,40 @@ private async Task GatewayStoreModel_Exceptionless_UpdateSessionTokenOnKnownResp
}
}

[TestMethod]
public async Task GatewayStatsDurationTest()
{
bool failedOnce = false;
Func<HttpRequestMessage, Task<HttpResponseMessage>> sendFunc = async request =>
{
await Task.Delay(1000);
if (!failedOnce)
{
failedOnce = true;
throw new OperationCanceledException();
}

return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Response") };
};

HttpMessageHandler mockMessageHandler = new MockMessageHandler(sendFunc);
CosmosHttpClient cosmosHttpClient = MockCosmosUtil.CreateCosmosHttpClient(() => new HttpClient(mockMessageHandler),
DocumentClientEventSource.Instance);
Tracing.TraceData.ClientSideRequestStatisticsTraceDatum clientSideRequestStatistics = new Tracing.TraceData.ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow);

await cosmosHttpClient.SendHttpAsync(() => new ValueTask<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "http://someuri.com")),
ResourceType.Document,
HttpTimeoutPolicyDefault.Instance,
clientSideRequestStatistics,
CancellationToken.None);

Assert.AreEqual(clientSideRequestStatistics.HttpResponseStatisticsList.Count, 2);
Assert.IsTrue(clientSideRequestStatistics.HttpResponseStatisticsList[0].Duration.TotalMilliseconds >= 1000);
Assert.IsTrue(clientSideRequestStatistics.HttpResponseStatisticsList[1].Duration.TotalMilliseconds >= 1000);
Assert.IsTrue(clientSideRequestStatistics.HttpResponseStatisticsList[0].RequestStartTime <
clientSideRequestStatistics.HttpResponseStatisticsList[1].RequestStartTime);
}

[TestMethod]
[Owner("maquaran")]
// Validates that if its a master resource, we don't update the Session Token, even though the status code would be one of the included ones
Expand Down