blazer.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/drill_adapter"
  11. require "blazer/adapters/elasticsearch_adapter"
  12. require "blazer/adapters/mongodb_adapter"
  13. require "blazer/adapters/presto_adapter"
  14. require "blazer/adapters/sql_adapter"
  15. require "blazer/engine"
  16. module Blazer
  17. class Error < StandardError; end
  18. class TimeoutNotSupported < Error; end
  19. class << self
  20. attr_accessor :audit
  21. attr_reader :time_zone
  22. attr_accessor :user_name
  23. attr_accessor :user_class
  24. attr_accessor :user_method
  25. attr_accessor :before_action
  26. attr_accessor :from_email
  27. attr_accessor :cache
  28. attr_accessor :transform_statement
  29. attr_accessor :check_schedules
  30. attr_accessor :anomaly_checks
  31. attr_accessor :async
  32. attr_accessor :images
  33. attr_accessor :query_editable
  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. # TODO Blazer 2.0
  75. # ds2 = Hash.new { |hash, key| raise Blazer::Error, "Unknown data source: #{key}" }
  76. # ds.each do |k, v|
  77. # ds2[k] = v
  78. # end
  79. # ds2
  80. end
  81. end
  82. def self.run_checks(schedule: nil)
  83. checks = Blazer::Check.includes(:query)
  84. checks = checks.where(schedule: schedule) if schedule
  85. checks.find_each do |check|
  86. next if check.state == "disabled"
  87. Safely.safely { run_check(check) }
  88. end
  89. end
  90. def self.run_check(check)
  91. rows = nil
  92. error = nil
  93. tries = 1
  94. ActiveSupport::Notifications.instrument("run_check.blazer", check_id: check.id, query_id: check.query.id, state_was: check.state) do |instrument|
  95. # try 3 times on timeout errors
  96. data_source = data_sources[check.query.data_source]
  97. statement = check.query.statement
  98. Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement
  99. while tries <= 3
  100. result = data_source.run_statement(statement, refresh_cache: true, check: check, query: check.query)
  101. if result.timed_out?
  102. Rails.logger.info "[blazer timeout] query=#{check.query.name}"
  103. tries += 1
  104. sleep(10)
  105. elsif result.error.to_s.start_with?("PG::ConnectionBad")
  106. data_source.reconnect
  107. Rails.logger.info "[blazer reconnect] query=#{check.query.name}"
  108. tries += 1
  109. sleep(10)
  110. else
  111. break
  112. end
  113. end
  114. check.update_state(result)
  115. # TODO use proper logfmt
  116. Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{result.rows.try(:size)} error=#{result.error}"
  117. instrument[:statement] = statement
  118. instrument[:data_source] = data_source
  119. instrument[:state] = check.state
  120. instrument[:rows] = result.rows.try(:size)
  121. instrument[:error] = result.error
  122. instrument[:tries] = tries
  123. end
  124. end
  125. def self.send_failing_checks
  126. emails = {}
  127. Blazer::Check.includes(:query).where(state: ["failing", "error", "timed out", "disabled"]).find_each do |check|
  128. check.split_emails.each do |email|
  129. (emails[email] ||= []) << check
  130. end
  131. end
  132. emails.each do |email, checks|
  133. Safely.safely do
  134. Blazer::CheckMailer.failing_checks(email, checks).deliver_now
  135. end
  136. end
  137. end
  138. def self.adapters
  139. @adapters ||= {}
  140. end
  141. def self.register_adapter(name, adapter)
  142. adapters[name] = adapter
  143. end
  144. end
  145. Blazer.register_adapter "drill", Blazer::Adapters::DrillAdapter
  146. Blazer.register_adapter "elasticsearch", Blazer::Adapters::ElasticsearchAdapter
  147. Blazer.register_adapter "mongodb", Blazer::Adapters::MongodbAdapter
  148. Blazer.register_adapter "presto", Blazer::Adapters::PrestoAdapter
  149. Blazer.register_adapter "sql", Blazer::Adapters::SqlAdapter