Class: Ask::Monitoring::Channels::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/ask/monitoring/channels/slack.rb

Overview

Slack alert channel.

Delivers alerts via Slack Incoming Webhooks.

Examples:

channel = Ask::Monitoring::Channels::Slack.new(webhook_url: "https://hooks.slack.com/...")
channel.deliver(alert)

Instance Method Summary collapse

Constructor Details

#initialize(webhook_url:) ⇒ Slack

Returns a new instance of Slack.

Parameters:

  • webhook_url (String)

    Slack Incoming Webhook URL



13
14
15
# File 'lib/ask/monitoring/channels/slack.rb', line 13

def initialize(webhook_url:)
  @webhook_url = webhook_url
end

Instance Method Details

#deliver(alert) ⇒ Boolean

Deliver an alert to Slack.

Parameters:

  • alert (Hash)

    Alert hash with :name, :message, :metrics

Returns:

  • (Boolean)

    Whether delivery succeeded



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ask/monitoring/channels/slack.rb', line 21

def deliver(alert)
  require "net/http"
  require "uri"
  require "json"

  uri = URI.parse(@webhook_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"

  payload = {
    text: format_alert(alert),
    mrkdwn: true
  }

  request = Net::HTTP::Post.new(uri.request_uri,
    "Content-Type" => "application/json")
  request.body = JSON.generate(payload)

  response = http.request(request)
  response.code.to_i == 200
end