lib/yodeler/client.rb in yodeler-0.1.1 vs lib/yodeler/client.rb in yodeler-0.1.2

- old
+ new

@@ -3,27 +3,44 @@ module Yodeler class Client attr_accessor :default_endpoint_name attr_accessor :default_prefix attr_accessor :default_sample_rate + attr_accessor :timestamp_format attr_reader :endpoints + TIMESTAMP_FORMATS = { + iso8601: -> { Time.now.utc.iso8601 }, + epoch: -> { Time.now.to_i } + } + def initialize @endpoints = {} @default_sample_rate = 1.0 @default_prefix = nil - @hostname = Socket.gethostname + @hostname = Socket.gethostname + @timestamp_format = :iso8601 end + def timestamp_generator + if timestamp_format.is_a?(Symbol) && TIMESTAMP_FORMATS[timestamp_format] + TIMESTAMP_FORMATS[timestamp_format].call + elsif timestamp_format.is_a?(Proc) + timestamp_format.call + else + raise ArgumentError, "Time format not recognized: #{timestamp_format}. \nOptions are #{TIMESTAMP_FORMATS.join(', ')} or a lamba" + end + end + # Register a new endpoint # # @param [Symbol|String] name of endpoint, must be unique # @return [Yodeler::Endpoint] - def endpoint(name=:default, &block) - raise DuplicateEndpointNameError.new(name: name) if @endpoints[name] + def endpoint(name = :default, &block) + fail DuplicateEndpointNameError.new(name: name) if @endpoints[name] @default_endpoint_name ||= name - @endpoints[name] = Endpoint.new(name,&block) + @endpoints[name] = Endpoint.new(name, &block) end # Get the default endpoint # # @return [Yodeler::Endpoint] Get the default endpoint @@ -38,12 +55,12 @@ # ... is this useful or is it a big ol' booger? # # @param [Symbol] name registered adapter name # @param [Type] &block configuration for adapter # @return [~Yodeler::Adapters::Base] the adapter - def adapter(name ,&block) - endpoint() if @endpoints.empty? + def adapter(name, &block) + endpoint if @endpoints.empty? default_endpoint.use(name, &block) end # Set a gauge # @@ -53,11 +70,11 @@ # # @param [~String] name of the metric # @param [~Fixnum] value of the metric # @param [Hash] opts={} Examples {#format_options} # @return [Yodeler::Metric, nil] the dispatched metric, nil if not sampled - def gauge(name, value, opts={}) + def gauge(name, value, opts = {}) dispatch(:gauge, name, value, opts) end # Increment a counter # @@ -68,12 +85,12 @@ # # @param [~String] name of the metric # @param [~Fixnum] value=1 of the metric # @param [Hash] opts={} Examples {#format_options} # @return [Yodeler::Metric, nil] the dispatched metric, nil if not sampled - def increment(name, value=1, opts={}) - if value.kind_of?(Hash) + def increment(name, value = 1, opts = {}) + if value.is_a?(Hash) opts = value value = 1 end dispatch(:increment, name, value, opts) end @@ -90,67 +107,72 @@ # @param [~Fixnum] value time in ms # @param [Hash] opts={} Examples {#format_options} # @return [Yodeler::Metric, nil, Object] # the dispatched metric, nil if not sampled # if a block is given the result of the block is returned - def timing(name, value=nil, opts={}) - if value.kind_of?(Hash) + def timing(name, value = nil, opts = {}) + if value.is_a?(Hash) opts = value value = nil end - _retval = nil - + retval = nil if block_given? - start = Time.now - _retval = yield - value = Time.now - start + start = Time.now.to_i + retval = yield + value = Time.now.to_i - start end metric = dispatch(:timing, name, value, opts) - _retval || metric + retval || metric end # Publish an event # # @example # client.publish('item.sold', purchase.to_json) - # client.publish('user.sign_up', {name: user.name, avatar: user.image_url}) + # client.publish('user.sign_up', {name: user.name, avatar: user.image}) # # @param [~String] name of the metric # @param [~Hash] value of the metric # @param [Hash] opts={} Examples {#format_options} # @return [Yodeler::Metric, nil] the dispatched metric, nil if not sampled - def publish(name, payload, opts={}) + def publish(name, payload, opts = {}) dispatch(:event, name, payload, opts) end # Formats/Defaults metric options # # @param [Hash] opts metric options - # @option opts [Array<String,Symbol>, String, Symbol] :tags ([]) array of tags to apply to metric/event + # @option opts [Array<String,Symbol>, String, Symbol] :tags ([]) + # array of tags to apply to metric/event # @option opts [Float] :sample_rate (1.0) The sample rate to use - # @option opts [Array<Symbol>, Symbol] :to array of endpoint names to send the metric to. If not set will send to {Yodeler::Client#default_endpoint_name} + # @option opts [Array<Symbol>, Symbol] :to + # array of endpoint names to send the metric to. + # If not set will send to {Yodeler::Client#default_endpoint_name} # @return [Hash] formatted, defaulted options def format_options(opts) endpoint_names = opts.delete(:to) || [default_endpoint_name] tags = opts.delete(:tags) prefix = opts.delete(:prefix) || default_prefix + timestamp = opts.delete(:timestamp) || timestamp_generator { prefix: prefix, to: [endpoint_names].flatten.compact, sample_rate: opts.delete(:sample_rate) || default_sample_rate, tags: [tags].flatten.compact, - hostname: @hostname + hostname: @hostname, + timestamp: timestamp } end private def dispatch(type, name, value, opts) opts = format_options(opts) destinations = opts.delete(:to) + metric = Metric.new(type, name, value, opts) return nil unless metric.sample? destinations.each do |endpoint_name|