JustinAiken / adhearsion-stats

Stats sending for Adhearsion
MIT License
3 stars 2 forks source link

Namespace probes #3

Open bklang opened 10 years ago

bklang commented 10 years ago

I'm not sure if this belongs in the library hard-coded, should be config, or should be left up to the Application exclusively. But I did something like this to namespace my probes to make it easier to separate development from production. This module was mixed in so that every call go #gauge, #increment or #timing had prefixes:

module CoreUtility
  def gauge(*args)
    AdhearsionStats.gauge prefixed_label(args.shift), *args
  end

  def increment(*args)
    # Increment gets a special label so it shows up in Graphite at a similar
    # nesting level to timers and gauges
    AdhearsionStats.increment "counters.#{prefixed_label(args.shift)}", *args
  end

  def timing(*args)
    label = prefixed_label args.shift
    if block_given?
      start = Time.now
      yield
      value = Time.now - start
    else
      value = args.shift
    end
    AdhearsionStats.timing label, value
  end

  def prefixed_label(label)
    "#{Adhearsion.config.platform.environment}.#{node_name}.#{label}"
  end

  def node_name
    @node_name ||= Socket.gethostname.split('.').first
  end
end
JustinAiken commented 10 years ago

I think there's two separate ideas there, the mixins and the labels; I like both.

  def timing(*args, &block)
    label = prefixed_label args.shift
    if block
      AdhearsionStats.time label, block
    else
      AdhearsionStats.timing label, args.shift
    end
  end