blazer.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/athena_adapter"
  11. require "blazer/adapters/bigquery_adapter"
  12. require "blazer/adapters/cassandra_adapter"
  13. require "blazer/adapters/drill_adapter"
  14. require "blazer/adapters/druid_adapter"
  15. require "blazer/adapters/elasticsearch_adapter"
  16. require "blazer/adapters/mongodb_adapter"
  17. require "blazer/adapters/presto_adapter"
  18. require "blazer/adapters/sql_adapter"
  19. require "blazer/engine"
  20. module Blazer
  21. class Error < StandardError; end
  22. class TimeoutNotSupported < Error; end
  23. class << self
  24. attr_accessor :audit
  25. attr_reader :time_zone
  26. attr_accessor :user_name
  27. attr_writer :user_class
  28. attr_writer :user_method
  29. attr_accessor :before_action
  30. attr_accessor :from_email
  31. attr_accessor :cache
  32. attr_accessor :transform_statement
  33. attr_accessor :check_schedules
  34. attr_accessor :anomaly_checks
  35. attr_accessor :async
  36. attr_accessor :images
  37. attr_accessor :query_viewable
  38. attr_accessor :query_editable
  39. end
  40. self.audit = true
  41. self.user_name = :name
  42. self.check_schedules = ["5 minutes", "1 hour", "1 day"]
  43. self.anomaly_checks = false
  44. self.async = false
  45. self.images = false
  46. TIMEOUT_MESSAGE = "Query timed out :("
  47. TIMEOUT_ERRORS = [
  48. "canceling statement due to statement timeout", # postgres
  49. "canceling statement due to conflict with recovery", # postgres
  50. "cancelled on user's request", # redshift
  51. "canceled on user's request", # redshift
  52. "system requested abort", # redshift
  53. "maximum statement execution time exceeded" # mysql
  54. ]
  55. BELONGS_TO_OPTIONAL = {}
  56. BELONGS_TO_OPTIONAL[:optional] = true if Rails::VERSION::MAJOR >= 5
  57. def self.time_zone=(time_zone)
  58. @time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s]
  59. end
  60. def self.user_class
  61. if !defined?(@user_class)
  62. @user_class = settings.key?("user_class") ? settings["user_class"] : (User.name rescue nil)
  63. end
  64. @user_class
  65. end
  66. def self.user_method
  67. if !defined?(@user_method)
  68. @user_method = settings["user_method"]
  69. if user_class
  70. @user_method ||= "current_#{user_class.to_s.downcase.singularize}"
  71. end
  72. end
  73. @user_method
  74. end
  75. def self.settings
  76. @settings ||= begin
  77. path = Rails.root.join("config", "blazer.yml").to_s
  78. if File.exist?(path)
  79. YAML.load(ERB.new(File.read(path)).result)
  80. else
  81. {}
  82. end
  83. end
  84. end
  85. def self.data_sources
  86. @data_sources ||= begin
  87. ds = Hash[
  88. settings["data_sources"].map do |id, s|
  89. [id, Blazer::DataSource.new(id, s)]
  90. end
  91. ]
  92. ds.default = ds.values.first
  93. ds
  94. # TODO Blazer 2.0
  95. # ds2 = Hash.new { |hash, key| raise Blazer::Error, "Unknown data source: #{key}" }
  96. # ds.each do |k, v|
  97. # ds2[k] = v
  98. # end
  99. # ds2
  100. end
  101. end
  102. def self.extract_vars(statement)
  103. # strip commented out lines
  104. # and regex {1} or {1,2}
  105. statement.gsub(/\-\-.+/, "").gsub(/\/\*.+\*\//m, "").scan(/\{\w*?\}/i).map { |v| v[1...-1] }.reject { |v| /\A\d+(\,\d+)?\z/.match(v) || v.empty? }.uniq
  106. end
  107. def self.run_checks(schedule: nil)
  108. checks = Blazer::Check.includes(:query)
  109. checks = checks.where(schedule: schedule) if schedule
  110. checks.find_each do |check|
  111. next if check.state == "disabled"
  112. Safely.safely { run_check(check) }
  113. end
  114. end
  115. def self.run_check(check)
  116. rows = nil
  117. error = nil
  118. tries = 1
  119. ActiveSupport::Notifications.instrument("run_check.blazer", check_id: check.id, query_id: check.query.id, state_was: check.state) do |instrument|
  120. # try 3 times on timeout errors
  121. data_source = data_sources[check.query.data_source]
  122. statement = check.query.statement
  123. Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement
  124. while tries <= 3
  125. result = data_source.run_statement(statement, refresh_cache: true, check: check, query: check.query)
  126. if result.timed_out?
  127. Rails.logger.info "[blazer timeout] query=#{check.query.name}"
  128. tries += 1
  129. sleep(10)
  130. elsif result.error.to_s.start_with?("PG::ConnectionBad")
  131. data_source.reconnect
  132. Rails.logger.info "[blazer reconnect] query=#{check.query.name}"
  133. tries += 1
  134. sleep(10)
  135. else
  136. break
  137. end
  138. end
  139. begin
  140. check.reload # in case state has changed since job started
  141. check.update_state(result)
  142. rescue ActiveRecord::RecordNotFound
  143. # check deleted
  144. end
  145. # TODO use proper logfmt
  146. Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{result.rows.try(:size)} error=#{result.error}"
  147. instrument[:statement] = statement
  148. instrument[:data_source] = data_source
  149. instrument[:state] = check.state
  150. instrument[:rows] = result.rows.try(:size)
  151. instrument[:error] = result.error
  152. instrument[:tries] = tries
  153. end
  154. end
  155. def self.send_failing_checks
  156. emails = {}
  157. Blazer::Check.includes(:query).where(state: ["failing", "error", "timed out", "disabled"]).find_each do |check|
  158. check.split_emails.each do |email|
  159. (emails[email] ||= []) << check
  160. end
  161. end
  162. emails.each do |email, checks|
  163. Safely.safely do
  164. Blazer::CheckMailer.failing_checks(email, checks).deliver_now
  165. end
  166. end
  167. end
  168. def self.adapters
  169. @adapters ||= {}
  170. end
  171. def self.register_adapter(name, adapter)
  172. adapters[name] = adapter
  173. end
  174. end
  175. Blazer.register_adapter "athena", Blazer::Adapters::AthenaAdapter
  176. Blazer.register_adapter "bigquery", Blazer::Adapters::BigQueryAdapter
  177. Blazer.register_adapter "cassandra", Blazer::Adapters::CassandraAdapter
  178. Blazer.register_adapter "drill", Blazer::Adapters::DrillAdapter
  179. Blazer.register_adapter "druid", Blazer::Adapters::DruidAdapter
  180. Blazer.register_adapter "elasticsearch", Blazer::Adapters::ElasticsearchAdapter
  181. Blazer.register_adapter "presto", Blazer::Adapters::PrestoAdapter
  182. Blazer.register_adapter "mongodb", Blazer::Adapters::MongodbAdapter
  183. Blazer.register_adapter "sql", Blazer::Adapters::SqlAdapter