public void VerifyNegativeCosmosQueryResponseStream() { string contianerRid = "mockContainerRid"; string errorMessage = "TestErrorMessage"; string activityId = "TestActivityId"; double requestCharge = 42.42; CosmosDiagnosticsContext diagnostics = new CosmosDiagnosticsContextCore(); CosmosException cosmosException = CosmosExceptionFactory.CreateBadRequestException(errorMessage, diagnosticsContext: diagnostics); diagnostics.GetOverallScope().Dispose(); QueryResponse queryResponse = QueryResponse.CreateFailure( statusCode: HttpStatusCode.NotFound, cosmosException: cosmosException, requestMessage: null, responseHeaders: new CosmosQueryResponseMessageHeaders( null, null, ResourceType.Document, contianerRid) { RequestCharge = requestCharge, ActivityId = activityId }, diagnostics: diagnostics); Assert.AreEqual(HttpStatusCode.NotFound, queryResponse.StatusCode); Assert.AreEqual(cosmosException.Message, queryResponse.ErrorMessage); Assert.AreEqual(requestCharge, queryResponse.Headers.RequestCharge); Assert.AreEqual(activityId, queryResponse.Headers.ActivityId); Assert.AreEqual(diagnostics, queryResponse.DiagnosticsContext); Assert.IsNull(queryResponse.Content); }
public void ValidateDiagnosticsAppendContext() { CosmosDiagnosticsContext cosmosDiagnostics = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsAppendContext), "MyCustomUserAgentString"); CosmosDiagnosticsContext cosmosDiagnostics2; using (cosmosDiagnostics.GetOverallScope()) { // Test all the different operations on diagnostics context using (cosmosDiagnostics.CreateScope("ValidateScope")) { Thread.Sleep(TimeSpan.FromSeconds(2)); } cosmosDiagnostics2 = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsAppendContext), "MyCustomUserAgentString"); cosmosDiagnostics2.GetOverallScope().Dispose(); using (cosmosDiagnostics.CreateScope("CosmosDiagnostics2Scope")) { Thread.Sleep(TimeSpan.FromMilliseconds(100)); } cosmosDiagnostics2.AddDiagnosticsInternal(cosmosDiagnostics); } string diagnostics = cosmosDiagnostics2.ToString(); Assert.IsTrue(diagnostics.Contains("MyCustomUserAgentString")); Assert.IsTrue(diagnostics.Contains("ValidateScope")); Assert.IsTrue(diagnostics.Contains("CosmosDiagnostics2Scope")); }
public void ValidateClientSideRequestStatisticsToString() { // Verify that API using the interface get the older v2 string CosmosDiagnosticsContext diagnosticsContext = new CosmosDiagnosticsContextCore(); diagnosticsContext.GetOverallScope().Dispose(); CosmosClientSideRequestStatistics clientSideRequestStatistics = new CosmosClientSideRequestStatistics(diagnosticsContext); string noInfo = clientSideRequestStatistics.ToString(); Assert.IsNotNull(noInfo); StringBuilder stringBuilder = new StringBuilder(); clientSideRequestStatistics.AppendToBuilder(stringBuilder); string noInfoStringBuilder = stringBuilder.ToString(); Assert.IsNotNull(noInfoStringBuilder); Assert.AreEqual(noInfo, noInfoStringBuilder); string id = clientSideRequestStatistics.RecordAddressResolutionStart(new Uri("https://testuri")); clientSideRequestStatistics.RecordAddressResolutionEnd(id); Documents.DocumentServiceRequest documentServiceRequest = new Documents.DocumentServiceRequest( operationType: Documents.OperationType.Read, resourceIdOrFullName: null, resourceType: Documents.ResourceType.Database, body: null, headers: null, isNameBased: false, authorizationTokenType: Documents.AuthorizationTokenType.PrimaryMasterKey); clientSideRequestStatistics.RecordRequest(documentServiceRequest); clientSideRequestStatistics.RecordResponse( documentServiceRequest, new Documents.StoreResult( storeResponse: new Documents.StoreResponse(), exception: null, partitionKeyRangeId: "PkRange", lsn: 42, quorumAckedLsn: 4242, requestCharge: 9000.42, currentReplicaSetSize: 3, currentWriteQuorum: 4, isValid: true, storePhysicalAddress: null, globalCommittedLSN: 2, numberOfReadRegions: 1, itemLSN: 5, sessionToken: null, usingLocalLSN: true)); string statistics = clientSideRequestStatistics.ToString(); Assert.IsTrue(statistics.Contains("\"UserAgent\":\"cosmos-netstandard-sdk")); Assert.IsTrue(statistics.Contains("UsingLocalLSN: True, TransportException: null")); Assert.IsTrue(statistics.Contains("AddressResolutionStatistics\",\"StartTimeUtc")); }
public void ValidateDiagnosticsAppendContext() { CosmosDiagnosticsContext cosmosDiagnostics = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsAppendContext), "MyCustomUserAgentString"); CosmosDiagnosticsContext cosmosDiagnostics2; using (cosmosDiagnostics.GetOverallScope()) { bool insertIntoDiagnostics1 = true; bool isInsertDiagnostics = false; // Start a background thread and ensure that no exception occurs even if items are getting added to the context // when 2 contexts are appended. Task.Run(() => { isInsertDiagnostics = true; CosmosSystemInfo cosmosSystemInfo = new CosmosSystemInfo( cpuLoadHistory: new Documents.Rntbd.CpuLoadHistory(new List <Documents.Rntbd.CpuLoad>().AsReadOnly(), TimeSpan.FromSeconds(1))); while (insertIntoDiagnostics1) { Task.Delay(TimeSpan.FromMilliseconds(1)).Wait(); cosmosDiagnostics.AddDiagnosticsInternal(cosmosSystemInfo); } }); while (!isInsertDiagnostics) { Task.Delay(TimeSpan.FromMilliseconds(5)).Wait(); } // Test all the different operations on diagnostics context using (cosmosDiagnostics.CreateScope("ValidateScope")) { Thread.Sleep(TimeSpan.FromMilliseconds(3)); } cosmosDiagnostics2 = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsAppendContext), "MyCustomUserAgentString"); cosmosDiagnostics2.GetOverallScope().Dispose(); using (cosmosDiagnostics.CreateScope("CosmosDiagnostics2Scope")) { Thread.Sleep(TimeSpan.FromMilliseconds(3)); } cosmosDiagnostics2.AddDiagnosticsInternal(cosmosDiagnostics); // Stop the background inserts insertIntoDiagnostics1 = false; } string diagnostics = cosmosDiagnostics2.ToString(); Assert.IsTrue(diagnostics.Contains("MyCustomUserAgentString")); Assert.IsTrue(diagnostics.Contains("ValidateScope")); Assert.IsTrue(diagnostics.Contains("CosmosDiagnostics2Scope")); }
public void ValidateDiagnosticsAppendContextConcurrentCalls() { int threadCount = 10; int itemCountPerThread = 100000; ConcurrentStack <Exception> concurrentStack = new ConcurrentStack <Exception>(); CosmosDiagnosticsContext cosmosDiagnostics = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsAppendContext), "MyCustomUserAgentString"); using (cosmosDiagnostics.GetOverallScope()) { // Test all the different operations on diagnostics context using (cosmosDiagnostics.CreateScope("ValidateScope")) { Thread.Sleep(TimeSpan.FromSeconds(2)); } List <Thread> threads = new List <Thread>(threadCount); for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(() => this.AddDiagnosticsInBackgroundLoop( itemCountPerThread, cosmosDiagnostics, concurrentStack)); thread.Start(); threads.Add(thread); } foreach (Thread thread in threads) { thread.Join(); } } Assert.AreEqual(0, concurrentStack.Count, $"Exceptions count: {concurrentStack.Count} Exceptions: {string.Join(';', concurrentStack)}"); int count = cosmosDiagnostics.Count(); Assert.AreEqual((threadCount * itemCountPerThread) + 1, count); }
private void AddDiagnosticsInBackgroundLoop( int count, CosmosDiagnosticsContext cosmosDiagnostics, ConcurrentStack <Exception> concurrentStack) { CosmosDiagnosticsContext cosmosDiagnostics2 = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsAppendContext), "MyCustomUserAgentString"); Random random = new Random(); cosmosDiagnostics2.GetOverallScope().Dispose(); for (int i = 0; i < count; i++) { try { cosmosDiagnostics.AddDiagnosticsInternal(cosmosDiagnostics2); } catch (Exception e) { concurrentStack.Append(e); } } }
public void ValidateRetriableRequestsCount() { CosmosDiagnosticsContext cosmosDiagnostics = new CosmosDiagnosticsContextCore( nameof(ValidateRetriableRequestsCount), "cosmos-netstandard-sdk"); cosmosDiagnostics.GetOverallScope().Dispose(); using (cosmosDiagnostics.GetOverallScope()) { // Test all the different operations on diagnostics context using (cosmosDiagnostics.CreateScope("ValidateScope")) { Assert.AreEqual(0, cosmosDiagnostics.GetRetriableResponseCount()); Assert.AreEqual(0, cosmosDiagnostics.GetFailedResponseCount()); Assert.AreEqual(0, cosmosDiagnostics.GetTotalResponseCount()); cosmosDiagnostics.AddDiagnosticsInternal(new PointOperationStatistics( new Guid("692ab2f2-41ba-486b-aad7-8c7c6c52379f").ToString(), (HttpStatusCode)429, Documents.SubStatusCodes.Unknown, DateTime.UtcNow, 42, null, HttpMethod.Get, "http://MockUri.com", null, null)); Assert.AreEqual(1, cosmosDiagnostics.GetRetriableResponseCount()); Assert.AreEqual(1, cosmosDiagnostics.GetFailedResponseCount()); Assert.AreEqual(1, cosmosDiagnostics.GetTotalResponseCount()); cosmosDiagnostics.AddDiagnosticsInternal( new StoreResponseStatistics( DateTime.UtcNow, DateTime.UtcNow, StoreResult.CreateStoreResult( new StoreResponse { Status = 449 }, null, false, false), ResourceType.Document, OperationType.Delete, new Uri("http://MockUri.com"))); Assert.AreEqual(2, cosmosDiagnostics.GetRetriableResponseCount()); Assert.AreEqual(2, cosmosDiagnostics.GetFailedResponseCount()); Assert.AreEqual(2, cosmosDiagnostics.GetTotalResponseCount()); cosmosDiagnostics.AddDiagnosticsInternal( new StoreResponseStatistics( DateTime.UtcNow, DateTime.UtcNow, StoreResult.CreateStoreResult( new StoreResponse { Status = 503 }, null, false, false), ResourceType.Document, OperationType.Delete, new Uri("http://MockUri.com"))); Assert.AreEqual(2, cosmosDiagnostics.GetRetriableResponseCount()); Assert.AreEqual(3, cosmosDiagnostics.GetFailedResponseCount()); Assert.AreEqual(3, cosmosDiagnostics.GetTotalResponseCount()); } Assert.AreEqual(2, cosmosDiagnostics.GetRetriableResponseCount()); Assert.AreEqual(3, cosmosDiagnostics.GetFailedResponseCount()); Assert.AreEqual(3, cosmosDiagnostics.GetTotalResponseCount()); } Assert.AreEqual(2, cosmosDiagnostics.GetRetriableResponseCount()); Assert.AreEqual(3, cosmosDiagnostics.GetFailedResponseCount()); Assert.AreEqual(3, cosmosDiagnostics.GetTotalResponseCount()); CosmosDiagnosticsContext cosmosDiagnostics2 = new CosmosDiagnosticsContextCore( nameof(ValidateRetriableRequestsCount), "cosmos-netstandard-sdk"); cosmosDiagnostics2.AddDiagnosticsInternal(cosmosDiagnostics); Assert.AreEqual(2, cosmosDiagnostics2.GetRetriableResponseCount()); Assert.AreEqual(3, cosmosDiagnostics2.GetFailedResponseCount()); Assert.AreEqual(3, cosmosDiagnostics2.GetTotalResponseCount()); }
public void ValidateDiagnosticsContext() { CosmosDiagnosticsContext cosmosDiagnostics = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsContext), "cosmos-netstandard-sdk"); cosmosDiagnostics.GetOverallScope().Dispose(); string diagnostics = cosmosDiagnostics.ToString(); //Test the default user agent string JObject jObject = JObject.Parse(diagnostics); JToken summary = jObject["Summary"]; Assert.IsTrue(summary["UserAgent"].ToString().Contains("cosmos-netstandard-sdk"), "Diagnostics should have user agent string"); cosmosDiagnostics = new CosmosDiagnosticsContextCore( nameof(ValidateDiagnosticsContext), "MyCustomUserAgentString"); using (cosmosDiagnostics.GetOverallScope()) { // Test all the different operations on diagnostics context Thread.Sleep(TimeSpan.FromSeconds(1)); using (cosmosDiagnostics.CreateScope("ValidateScope")) { Thread.Sleep(TimeSpan.FromSeconds(1)); cosmosDiagnostics.AddDiagnosticsInternal(new PointOperationStatistics( new Guid("692ab2f2-41ba-486b-aad7-8c7c6c52379f").ToString(), (HttpStatusCode)429, Documents.SubStatusCodes.Unknown, DateTime.UtcNow, 42, null, HttpMethod.Get, "http://MockUri.com", null, null)); } using (cosmosDiagnostics.CreateScope("SuccessScope")) { cosmosDiagnostics.AddDiagnosticsInternal(new PointOperationStatistics( new Guid("de09baab-71a4-4897-a163-470711c93ed3").ToString(), HttpStatusCode.OK, Documents.SubStatusCodes.Unknown, DateTime.UtcNow, 42, null, HttpMethod.Get, "http://MockUri.com", null, null)); } } string result = cosmosDiagnostics.ToString(); string regex = @"\{""DiagnosticVersion"":""2"",""Summary"":\{""StartUtc"":"".+Z"",""TotalElapsedTimeInMs"":.+,""UserAgent"":""MyCustomUserAgentString"",""TotalRequestCount"":2,""FailedRequestCount"":1\},""Context"":\[\{""Id"":""ValidateScope"",""ElapsedTimeInMs"":.+\},\{""Id"":""PointOperationStatistics"",""ActivityId"":""692ab2f2-41ba-486b-aad7-8c7c6c52379f"",""ResponseTimeUtc"":"".+Z"",""StatusCode"":429,""SubStatusCode"":0,""RequestCharge"":42.0,""RequestUri"":""http://MockUri.com"",""RequestSessionToken"":null,""ResponseSessionToken"":null\},\{""Id"":""SuccessScope"",""ElapsedTimeInMs"":.+\},\{""Id"":""PointOperationStatistics"",""ActivityId"":""de09baab-71a4-4897-a163-470711c93ed3"",""ResponseTimeUtc"":"".+Z"",""StatusCode"":200,""SubStatusCode"":0,""RequestCharge"":42.0,""RequestUri"":""http://MockUri.com"",""RequestSessionToken"":null,""ResponseSessionToken"":null\}\]\}"; Assert.IsTrue(Regex.IsMatch(result, regex), $"regex: {regex} result: {result}"); JToken jToken = JToken.Parse(result); double total = jToken["Summary"]["TotalElapsedTimeInMs"].ToObject <double>(); Assert.IsTrue(total > TimeSpan.FromSeconds(2).TotalMilliseconds); double overalScope = jToken["Context"][0]["ElapsedTimeInMs"].ToObject <double>(); Assert.IsTrue(overalScope < total); Assert.IsTrue(overalScope > TimeSpan.FromSeconds(1).TotalMilliseconds); double innerScope = jToken["Context"][2]["ElapsedTimeInMs"].ToObject <double>(); Assert.IsTrue(innerScope > 0); }