diff --git a/lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb b/lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb index cba09a2..ee4c15f 100644 --- a/lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb +++ b/lib/prometheus_exporter/ext/instrumentation/periodic_stats.rb @@ -21,6 +21,7 @@ def start(frequency: 30, client: PrometheusExporter::Client.default, **) rescue StandardError => e client.logger.error("#{klass} Prometheus Exporter Failed To Collect Stats") client.logger.error("#{e.class} #{e.backtrace&.join("\n")}") + @on_exception&.call(e) if defined?(@on_exception) ensure sleep frequency end @@ -43,6 +44,12 @@ def stop end @thread = nil end + + # Adds handler that will be called when exception is raised in the thread. + # @yieldparam exception [Exception] + def on_exception(&block) + @on_exception = block + end end end end