Exemple #1
0
        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"));
        }
Exemple #2
0
        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 TestUpdatesWhileVisiting()
        {
            CosmosDiagnosticsContext cosmosDiagnostics = new CosmosDiagnosticsContextCore();

            cosmosDiagnostics.CreateScope("FirstScope");

            bool isFirst = true;

            foreach (CosmosDiagnosticsInternal diagnostic in cosmosDiagnostics)
            {
                if (isFirst)
                {
                    cosmosDiagnostics.CreateScope("SecondScope");
                    isFirst = false;
                }

                diagnostic.ToString();
            }
        }
        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);
        }
Exemple #5
0
        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());
        }
Exemple #6
0
        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);
        }