module StackDriver

Constants

DELETE_URI
POST_URI

Public Class Methods

delete_metric(name, time) click to toggle source
# File lib/stackdriver.rb, line 25
def self.delete_metric name, time
  msg = build_message name, nil, time
  post MultiJson.dump(msg), StackDriver::DELETE_URI
end
init(*args) click to toggle source
# File lib/stackdriver.rb, line 7
def self.init *args  
  if args.count > 1
    puts "Customer ID is no longer needed, and will be deprecated"
    args.shift
  end
  @api_key = args[0]
end
send_metric(name, value, time, instance='') click to toggle source
# File lib/stackdriver.rb, line 15
def self.send_metric name, value, time, instance=''
  msg = build_message name, value, time, instance
  post MultiJson.dump(msg), StackDriver::POST_URI
end
send_multi_metrics(data) click to toggle source
# File lib/stackdriver.rb, line 20
def self.send_multi_metrics data
  msg = build_multi_message data
  post MultiJson.dump(msg), StackDriver::POST_URI
end

Private Class Methods

build_message(name, value, time, instance='') click to toggle source
# File lib/stackdriver.rb, line 48
def self.build_message name, value, time, instance=''
  data_point = {'name' => name, 'value' => value, 'collected_at' => time}
  data_point.merge!('value' => value) unless value.nil?
  data_point.merge!('instance' => instance) unless instance.empty?
  {'timestamp' => Time.now.to_i, 'proto_version' => '1', 'data' => data_point}
end
build_multi_message(data) click to toggle source
# File lib/stackdriver.rb, line 55
def self.build_multi_message data
  data_point = data
  {
      'timestamp' => Time.now.to_i,
      'proto_version' => '1',
      'data' => data_point
  }

end
post(msg, uri) click to toggle source
# File lib/stackdriver.rb, line 32
def self.post msg, uri
  headers = {'content-type' => 'application/json',
    'x-stackdriver-apikey' => @api_key}

  uri = URI(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  http.start do |http|
    response = http.post(uri.path, msg, headers)
    if response.code != "201"
      raise RuntimeError, "#{response.code} - #{response.body}"
    end
  end
end