Class: MonitRequestClient::Statistic

Inherits:
Object
  • Object
show all
Defined in:
lib/monit_request_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Statistic

Returns a new instance of Statistic.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/monit_request_client.rb', line 11

def initialize(app)
  begin
    @config = Hashie::Mash.new YAML.load_file(Rails.root.join('config', 'dashboard.yml'))
    if @config["collect_data"] == false
      @app = app
      return
    end
    conn = Bunny.new(@config["connect"])
    conn.start
    channel = conn.create_channel
    @queue = channel.queue(@config["queue_name"], durable: true)
    @exchange  = channel.default_exchange
    rescue => e
    end
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/monit_request_client.rb', line 28

def call(env)
  start = (Time.now.to_f * 1000).to_i
  trace = ""
  exception_message = ""
  code = ""
  begin
    status, headers, response = @app.call(env)
  rescue => e
    trace = e.backtrace.join(",")
    exception_message = e.message
    raise e
  ensure
    request = ::Rack::Request.new(env)
    if @config && @config["collect_data"] == true && request.path.start_with?(@config["path_prifex"])

      Thread.new do
        begin
          begin
            # api code for record
            if response && headers && headers["Content-Type"].include?("application/json")
              body = JSON.parse(response.body.dup)
              if body && body["head"] && body["head"]["code"]
                code = body["head"]["code"]
              end
            end
          rescue => e
          end
          stop = (Time.now.to_f * 1000).to_i
          data = {"path" => request.path}
          data["method"] = request.request_method
          if env["rack.methodoverride.original_method"].present?
            data["method"] = env["rack.methodoverride.original_method"]
          end
          data["error_code"] = code
          params = request.params.dup
          # add routes params
          if env["action_dispatch.request.parameters"].present?
            params = params.merge(env["action_dispatch.request.parameters"])
          end
          params.delete("_method")
          params.delete("authenticity_token")
          params.delete("password")
          params.delete("password_confirmation")
          data["params"] = params.to_query
          data["start_time"] = start
          data["end_time"] = stop
          data["exception"] = exception_message
          data["exception_content"] = trace
          fwd = env['HTTP_X_FORWARDED_FOR']
          if fwd
            ip = fwd.strip.split(/[,\s]+/)[0] # stolen from Rack::Request#split_ip_addresses
          else
            ip = env['HTTP_X_REAL_IP'] || env['REMOTE_ADDR']
          end
          data["ip"] = ip
          data["user_id"] = env["current_user_id"]
          data["user_agent"] = request.user_agent
          data["uuid"] = env["HTTP_UUID"] if env["HTTP_UUID"]
          @exchange.publish(data.to_json, :routing_key => @queue.name,:persistent => true, :content_type => "text/plain")
        rescue => e
        end
      end
    end
  end
  [status, headers, response]
end