Module: Blazer

Defined in:
lib/blazer.rb,
lib/blazer/engine.rb,
lib/blazer/result.rb,
lib/blazer/version.rb,
lib/blazer/data_source.rb,
app/models/blazer/audit.rb,
app/models/blazer/check.rb,
app/models/blazer/query.rb,
lib/blazer/run_statement.rb,
app/models/blazer/dashboard.rb,
app/models/blazer/connection.rb,
lib/blazer/run_statement_job.rb,
app/helpers/blazer/base_helper.rb,
app/mailers/blazer/check_mailer.rb,
lib/blazer/adapters/sql_adapter.rb,
lib/blazer/adapters/base_adapter.rb,
app/models/blazer/dashboard_query.rb,
lib/blazer/adapters/presto_adapter.rb,
lib/blazer/adapters/mongodb_adapter.rb,
app/controllers/blazer/base_controller.rb,
lib/generators/blazer/install_generator.rb,
app/controllers/blazer/checks_controller.rb,
app/controllers/blazer/queries_controller.rb,
lib/blazer/adapters/elasticsearch_adapter.rb,
app/controllers/blazer/dashboards_controller.rb

Defined Under Namespace

Modules: Adapters, BaseHelper, Generators Classes: Audit, BaseController, Check, CheckMailer, ChecksController, Connection, Dashboard, DashboardQuery, DashboardsController, DataSource, Engine, Error, QueriesController, Query, Result, RunStatement, RunStatementJob, TimeoutNotSupported

Constant Summary collapse

TIMEOUT_MESSAGE =
"Query timed out :("
TIMEOUT_ERRORS =
[
  "canceling statement due to statement timeout", # postgres
  "canceling statement due to conflict with recovery", # postgres
  "cancelled on user's request", # redshift
  "canceled on user's request", # redshift
  "system requested abort", # redshift
  "maximum statement execution time exceeded" # mysql
]
BELONGS_TO_OPTIONAL =
{}
VERSION =
"1.7.7"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.anomaly_checksObject

Returns the value of attribute anomaly_checks.



31
32
33
# File 'lib/blazer.rb', line 31

def anomaly_checks
  @anomaly_checks
end

.asyncObject

Returns the value of attribute async.



32
33
34
# File 'lib/blazer.rb', line 32

def async
  @async
end

.auditObject

Returns the value of attribute audit.



21
22
23
# File 'lib/blazer.rb', line 21

def audit
  @audit
end

.before_actionObject

Returns the value of attribute before_action.



26
27
28
# File 'lib/blazer.rb', line 26

def before_action
  @before_action
end

.cacheObject

Returns the value of attribute cache.



28
29
30
# File 'lib/blazer.rb', line 28

def cache
  @cache
end

.check_schedulesObject

Returns the value of attribute check_schedules.



30
31
32
# File 'lib/blazer.rb', line 30

def check_schedules
  @check_schedules
end

.from_emailObject

Returns the value of attribute from_email.



27
28
29
# File 'lib/blazer.rb', line 27

def from_email
  @from_email
end

.imagesObject

Returns the value of attribute images.



33
34
35
# File 'lib/blazer.rb', line 33

def images
  @images
end

.query_editableObject

Returns the value of attribute query_editable.



34
35
36
# File 'lib/blazer.rb', line 34

def query_editable
  @query_editable
end

.time_zoneObject

Returns the value of attribute time_zone.



22
23
24
# File 'lib/blazer.rb', line 22

def time_zone
  @time_zone
end

.transform_statementObject

Returns the value of attribute transform_statement.



29
30
31
# File 'lib/blazer.rb', line 29

def transform_statement
  @transform_statement
end

.user_classObject

Returns the value of attribute user_class.



24
25
26
# File 'lib/blazer.rb', line 24

def user_class
  @user_class
end

.user_methodObject

Returns the value of attribute user_method.



25
26
27
# File 'lib/blazer.rb', line 25

def user_method
  @user_method
end

.user_nameObject

Returns the value of attribute user_name.



23
24
25
# File 'lib/blazer.rb', line 23

def user_name
  @user_name
end

Class Method Details

.data_sourcesObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/blazer.rb', line 70

def self.data_sources
  @data_sources ||= begin
    ds = Hash[
      settings["data_sources"].map do |id, s|
        [id, Blazer::DataSource.new(id, s)]
      end
    ]
    ds.default = ds.values.first
    ds

    # TODO Blazer 2.0
    # ds2 = Hash.new { |hash, key| raise Blazer::Error, "Unknown data source: #{key}" }
    # ds.each do |k, v|
    #   ds2[k] = v
    # end
    # ds2
  end
end

.run_check(check) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/blazer.rb', line 98

def self.run_check(check)
  rows = nil
  error = nil
  tries = 1

  ActiveSupport::Notifications.instrument("run_check.blazer", check_id: check.id, query_id: check.query.id, state_was: check.state) do |instrument|
    # try 3 times on timeout errors
    data_source = data_sources[check.query.data_source]
    statement = check.query.statement
    Blazer.transform_statement.call(data_source, statement) if Blazer.transform_statement

    while tries <= 3
      result = data_source.run_statement(statement, refresh_cache: true, check: check, query: check.query)
      if result.timed_out?
        Rails.logger.info "[blazer timeout] query=#{check.query.name}"
        tries += 1
        sleep(10)
      elsif result.error.to_s.start_with?("PG::ConnectionBad")
        data_source.reconnect
        Rails.logger.info "[blazer reconnect] query=#{check.query.name}"
        tries += 1
        sleep(10)
      else
        break
      end
    end
    check.update_state(result)
    # TODO use proper logfmt
    Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{result.rows.try(:size)} error=#{result.error}"

    instrument[:statement] = statement
    instrument[:data_source] = data_source
    instrument[:state] = check.state
    instrument[:rows] = result.rows.try(:size)
    instrument[:error] = result.error
    instrument[:tries] = tries
  end
end

.run_checks(schedule: nil) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/blazer.rb', line 89

def self.run_checks(schedule: nil)
  checks = Blazer::Check.includes(:query)
  checks = checks.where(schedule: schedule) if schedule
  checks.find_each do |check|
    next if check.state == "disabled"
    Safely.safely { run_check(check) }
  end
end

.send_failing_checksObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/blazer.rb', line 137

def self.send_failing_checks
  emails = {}
  Blazer::Check.includes(:query).where(state: ["failing", "error", "timed out", "disabled"]).find_each do |check|
    check.split_emails.each do |email|
      (emails[email] ||= []) << check
    end
  end

  emails.each do |email, checks|
    Safely.safely do
      Blazer::CheckMailer.failing_checks(email, checks).deliver_now
    end
  end
end

.settingsObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/blazer.rb', line 59

def self.settings
  @settings ||= begin
    path = Rails.root.join("config", "blazer.yml").to_s
    if File.exist?(path)
      YAML.load(ERB.new(File.read(path)).result)
    else
      {}
    end
  end
end