Class: Fiveruns::Dash::CounterMetric

Inherits:
Metric show all
Defined in:
lib/fiveruns/dash/metric.rb

Instance Attribute Summary

Attributes inherited from Metric

#description, #help_text, #name, #options, #recipe

Instance Method Summary collapse

Methods inherited from Metric

#==, #abstract?, #calculate, #data, #find_context_with, #info, #key, #virtual?

Methods included from Typable

included

Constructor Details

#initialize(*args) ⇒ CounterMetric

Returns a new instance of CounterMetric.



320
321
322
323
324
325
326
# File 'lib/fiveruns/dash/metric.rb', line 320

def initialize(*args)
  super(*args)
  if incrementing_methods.any?
    reset
    install_hook
  end
end

Instance Method Details

#blank_dataObject



360
361
362
# File 'lib/fiveruns/dash/metric.rb', line 360

def blank_data
  Hash.new(0)
end

#current_valueObject

Get the current value

  • Note: We sync here (and wherever @data is being written)



377
378
379
380
381
382
383
384
# File 'lib/fiveruns/dash/metric.rb', line 377

def current_value
  result = ::Fiveruns::Dash.sync do
    # Ensure the empty context is stored with a default of 0
    @data[[]] = @data.fetch([], 0)
    @data
  end
  parse_value result
end

#incrementing_methodsObject



364
365
366
# File 'lib/fiveruns/dash/metric.rb', line 364

def incrementing_methods
  @incrementing_methods ||= Array(@options[:incremented_by])
end

#install_hookObject



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/fiveruns/dash/metric.rb', line 338

def install_hook
  if Util.blank?(incrementing_methods)
    raise RuntimeError,
          "Bad configuration for `#{@name}` counter metric"
  end
  @operation ||= lambda { nil }
  incrementing_methods.each do |meth|
    Instrument.add meth do |obj, time, *args|
      find_containers(obj, *args) do |container|
        container += 1
        container
      end
    end
  end
end

#resetObject

Reset the current value

  • Note: We sync here (and wherever @data is being written)



356
357
358
# File 'lib/fiveruns/dash/metric.rb', line 356

def reset
  ::Fiveruns::Dash.sync { @data = blank_data }
end

#validate!Object



368
369
370
371
372
373
# File 'lib/fiveruns/dash/metric.rb', line 368

def validate!
  super
  if !@options[:incremented_by]
    raise ArgumentError, "No block given to capture counter `#{@name}'" unless @operation
  end
end

#value_hashObject



328
329
330
331
332
333
334
335
336
# File 'lib/fiveruns/dash/metric.rb', line 328

def value_hash
  if incrementing_methods.any?
    values = {:values => current_value}
    reset
    values
  else
    super
  end
end