Esempio n. 1
0
        public async Task InvalidStorageConnection_Handled()
        {
            var configuration = new ConfigurationBuilder().Build();

            Assert.Null(configuration.GetWebJobsConnectionString(ConnectionStringNames.Storage));

            var            options       = new ScaleOptions();
            ILoggerFactory loggerFactory = new LoggerFactory();

            loggerFactory.AddProvider(_loggerProvider);
            var localRepository = new TableStorageScaleMetricsRepository(configuration, _hostIdProviderMock.Object, new OptionsWrapper <ScaleOptions>(options), loggerFactory, new TestEnvironment());

            var monitor1 = new TestScaleMonitor1();
            var monitor2 = new TestScaleMonitor2();
            var monitor3 = new TestScaleMonitor3();
            var monitors = new IScaleMonitor[] { monitor1, monitor2, monitor3 };
            var result   = await localRepository.ReadMetricsAsync(monitors);

            Assert.Empty(result);

            var logs = _loggerProvider.GetAllLogMessages();

            Assert.Single(logs);
            Assert.Equal("Azure Storage connection string is empty or invalid. Unable to read/write scale metrics.", logs[0].FormattedMessage);

            _loggerProvider.ClearAllLogMessages();
            Dictionary <IScaleMonitor, ScaleMetrics> metricsMap = new Dictionary <IScaleMonitor, ScaleMetrics>();

            metricsMap.Add(monitor1, new TestScaleMetrics1 {
                Count = 10
            });
            metricsMap.Add(monitor2, new TestScaleMetrics2 {
                Num = 50
            });
            metricsMap.Add(monitor3, new TestScaleMetrics3 {
                Length = 100
            });
            await localRepository.WriteMetricsAsync(metricsMap);
        }
        public async Task OnTimer_MonitorFailuresAreHandled()
        {
            var testMetrics1 = new List <TestScaleMetrics1>
            {
                new TestScaleMetrics1 {
                    Count = 10
                },
                new TestScaleMetrics1 {
                    Count = 15
                },
                new TestScaleMetrics1 {
                    Count = 45
                },
                new TestScaleMetrics1 {
                    Count = 50
                },
                new TestScaleMetrics1 {
                    Count = 100
                }
            };
            var monitor1 = new TestScaleMonitor1
            {
                Exception = new Exception("Kaboom!")
            };

            _monitors.Add(monitor1);

            var testMetrics2 = new List <TestScaleMetrics2>
            {
                new TestScaleMetrics2 {
                    Num = 300
                },
                new TestScaleMetrics2 {
                    Num = 350
                },
                new TestScaleMetrics2 {
                    Num = 400
                },
                new TestScaleMetrics2 {
                    Num = 450
                },
                new TestScaleMetrics2 {
                    Num = 500
                }
            };
            var monitor2 = new TestScaleMonitor2
            {
                Metrics = testMetrics2
            };

            _monitors.Add(monitor2);

            await _monitor.StartAsync(CancellationToken.None);

            await TestHelpers.Await(() =>
            {
                return(_metricsRepository.Count >= 5);
            });

            var logs = _loggerProvider.GetAllLogMessages().ToArray();

            var infoLogs = logs.Where(p => p.Level == LogLevel.Information);

            Assert.Equal("Runtime scale monitoring is enabled.", logs[0].FormattedMessage);
            Assert.Equal("Taking metrics samples for 2 monitor(s).", logs[1].FormattedMessage);

            // verify the failure logs for the failing monitor
            Assert.True(logs.Count(p => p.FormattedMessage.Equals($"Failed to collect scale metrics sample for monitor 'testscalemonitor1'.")) >= 5);

            // verify each successful sample is logged
            Assert.True(logs.Count(p => p.FormattedMessage.StartsWith($"Scale metrics sample for monitor 'testscalemonitor2'")) >= 5);

            var metricsWritten = _metricsRepository.Metrics[monitor2].Take(5);

            Assert.Equal(testMetrics2, metricsWritten);
        }
Esempio n. 3
0
        public async Task WriteMetricsAsync_PersistsMetrics()
        {
            var monitor1 = new TestScaleMonitor1();
            var monitor2 = new TestScaleMonitor2();
            var monitor3 = new TestScaleMonitor3();
            var monitors = new IScaleMonitor[] { monitor1, monitor2, monitor3 };

            var result = await _repository.ReadMetricsAsync(monitors);

            Assert.Equal(3, result.Count);

            // simulate 10 sample iterations
            for (int i = 0; i < 10; i++)
            {
                Dictionary <IScaleMonitor, ScaleMetrics> metricsMap = new Dictionary <IScaleMonitor, ScaleMetrics>();

                metricsMap.Add(monitor1, new TestScaleMetrics1 {
                    Count = i
                });
                metricsMap.Add(monitor2, new TestScaleMetrics2 {
                    Num = i
                });
                metricsMap.Add(monitor3, new TestScaleMetrics3 {
                    Length = i
                });

                await _repository.WriteMetricsAsync(metricsMap);
            }

            // read the metrics back
            result = await _repository.ReadMetricsAsync(monitors);

            Assert.Equal(3, result.Count);

            var monitorMetricsList = result[monitor1];

            for (int i = 0; i < 10; i++)
            {
                var currSample = (TestScaleMetrics1)monitorMetricsList[i];
                Assert.Equal(i, currSample.Count);
                Assert.NotEqual(default(DateTime), currSample.Timestamp);
            }

            monitorMetricsList = result[monitor2];
            for (int i = 0; i < 10; i++)
            {
                var currSample = (TestScaleMetrics2)monitorMetricsList[i];
                Assert.Equal(i, currSample.Num);
                Assert.NotEqual(default(DateTime), currSample.Timestamp);
            }

            monitorMetricsList = result[monitor3];
            for (int i = 0; i < 10; i++)
            {
                var currSample = (TestScaleMetrics3)monitorMetricsList[i];
                Assert.Equal(i, currSample.Length);
                Assert.NotEqual(default(DateTime), currSample.Timestamp);
            }

            // if no monitors are presented result will be empty
            monitors = new IScaleMonitor[0];
            result   = await _repository.ReadMetricsAsync(monitors);

            Assert.Equal(0, result.Count);
        }