Class: Sc4ry::Notifiers::Mattermost

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

Class Method Summary collapse

Class Method Details

.notify(options = {}) ⇒ Bool

send metrics to Prometheus PushGateway

Returns:

  • (Bool)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sc4ry/notifiers/mattermost.rb', line 8

def Mattermost.notify(options = {})
    config = Sc4ry::Notifiers.get({name: :mattermost})[:config]
    status = options[:config][:status][:general]
    circuit = options[:circuit]
    status_map = {:open => 0, :half_open => 1, :closed => 2}
    
    uri = URI.parse("#{config[:url]}/hooks/#{config[:token]}")
    message = "notifying for circuit #{circuit.to_s}, state : #{status.to_s}."
    if Sc4ry::Helpers::verify_service url: config[:url] then
        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 Notifier : #{message}"
        request.body = ::JSON.dump(payload)
        response = ::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
end