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);
        }
Example #2
0
        public HangfirePrometheusExporterTests()
        {
            _mockStorageConnection = new Mock <IStorageConnection>();
            _mockMonitoringApi     = new Mock <IMonitoringApi>();

            var mockStorage = new Mock <JobStorage>();

            mockStorage.Setup(x => x.GetConnection()).Returns(_mockStorageConnection.Object);
            mockStorage.Setup(x => x.GetMonitoringApi()).Returns(_mockMonitoringApi.Object);

            _exporter = new HangfirePrometheusExporter(mockStorage.Object, new MetricService());
        }
        public PrometheusExporterTests()
        {
            _autoFixture = new Fixture();

            _mockHangfireMonitor = new Mock <IHangfireMonitorService>();

            _collectorRegistry = Metrics.NewCustomRegistry();

            _settings = new HangfirePrometheusSettings
            {
                CollectorRegistry = _collectorRegistry
            };

            _classUnderTest = new HangfirePrometheusExporter(_mockHangfireMonitor.Object, _settings);
        }
        /// <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);
        }
Example #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);
        }