Class: Statsman::ReporterJob

Inherits:
Object
  • Object
show all
Includes:
SuckerPunch::Job
Defined in:
lib/statsman/reporter_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(config, data_type, key, value, meta) ⇒ Object



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

def perform(config, data_type, key, value, meta)
  Statsman::Agent.log("ReporterJob sending data_type: #{data_type}, key: #{key}, value: #{value}, meta: #{meta}")

  body = { 
           data_type: data_type, 
           key: key,
           value: value,
           meta: meta,
           time: Time.now.to_i    # Add timestamp to body to avoid replay attacks
         }

  url = "#{config.url}/stats"

  resp = HTTParty.post(
              "#{config.url}/stats",
              headers: { "authorization" => request_auth_header(config, "/stats", body)},
              body: body
              )
  Statsman::Agent.log("sent data_type: #{data_type}, key: #{key}, value: #{value}, meta: #{meta}, response: [#{resp.code}] #{resp.headers.inspect}")

rescue => e
  Statsman::Agent.log("Exception sending data_type: #{data_type}, key: #{key}, value: #{value}, meta: #{meta}: #{e.message}\n#{e.backtrace.join("\n")}")

end

#request_auth_header(config, path, body) ⇒ Object



35
36
37
38
# File 'lib/statsman/reporter_job.rb', line 35

def request_auth_header(config, path, body)
  signature = request_signature(config, path, body)
  "#{config.api_key}:#{signature}"
end

#request_signature(config, path, body) ⇒ Object



40
41
42
43
44
# File 'lib/statsman/reporter_job.rb', line 40

def request_signature(config, path, body)
  str = HTTParty::HashConversions.to_params(body)
  to_sign = path + str
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), config.api_secret, to_sign)
end