Module: PgSqlTriggers::Alerting
- Defined in:
- lib/pg_sql_triggers/alerting.rb
Overview
Drift alerting: configurable callback when drift detection finds drifted, dropped, or unknown triggers. Use with trigger:check_drift or PgSqlTriggers::Alerting.check_and_notify.
Constant Summary collapse
- ALERTABLE_STATES =
[ PgSqlTriggers::DRIFT_STATE_DRIFTED, PgSqlTriggers::DRIFT_STATE_DROPPED, PgSqlTriggers::DRIFT_STATE_UNKNOWN ].freeze
Class Method Summary collapse
- .alertable?(result) ⇒ Boolean
-
.check_and_notify ⇒ Hash
Runs drift detection for all triggers, invokes
PgSqlTriggers.drift_notifierwhen configured and there is at least one alertable result, and emitsActiveSupport::Notificationswhen available. - .filter_alertable(results) ⇒ Array<Hash>
Class Method Details
.alertable?(result) ⇒ Boolean
16 17 18 |
# File 'lib/pg_sql_triggers/alerting.rb', line 16 def alertable?(result) ALERTABLE_STATES.include?(result[:state]) end |
.check_and_notify ⇒ Hash
Runs drift detection for all triggers, invokes PgSqlTriggers.drift_notifier when configured and there is at least one alertable result, and emits ActiveSupport::Notifications when available.
The notifier receives one argument: an Array of alertable result hashes (same shape as Drift::Detector). For advanced use, a second keyword argument all_results: is passed with the full result set.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pg_sql_triggers/alerting.rb', line 35 def check_and_notify results = PgSqlTriggers::Drift::Detector.detect_all alertable = filter_alertable(results) notified = false payload = { results: results, alertable: alertable, alertable_count: alertable.size, total_count: results.size, notified: false } instrument("pg_sql_triggers.drift_check", payload) do if alertable.any? && PgSqlTriggers.drift_notifier begin PgSqlTriggers.drift_notifier.call(alertable, all_results: results) notified = true rescue StandardError => e payload[:notifier_error] = e. if defined?(Rails.logger) && Rails.logger Rails.logger.error("PgSqlTriggers drift_notifier failed: #{e.class}: #{e.}") end end end payload[:notified] = notified end { results: results, alertable: alertable, notified: notified } end |
.filter_alertable(results) ⇒ Array<Hash>
22 23 24 |
# File 'lib/pg_sql_triggers/alerting.rb', line 22 def filter_alertable(results) results.select { |r| alertable?(r) } end |