lib/spectator/registry.rb in netflix-spectator-rb-0.1.3 vs lib/spectator/registry.rb in netflix-spectator-rb-0.2.1

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'spectator/clock' require 'spectator/counter' require 'spectator/distribution_summary' require 'spectator/gauge' require 'spectator/http' @@ -7,22 +9,24 @@ require 'spectator/timer' module Spectator # Registry to manage a set of meters class Registry - attr_reader :config, :clock, :publisher, :common_tags + DEFAULT_BATCH_SIZE = 10_000 + attr_reader :config, :clock, :publisher, :common_tags, :batch_size + # Initialize the registry using the given config, and clock # The default clock is the SystemClock # The config is a Hash which should include: # :common_tags as a hash with tags that will be added to all metrics # :frequency the interval at which metrics will be sent to an # aggregator service, expressed in seconds # :uri the endpoint for the aggregator service def initialize(config, clock = SystemClock.new) @config = config - @batch_size = config[:batch_size] || 10_000 + @batch_size = config[:batch_size] || DEFAULT_BATCH_SIZE @clock = clock @meters = {} @common_tags = to_symbols(config[:common_tags]) || {} @lock = Mutex.new @publisher = Publisher.new(self) @@ -161,43 +165,39 @@ return end @should_stop = true Spectator.logger.info('Stopping spectator') - @publish_thread.kill if @publish_thread + @publish_thread&.kill @started = false Spectator.logger.info('Sending last batch of metrics before exiting') send_metrics_now end ADD_OP = 0 MAX_OP = 10 - UNKNOWN_OP = -1 - OPS = { count: ADD_OP, - totalAmount: ADD_OP, - totalTime: ADD_OP, - totalOfSquares: ADD_OP, - percentile: ADD_OP, - max: MAX_OP, - gauge: MAX_OP, - activeTasks: MAX_OP, - duration: MAX_OP }.freeze + COUNTER_STATS = %i[count totalAmount totalTime + totalOfSquares percentile].freeze + # Get the operation to be used for the given Measure # Gauges are aggregated using MAX_OP, counters with ADD_OP def op_for_measurement(measure) - stat = measure.id.tags.fetch(:statistic, :unknown) - OPS.fetch(stat, UNKNOWN_OP) + stat = measure.id.tags.fetch(:statistic, '') + if COUNTER_STATS.include? stat + ADD_OP + else + MAX_OP + end end # Gauges are sent if they have a value # Counters if they have a number of increments greater than 0 def should_send(measure) op = op_for_measurement(measure) - return measure.value > 0 if op == ADD_OP - return !measure.value.nan? if op == MAX_OP + return measure.value.positive? if op == ADD_OP - false + !measure.value.nan? end # Build a string table from the list of measurements # Unique words are identified, and assigned a number starting from 0 based # on their lexicographical order