Sha256: b613665fb1f9d6ba0ff969f6702ecd9b84338dafffa1e5b4298ad1d340fb6a7c

Contents?: true

Size: 901 Bytes

Versions: 1

Compression:

Stored size: 901 Bytes

Contents

require 'multi_json'

module StackDriver
  POST_URI = "https://custom-gateway.stackdriver.com/v1/custom"
  
  def self.init customer_id, api_key
    @customer_id = customer_id
    @api_key = api_key
  end
  
  def self.send_metric name, value, time
    msg = build_message name, value, time
    post MultiJson.dump(msg)
  end

  private

  def self.post msg
    headers = {'content-type' => 'application/json',
      'x-stackdriver-apikey' => @api_key}

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

    http.start do |http|
      http.post(uri.path, msg.to_json, headers)
    end
  end

  def self.build_message name, value, time
    data_point = {'name' => name, 'value' => value, 'collected_at' => time}
    
    {'timestamp' => Time.now.to_i, 'customer_id' => @customer_id,
      'proto_version' => '1', 'data' => data_point}
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stackdriver-0.1.0 ./lib/stackdriver.rb