public void MetricsWithAllStatesGetCreated()
        {
            HangfireJobStatistics hangfireJobStatistics = _autoFixture.Create <HangfireJobStatistics>();

            PerformMetricsTest(hangfireJobStatistics);
            _mockHangfireMonitor.Verify(x => x.GetJobStatistics(), Times.Once);
        }
        private void VerifyPrometheusMetrics(HangfireJobStatistics hangfireJobStatistics)
        {
            List <string> expectedStrings = CreateExpectedStrings(hangfireJobStatistics);
            string        actual          = GetPrometheusContent();

            foreach (string expected in expectedStrings)
            {
                Assert.Contains(expected, actual);
            }
        }
Beispiel #3
0
        public void ShouldGetNumberOfJobs()
        {
            HangfireJobStatistics actual = _hangfireMonitorService.GetJobStatistics();

            Assert.Equal(_expectedStats.Failed, actual.Failed);
            Assert.Equal(_expectedStats.Enqueued, actual.Enqueued);
            Assert.Equal(_expectedStats.Scheduled, actual.Scheduled);
            Assert.Equal(_expectedStats.Processing, actual.Processing);
            Assert.Equal(_expectedStats.Succeeded, actual.Succeeded);
            Assert.Equal(_expectedRetrySet.Count, actual.Retry);
        }
        public void MetricsWithAllStatesGetUpdatedOnSubsequentCalls()
        {
            int count = 10;

            for (int i = 0; i < count; i++)
            {
                HangfireJobStatistics hangfireJobStatistics = _autoFixture.Create <HangfireJobStatistics>();
                PerformMetricsTest(hangfireJobStatistics);
            }

            _mockHangfireMonitor.Verify(x => x.GetJobStatistics(), Times.Exactly(10));
        }
        private List <string> CreateExpectedStrings(HangfireJobStatistics hangfireJobStatistics)
        {
            List <string> expectedStrings = new List <string>();

            expectedStrings.Add($"# HELP {_metricName} {_metricHelp}\n# TYPE {_metricName} gauge");
            expectedStrings.Add(GetMetricString(_failedLabelValue, hangfireJobStatistics.Failed));
            expectedStrings.Add(GetMetricString(_enqueuedLabelValue, hangfireJobStatistics.Enqueued));
            expectedStrings.Add(GetMetricString(_scheduledLabelValue, hangfireJobStatistics.Scheduled));
            expectedStrings.Add(GetMetricString(_processingLabelValue, hangfireJobStatistics.Processing));
            expectedStrings.Add(GetMetricString(_succeededLabelValue, hangfireJobStatistics.Succeeded));
            expectedStrings.Add(GetMetricString(_retryLabelValue, hangfireJobStatistics.Retry));
            return(expectedStrings);
        }
        public void ScrapeShoudFailOnExceptionWhenSettingEnabled()
        {
            HangfireJobStatistics hangfireJobStatistics = _autoFixture.Create <HangfireJobStatistics>();

            PerformMetricsTest(hangfireJobStatistics);

            _settings.FailScrapeOnException = true;
            Exception exToThrow = new Exception();

            _mockHangfireMonitor.Setup(x => x.GetJobStatistics()).Throws(exToThrow);
            ScrapeFailedException ex = Assert.Throws <ScrapeFailedException>(() => _classUnderTest.ExportHangfireStatistics());

            Assert.Same(exToThrow, ex.InnerException);
            Assert.Equal("Scrape failed due to exception. See InnerException for details.", ex.Message);
        }
        public void MetricsShouldNotGetUpdatedOnExceptionWhenSettingDisabled()
        {
            _settings.FailScrapeOnException = false;

            HangfireJobStatistics hangfireJobStatistics = _autoFixture.Create <HangfireJobStatistics>();

            PerformMetricsTest(hangfireJobStatistics);

            _mockHangfireMonitor.Setup(x => x.GetJobStatistics()).Throws(new Exception());
            _classUnderTest.ExportHangfireStatistics();
            VerifyPrometheusMetrics(hangfireJobStatistics);

            hangfireJobStatistics = _autoFixture.Create <HangfireJobStatistics>();
            PerformMetricsTest(hangfireJobStatistics);

            _mockHangfireMonitor.Verify(x => x.GetJobStatistics(), Times.Exactly(3));
        }
 private void PerformMetricsTest(HangfireJobStatistics hangfireJobStatistics)
 {
     _mockHangfireMonitor.Setup(x => x.GetJobStatistics()).Returns(hangfireJobStatistics);
     _classUnderTest.ExportHangfireStatistics();
     VerifyPrometheusMetrics(hangfireJobStatistics);
 }