class Sumologic::Metrics::Client

Public Class Methods

new(opts = {}) click to toggle source

@param [Hash] opts @option opts [String] :collector_uri Your project's collector_uri @option opts [FixNum] :max_queue_size Maximum number of calls to be

remain queued.

@option opts [Proc] :on_error Handles error calls from the API.

# File lib/sumologic/metrics/client.rb, line 19
def initialize(opts = {})
  symbolize_keys!(opts)

  @queue = Queue.new
  @collector_uri = opts[:collector_uri]
  @max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE
  @options = opts
  @worker_mutex = Mutex.new
  @worker = Worker.new(@queue, @collector_uri, @options)

  check_collector_uri!

  at_exit { @worker_thread && @worker_thread[:should_exit] = true }
end

Public Instance Methods

flush() click to toggle source

Synchronously waits until the worker has flushed the queue.

Use only for scripts which are not long-running, and will specifically exit

# File lib/sumologic/metrics/client.rb, line 38
def flush
  while !@queue.empty? || @worker.is_requesting?
    ensure_worker_running
    sleep(0.1)
  end
end
push(metric) click to toggle source

Pushes a metric

@see help.sumologic.com/Send-Data/Sources/02Sources-for-Hosted-Collectors/HTTP-Source/Upload-Data-to-an-HTTP-Source#About_the_Carbon_2.0_example_data_points

@param [String] metric

# File lib/sumologic/metrics/client.rb, line 50
def push(metric)
  check_presence!(metric)
  check_carbon_format!(metric)

  enqueue(metric)
end
queued_metrics() click to toggle source

@return [Fixnum] number of metrics in the queue

# File lib/sumologic/metrics/client.rb, line 58
def queued_metrics
  @queue.length
end

Private Instance Methods

check_carbon_format!(metric) click to toggle source

private: Checks the metric is Carbon 2

# File lib/sumologic/metrics/client.rb, line 99
def check_carbon_format!(metric)
  tag = '[^\s]+=[^\s]+'
  intrinsic_tags = "(?<intrinsic_tags>#{tag}(?: #{tag})+)"
  meta_tags = "(?<meta_tags>#{tag}(?:\s#{tag})*)"
  value = '(?<value>[\d|\.]+)'
  timestamp = '(?<timestamp>\d+)'
  r = Regexp.new(/#{intrinsic_tags}  (?:#{meta_tags}\s)?#{value} #{timestamp}/)
  raise ArgumentError, 'metric should be in Carbon 2 format' unless r.match?(metric)
end
check_collector_uri!() click to toggle source

private: Checks that the collector_uri is properly initialized

# File lib/sumologic/metrics/client.rb, line 94
def check_collector_uri!
  raise ArgumentError, 'collector URI must be initialized' if @collector_uri.nil?
end
check_presence!(obj) click to toggle source

private: Ensures that a string is non-empty

obj - String|Number that must be non-blank

# File lib/sumologic/metrics/client.rb, line 87
def check_presence!(obj)
  if obj.nil? || (obj.is_a?(String) && obj.empty?)
    raise ArgumentError, 'metric must be given'
  end
end
enqueue(metric) click to toggle source

private: Enqueues the action.

returns Boolean of whether the item was added to the queue.

# File lib/sumologic/metrics/client.rb, line 67
def enqueue(metric)
  if @queue.length < @max_queue_size
    @queue << metric
    ensure_worker_running

    true
  else
    logger.warn(
      'Queue is full, dropping events. The :max_queue_size ' \
      'configuration parameter can be increased to prevent this from ' \
      'happening.'
    )
    false
  end
end
ensure_worker_running() click to toggle source
# File lib/sumologic/metrics/client.rb, line 110
def ensure_worker_running
  return if worker_running?
  @worker_mutex.synchronize do
    return if worker_running?
    @worker_thread = Thread.new do
      @worker.run
    end
  end
end
worker_running?() click to toggle source
# File lib/sumologic/metrics/client.rb, line 120
def worker_running?
  @worker_thread && @worker_thread.alive?
end