示例#1
0
        public void ExportHangfireStatistics_PublishesToPrometheus()
        {
            var expectedStatistics = new StatisticsDto()
            {
                Failed     = 1,
                Deleted    = 2,
                Enqueued   = 3,
                Processing = 4,
                Recurring  = 5,
                Scheduled  = 6,
                Succeeded  = 7
            };
            var expectedRetrySet = new HashSet <string> {
                "job1", "job2", "job3"
            };

            _mockMonitoringApi.Setup(x => x.GetStatistics()).Returns(expectedStatistics);
            _mockStorageConnection.Setup(x => x.GetAllItemsFromSet("retries")).Returns(expectedRetrySet);

            _exporter.ExportHangfireStatistics();

            var prometheusContent = GetPrometheusContent();

            ExpectedMetricStrings(expectedStatistics, expectedRetrySet)
            .ForEach(ms => prometheusContent.Should().Contain(ms));
        }
        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));
        }
        public static IApplicationBuilder UsePrometheusHangfireExporter(this IApplicationBuilder app)
        {
            var jobStorage = (JobStorage)app.ApplicationServices.GetService(typeof(JobStorage));
            var metrics    = (IMetricService)app.ApplicationServices.GetService(typeof(IMetricService));
            var exporter   = new HangfirePrometheusExporter(jobStorage, metrics);

            Metrics.DefaultRegistry.AddBeforeCollectCallback(() => exporter.ExportHangfireStatistics());

            return(app);
        }
        /// <summary>
        /// Initializes Prometheus Hangfire Exporter using current Hangfire job storage and default metrics registry.
        /// </summary>
        /// <param name="app">IApplicationBuilder instance</param>
        /// <returns>Provided instance of IApplicationBuilder</returns>
        public static IApplicationBuilder UsePrometheusHangfireExporter(this IApplicationBuilder app, HangfirePrometheusSettings settings = null)
        {
            settings = settings ?? new HangfirePrometheusSettings();

            JobStorage js = (JobStorage)app.ApplicationServices.GetService(typeof(JobStorage));

            if (js != null)
            {
                IHangfireMonitorService hangfireMonitor = new HangfireMonitorService(js);
                IPrometheusExporter     exporter        = new HangfirePrometheusExporter(hangfireMonitor, settings);
                Metrics.DefaultRegistry.AddBeforeCollectCallback(() => exporter.ExportHangfireStatistics());
            }

            return(app);
        }
示例#5
0
        public static IAppBuilder UsePrometheusHangfireExporter(this IAppBuilder app, HangfirePrometheusSettings settings = null)
        {
            settings = settings ?? new HangfirePrometheusSettings();

            try
            {
                JobStorage js = JobStorage.Current;

                if (js != null)
                {
                    IHangfireMonitorService hangfireMonitor = new HangfireMonitorService(js);
                    IPrometheusExporter     exporter        = new HangfirePrometheusExporter(hangfireMonitor, settings);
                    Metrics.DefaultRegistry.AddBeforeCollectCallback(() => exporter.ExportHangfireStatistics());
                }
            }
            catch (Exception)
            {
                return(app);
            }

            return(app);
        }