blazer.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. require "csv"
  2. require "yaml"
  3. require "chartkick"
  4. require "safely/core"
  5. require "blazer/version"
  6. require "blazer/data_source"
  7. require "blazer/result"
  8. require "blazer/run_statement"
  9. require "blazer/adapters/base_adapter"
  10. require "blazer/adapters/elasticsearch_adapter"
  11. require "blazer/adapters/mongodb_adapter"
  12. require "blazer/adapters/presto_adapter"
  13. require "blazer/adapters/sql_adapter"
  14. require "blazer/engine"
  15. module Blazer
  16. class Error < StandardError; end
  17. class TimeoutNotSupported < Error; end
  18. class << self
  19. attr_accessor :audit
  20. attr_reader :time_zone
  21. attr_accessor :user_name
  22. attr_accessor :user_class
  23. attr_accessor :user_method
  24. attr_accessor :before_action
  25. attr_accessor :from_email
  26. attr_accessor :cache
  27. attr_accessor :transform_statement
  28. attr_accessor :check_schedules
  29. attr_accessor :anomaly_checks
  30. attr_accessor :async
  31. attr_accessor :images
  32. end
  33. self.audit = true
  34. self.user_name = :name
  35. self.check_schedules = ["5 minutes", "1 hour", "1 day"]
  36. self.anomaly_checks = false
  37. self.async = false
  38. self.images = false
  39. TIMEOUT_MESSAGE = "Query timed out :("
  40. TIMEOUT_ERRORS = [
  41. "canceling statement due to statement timeout", # postgres
  42. "canceling statement due to conflict with recovery", # postgres
  43. "cancelled on user's request", # redshift
  44. "canceled on user's request", # redshift
  45. "system requested abort", # redshift
  46. "maximum statement execution time exceeded" # mysql
  47. ]
  48. BELONGS_TO_OPTIONAL = {}
  49. BELONGS_TO_OPTIONAL[:optional] = true if Rails::VERSION::MAJOR >= 5
  50. def self.time_zone=(time_zone)
  51. @time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s]
  52. end
  53. def self.settings
  54. @settings ||= begin
  55. path = Rails.root.join("config", "blazer.yml").to_s
  56. if File.exist?(path)
  57. YAML.load(ERB.new(File.read(path)).result)
  58. else
  59. {}
  60. end
  61. end
  62. end
  63. def self.data_sources
  64. @data_sources ||= begin
  65. ds = Hash[
  66. settings["data_sources"].map do |id, s|
  67. [id, Blazer::DataSource.new(id, s)]
  68. end
  69. ]
  70. ds.default = ds.values.first
  71. ds
  72. end
  73. end
  74. def self.run_checks(schedule: nil)
  75. checks = Blazer::Check.includes(:query)
  76. checks = checks.where(schedule: schedule) if schedule
  77. checks.find_each do |check|
  78. next if check.state == "disabled"
  79. Safely.safely { run_check(check) }
  80. end
  81. end
  82. def self.run_check(check)
  83. rows = nil
  84. error = nil
  85. tries = 1
  86. ActiveSupport::Notifications.instrument("run_check.blazer", check_id: check.id, query_id: check.query.id, state_was: check.state) do |instrument|
  87. # try 3 times on timeout errors
  88. data_source = data_sources[check.query.data_source]
  89. statement = check.query.statement
  90. Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement
  91. while tries <= 3
  92. result = data_source.run_statement(statement, refresh_cache: true, check: check, query: check.query)
  93. if result.timed_out?
  94. Rails.logger.info "[blazer timeout] query=#{check.query.name}"
  95. tries += 1
  96. sleep(10)
  97. elsif result.error.to_s.start_with?("PG::ConnectionBad")
  98. data_source.reconnect
  99. Rails.logger.info "[blazer reconnect] query=#{check.query.name}"
  100. tries += 1
  101. sleep(10)
  102. else
  103. break
  104. end
  105. end
  106. check.update_state(result)
  107. # TODO use proper logfmt
  108. Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{result.rows.try(:size)} error=#{result.error}"
  109. instrument[:statement] = statement
  110. instrument[:data_source] = data_source
  111. instrument[:state] = check.state
  112. instrument[:rows] = result.rows.try(:size)
  113. instrument[:error] = result.error
  114. instrument[:tries] = tries
  115. end
  116. end
  117. def self.send_failing_checks
  118. emails = {}
  119. Blazer::Check.includes(:query).where(state: ["failing", "error", "timed out", "disabled"]).find_each do |check|
  120. check.split_emails.each do |email|
  121. (emails[email] ||= []) << check
  122. end
  123. end
  124. emails.each do |email, checks|
  125. Blazer::CheckMailer.failing_checks(email, checks).deliver_later
  126. end
  127. end
  128. end