Class: Sc4ry::Notifiers::Mattermost

Inherits:
Object
  • Object
show all
Defined in:
lib/sc4ry/notifiers/mattermost.rb

Overview

Mattermost Notifier class

Class Method Summary collapse

Class Method Details

.notify(options = {}) ⇒ Bool

send metrics to Prometheus PushGateway

Returns:

  • (Bool)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sc4ry/notifiers/mattermost.rb', line 13

def self.notify(options = {})
  config = Sc4ry::Notifiers.get(name: :mattermost)[:config]
  status = options[:config][:status][:general]
  circuit = options[:circuit]
  begin
    uri = URI.parse("#{config[:url]}/hooks/#{config[:token]}")
    message = "notifying for circuit #{circuit}, state : #{status}."
    if Sc4ry::Helpers.verify_service url: config[:url]
      request = ::Net::HTTP::Post.new(uri)
      request.content_type = 'application/json'
      req_options = {
        use_ssl: uri.scheme == 'https'
      }
      payload = { 'text' => "message : #{message} from #{Socket.gethostname}", 'username' => 'Sc4ry' }
      Sc4ry::Helpers.log level: :debug, message: "Mattermost Notifying : #{message}"
      request.body = ::JSON.dump(payload)
      ::Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
        http.request(request)
      end

    else
      Sc4ry::Helpers.log level: :warn, message: "Mattermost Notifier : can't notify Mattermost not reachable."
    end
  rescue URI::InvalidURIError
    Sc4ry::Helpers.log level: :warn, message: 'Mattermost Notifier : URL malformed'
  end
end