blazer.rb 4.2 KB

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