public void ValidateDiagnosticsAppendContext() { CosmosDiagnosticsContext cosmosDiagnostics = new CosmosDiagnosticsContextCore(); CosmosDiagnosticsContext cosmosDiagnostics2; using (cosmosDiagnostics.GetOverallScope()) { // Test all the different operations on diagnostics context using (cosmosDiagnostics.CreateScope("ValidateScope")) { Thread.Sleep(TimeSpan.FromSeconds(2)); } cosmosDiagnostics.SetSdkUserAgent("MyCustomUserAgentString"); cosmosDiagnostics2 = new CosmosDiagnosticsContextCore(); 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 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 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); }