Skip to content

Business intelligence made simple

Added support for Slack

Andrew Kane756d9156dd2e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 643f186b..dd29ac66 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
## 1.9.1 [unreleased]
+- Added support for Slack
- Added `override_csp` option
- Use SQL for Elasticsearch
- Fixed error with latest `google-cloud-bigquery`
diff --git a/README.md b/README.md
index e132f519..03f2c14a 100644
--- a/README.md
+++ b/README.md
@@ -53,7 +53,7 @@ For production, specify your database:
ENV["BLAZER_DATABASE_URL"] = "postgres://user:password@hostname:5432/database"
```
-Blazer tries to protect against queries which modify data (by running each query in a transaction and rolling it back), but a safer approach is to use a read only user. [See how to create one](#permissions).
+Blazer tries to protect against queries which modify data (by running each query in a transaction and rolling it back), but a safer approach is to use a read only user. [See how to create one](#permissions).
#### Checks (optional)
@@ -86,6 +86,14 @@ Here’s what it looks like with cron.
0 8 * * * rake blazer:send_failing_checks
```
+For Slack notifications, create an [incoming webhook](https://slack.com/apps/A0F7XDUAZ-incoming-webhooks) and set: [master]
+
+```sh
+BLAZER_SLACK_WEBHOOK_URL=https://hooks.slack.com/...
+```
+
+Name the webhook “Blazer” and add a cool icon.
+
## Permissions
### PostgreSQL
@@ -652,6 +660,20 @@ override_csp: true
## Upgrading
+### 1.9.1 [master]
+
+To use Slack notifications, create a migration
+
+```sh
+rails g migration add_slack_channels_to_blazer_checks
+```
+
+with:
+
+```ruby
+add_column :blazer_checks, :slack_channels, :text
+```
+
### 1.5
To take advantage of the anomaly detection, create a migration
@@ -663,8 +685,8 @@ rails g migration upgrade_blazer_to_1_5
with:
```ruby
-add_column(:blazer_checks, :check_type, :string)
-add_column(:blazer_checks, :message, :text)
+add_column :blazer_checks, :check_type, :string
+add_column :blazer_checks, :message, :text
commit_db_transaction
Blazer::Check.reset_column_information
diff --git a/app/controllers/blazer/checks_controller.rb b/app/controllers/blazer/checks_controller.rb
index 402aa6a5..580214c5 100644
--- a/app/controllers/blazer/checks_controller.rb
+++ b/app/controllers/blazer/checks_controller.rb
@@ -46,7 +46,7 @@ module Blazer
private
def check_params
- params.require(:check).permit(:query_id, :emails, :invert, :check_type, :schedule)
+ params.require(:check).permit(:query_id, :emails, :slack_channels, :invert, :check_type, :schedule)
end
def set_check
diff --git a/app/mailers/blazer/slack_notifier.rb b/app/mailers/blazer/slack_notifier.rb
new file mode 100644
index 00000000..79ab6acb
--- /dev/null
+++ b/app/mailers/blazer/slack_notifier.rb
@@ -0,0 +1,76 @@
+require "net/http"
+
+module Blazer
+ class SlackNotifier
+ def self.state_change(check, state, state_was, rows_count, error, check_type)
+ check.split_slack_channels.each do |channel|
+ text =
+ if error
+ error
+ elsif rows_count > 0 && check_type == "bad_data"
+ pluralize(rows_count, "row")
+ end
+
+ payload = {
+ channel: channel,
+ attachments: [
+ {
+ title: escape("Check #{state.titleize}: #{check.query.name}"),
+ title_link: query_url(check.query_id),
+ text: escape(text),
+ color: state == "passing" ? "good" : "danger"
+ }
+ ]
+ }
+
+ post(Blazer.slack_webhook_url, payload)
+ end
+ end
+
+ def self.failing_checks(channel, checks)
+ text =
+ checks.map do |check|
+ "<#{query_url(check.query_id)}|#{escape(check.query.name)}> #{escape(check.state)}"
+ end
+
+ payload = {
+ channel: channel,
+ attachments: [
+ {
+ title: escape("#{pluralize(checks.size, "Check")} Failing"),
+ text: text.join("\n"),
+ color: "warning"
+ }
+ ]
+ }
+
+ post(Blazer.slack_webhook_url, payload)
+ end
+
+ # https://api.slack.com/docs/message-formatting#how_to_escape_characters
+ # - Replace the ampersand, &, with &amp;
+ # - Replace the less-than sign, < with &lt;
+ # - Replace the greater-than sign, > with &gt;
+ # That's it. Don't HTML entity-encode the entire message.
+ def self.escape(str)
+ str.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;") if str
+ end
+
+ def self.pluralize(*args)
+ ActionController::Base.helpers.pluralize(*args)
+ end
+
+ def self.query_url(id)
+ Blazer::Engine.routes.url_helpers.query_url(id, ActionMailer::Base.default_url_options)
+ end
+
+ def self.post(url, payload)
+ uri = URI.parse(url)
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.use_ssl = true
+ http.open_timeout = 3
+ http.read_timeout = 5
+ http.post(uri.request_uri, payload.to_json)
+ end
+ end
+end
diff --git a/app/models/blazer/check.rb b/app/models/blazer/check.rb
index 286f49d7..bab53709 100644
--- a/app/models/blazer/check.rb
+++ b/app/models/blazer/check.rb
@@ -14,6 +14,14 @@ module Blazer
emails.to_s.downcase.split(",").map(&:strip)
end
+ def split_slack_channels
+ if Blazer.slack?
+ slack_channels.to_s.downcase.split(",").map(&:strip)
+ else
+ []
+ end
+ end
+
def update_state(result)
check_type =
if respond_to?(:check_type)
@@ -61,6 +69,7 @@ module Blazer
# do not notify on creation, except when not passing
if (state_was != "new" || state != "passing") && state != state_was && emails.present?
Blazer::CheckMailer.state_change(self, state, state_was, result.rows.size, message, result.columns, result.rows.first(10).as_json, result.column_types, check_type).deliver_now
+ Blazer::SlackNotifier.state_change(self, state, state_was, result.rows.size, message, check_type)
end
save! if changed?
end
diff --git a/app/views/blazer/checks/_form.html.erb b/app/views/blazer/checks/_form.html.erb
index 9eb1ce29..b04e4e64 100644
--- a/app/views/blazer/checks/_form.html.erb
+++ b/app/views/blazer/checks/_form.html.erb
@@ -60,7 +60,15 @@
<%= f.label :emails %>
<%= f.text_field :emails, placeholder: "Optional, comma separated", class: "form-control" %>
</div>
- <p class="text-muted">Emails are sent when a check starts failing, and when it starts passing again.
+
+ <% if Blazer.slack? %>
+ <div class="form-group">
+ <%= f.label :slack_channels %>
+ <%= f.text_field :slack_channels, placeholder: "Optional, comma separated", class: "form-control" %>
+ </div>
+ <% end %>
+
+ <p class="text-muted">Emails <%= Blazer.slack? ? "and Slack notifications " : nil %>are sent when a check starts failing, and when it starts passing again.
<p>
<% if @check.persisted? %>
<%= link_to "Delete", check_path(@check), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
diff --git a/app/views/blazer/checks/index.html.erb b/app/views/blazer/checks/index.html.erb
index 11d833ed..f733174b 100644
--- a/app/views/blazer/checks/index.html.erb
+++ b/app/views/blazer/checks/index.html.erb
@@ -9,7 +9,7 @@
<th>Query</th>
<th style="width: 10%;">State</th>
<th style="width: 10%;">Run</th>
- <th style="width: 20%;">Emails</th>
+ <th style="width: 20%;">Notify</th>
<th style="width: 15%;"></th>
</tr>
</thead>
@@ -28,6 +28,9 @@
<% check.split_emails.each do |email| %>
<li><%= email %></li>
<% end %>
+ <% check.split_slack_channels.each do |channel| %>
+ <li><%= channel %></li>
+ <% end %>
</ul>
</td>
<td style="text-align: right; padding: 1px;">
diff --git a/lib/blazer.rb b/lib/blazer.rb
index c588470f..155fbee6 100644
--- a/lib/blazer.rb
+++ b/lib/blazer.rb
@@ -40,6 +40,7 @@ module Blazer
attr_accessor :query_viewable
attr_accessor :query_editable
attr_accessor :override_csp
+ attr_accessor :slack_webhook_url
end
self.audit = true
self.user_name = :name
@@ -173,10 +174,15 @@ module Blazer
def self.send_failing_checks
emails = {}
+ slack_channels = {}
+
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
+ check.split_slack_channels.each do |channel|
+ (slack_channels[channel] ||= []) << check
+ end
end
emails.each do |email, checks|
@@ -184,6 +190,16 @@ module Blazer
Blazer::CheckMailer.failing_checks(email, checks).deliver_now
end
end
+
+ slack_channels.each do |channel, checks|
+ Safely.safely do
+ Blazer::SlackNotifier.failing_checks(channel, checks)
+ end
+ end
+ end
+
+ def self.slack?
+ slack_webhook_url.present?
end
def self.adapters
diff --git a/lib/blazer/engine.rb b/lib/blazer/engine.rb
index 5b3e5e8b..21341ffa 100644
--- a/lib/blazer/engine.rb
+++ b/lib/blazer/engine.rb
@@ -23,6 +23,7 @@ module Blazer
Blazer.images = Blazer.settings["images"] || false
Blazer.override_csp = Blazer.settings["override_csp"] || false
+ Blazer.slack_webhook_url = ENV["BLAZER_SLACK_WEBHOOK_URL"]
end
end
end
diff --git a/lib/generators/blazer/templates/install.rb.tt b/lib/generators/blazer/templates/install.rb.tt
index b7831592..34a7b525 100644
--- a/lib/generators/blazer/templates/install.rb.tt
+++ b/lib/generators/blazer/templates/install.rb.tt
@@ -36,6 +36,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
t.string :state
t.string :schedule
t.text :emails
+ t.text :slack_channels
t.string :check_type
t.text :message
t.timestamp :last_run_at
diff --git a/lib/tasks/blazer.rake b/lib/tasks/blazer.rake
index 284885ca..dff27306 100644
--- a/lib/tasks/blazer.rake
+++ b/lib/tasks/blazer.rake
@@ -4,6 +4,7 @@ namespace :blazer do
Blazer.run_checks(schedule: args[:schedule] || ENV["SCHEDULE"])
end
+ desc "send failing checks"
task send_failing_checks: :environment do
Blazer.send_failing_checks
end