예제 #1
0
        public void ProviderWithException()
        {
            var mockPayloadSender = new MockPayloadSender();
            var testLogger        = new TestLogger(LogLevel.Information);

            using (var mc = new MetricsCollector(testLogger, mockPayloadSender, new ConfigStore(new MockConfigSnapshot(disableMetrics: "*"), testLogger)))
            {
                mc.MetricsProviders.Clear();
                var providerWithException = new MetricsProviderWithException();
                mc.MetricsProviders.Add(providerWithException);

                for (var i = 0; i < MetricsCollector.MaxTryWithoutSuccess; i++)
                {
                    mc.CollectAllMetrics();
                }

                providerWithException.NumberOfGetValueCalls.Should().Be(MetricsCollector.MaxTryWithoutSuccess);

                testLogger.Lines.Count(line => line.Contains(MetricsProviderWithException.ExceptionMessage))
                .Should()
                .Be(MetricsCollector.MaxTryWithoutSuccess);

                testLogger.Lines.Select(l => l.Contains($"Failed reading {providerWithException.DbgName} 1 times")).Should().HaveCountGreaterThan(0);
                testLogger.Lines.Last(line => line.Contains("Failed reading"))
                .Should()
                .Contain(
                    $"Failed reading {providerWithException.DbgName} {MetricsCollector.MaxTryWithoutSuccess} consecutively - the agent won't try reading {providerWithException.DbgName} anymore");

                //make sure GetValue() in MetricsProviderWithException is not called anymore:
                for (var i = 0; i < 10; i++)
                {
                    mc.CollectAllMetrics();
                }

                var logLineBeforeStage2 = testLogger.Lines.Count;
                //no more logs, no more calls to GetValue():
                providerWithException.NumberOfGetValueCalls.Should().Be(MetricsCollector.MaxTryWithoutSuccess);
                testLogger.Lines.Count.Should().Be(logLineBeforeStage2);
            }
        }
        public void ProviderWithException()
        {
            var mockPayloadSender = new MockPayloadSender();
            var testLogger        = new TestLogger(LogLevel.Information);
            var mc = new MetricsCollector(testLogger, mockPayloadSender, new TestAgentConfigurationReader(testLogger, "Information"));

            mc.MetricsProviders.Clear();
            var providerWithException = new MetricsProviderWithException();

            mc.MetricsProviders.Add(providerWithException);

            for (var i = 0; i < MetricsCollector.MaxTryWithoutSuccess; i++)
            {
                mc.CollectAllMetrics();
            }

            providerWithException.NumberOfGetValueCalls.Should().Be(MetricsCollector.MaxTryWithoutSuccess);

            // *2 because exceptions are logged in a new line, + 2 because 1) printing the metricsinterval and 2) printing that the given metrics
            // wont be collected anymore:
            testLogger.Lines.Count.Should().Be(MetricsCollector.MaxTryWithoutSuccess * 2 + 2);

            testLogger.Lines[1].Should().Contain($"Failed reading {providerWithException.DbgName} 1 times");
            testLogger.Lines.Last()
            .Should()
            .Contain(
                $"Failed reading {providerWithException.DbgName} {MetricsCollector.MaxTryWithoutSuccess} consecutively - the agent won't try reading {providerWithException.DbgName} anymore");

            //make sure GetValue() in MetricsProviderWithException is not called anymore:
            for (var i = 0; i < 10; i++)
            {
                mc.CollectAllMetrics();
            }

            //no more logs, no more call to GetValue():
            providerWithException.NumberOfGetValueCalls.Should().Be(MetricsCollector.MaxTryWithoutSuccess);
            testLogger.Lines.Count.Should().Be(MetricsCollector.MaxTryWithoutSuccess * 2 + 2);
        }