Sha256: 6cb1b9f4c3d8f522bcc4efed04043e366f0bff0e78c46ea25471c639a21ec617

Contents?: true

Size: 1.9 KB

Versions: 58

Compression:

Stored size: 1.9 KB

Contents

require "net/http"
require "cgi"

module Vanity
  class Metric

    # Specifies the base URL to use for a remote metric. For example:
    #   metric :sandbox do
    #     remote "http://api.vanitydash.com/metrics/sandbox"
    #   end
    def remote(url = nil)
      @remote_url = URI.parse(url) if url
      @mutex ||= Mutex.new
      extend Remote
      @remote_url
    end

    # 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:
    # - The +metric+ identifier,
    # - The +timestamp+ must be RFC 2616 formatted (in Ruby just call +httpdate+
    #   on the Time object),
    # - The +identity+ (optional),
    # - Pass consecutive values using the field +values[]+, or
    # - Set values by their index using +values[0]+, +values[1]+, etc or
    # - Set values by series name using +values[foo]+, +values[bar]+, etc.
    module Remote

      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

      # "Don't worry, be crappy. Revolutionary means you ship and then test."
      # -- Guy Kawazaki

    end
  end
end

Version data entries

58 entries across 58 versions & 6 rubygems

Version Path
vanity-3.1.0 lib/vanity/metric/remote.rb
vanity-3.0.2 lib/vanity/metric/remote.rb
vanity-3.0.1 lib/vanity/metric/remote.rb
vanity-3.0.0 lib/vanity/metric/remote.rb
vanity-2.2.10 lib/vanity/metric/remote.rb
vanity-2.2.9 lib/vanity/metric/remote.rb
vanity-2.2.8 lib/vanity/metric/remote.rb
vanity-2.2.7 lib/vanity/metric/remote.rb
vanity-2.2.6 lib/vanity/metric/remote.rb
vanity-2.2.4 lib/vanity/metric/remote.rb
vanity-2.2.3 lib/vanity/metric/remote.rb
vanity-2.2.2 lib/vanity/metric/remote.rb
vanity-2.2.1 lib/vanity/metric/remote.rb
vanity-2.2.0 lib/vanity/metric/remote.rb
vanity-2.1.2 lib/vanity/metric/remote.rb
vanity-2.1.1 lib/vanity/metric/remote.rb
vanity-2.1.0 lib/vanity/metric/remote.rb
vanity-2.0.1 lib/vanity/metric/remote.rb
vanity-2.0.0 lib/vanity/metric/remote.rb
vanity-2.0.0.beta9 lib/vanity/metric/remote.rb