module Vanity::Metric::Remote

To update a remote metric, make a POST request to the metric URL with the content type “application/x-www-form-urlencoded” and the following fields:

Public Instance Methods

track!(args = nil) click to toggle source
# File lib/vanity/metric/remote.rb, line 30
def track!(args = nil)
  return unless @playground.collecting?
  timestamp, identity, values = track_args(args)
  params = ["metric=#{CGI.escape @id.to_s}", "timestamp=#{CGI.escape timestamp.httpdate}"]
  params << "identity=#{CGI.escape identity.to_s}" if identity
  params.concat values.map { |v| "values[]=#{v.to_i}" }
  params << @remote_url.query if @remote_url.query
  @mutex.synchronize do
    @http ||= Net::HTTP.start(@remote_url.host, @remote_url.port)
    @http.request Net::HTTP::Post.new(@remote_url.path, "Content-Type"=>"application/x-www-form-urlencoded"), params.join("&")
  end
rescue Timeout::Error, StandardError
  @playground.logger.error "Error sending data for metric #{name}: #{$!}"
  @http = nil
ensure
  call_hooks timestamp, identity, values
end