lib/harness.rb in harness-0.9.1 vs lib/harness.rb in harness-1.0.0

- old
+ new

@@ -1,107 +1,108 @@ require "harness/version" -require 'multi_json' +require 'statsd' +require 'active_support/notifications' -require 'redis' -require 'redis/namespace' +module Harness + class Config + attr_accessor :collector, :queue + end -require 'active_support/notifications' -require 'active_support/core_ext/string' -require 'active_support/core_ext/hash/keys' -require 'active_support/core_ext/numeric' -require 'active_support/core_ext/integer' + Measurement = Struct.new(:name, :value, :rate) do + def self.from_event(event) + payload = event.payload + value = payload.fetch name.split('::').last.downcase.to_sym -require 'active_support/ordered_options' + case value + when true + new event.name + when Fixnum + new event.name, value + when String + new value + when Hash + new(value[:name] || event.name, value[:value], value[:rate]) + end + end -module Harness - class LoggingError < RuntimeError ; end - class NoCounter < RuntimeError ; end + def sample_rate + rate.nil? ? 1 : rate + end + end - class Config - attr_reader :adapter, :queue - attr_accessor :namespace, :source - attr_reader :instrument + class Timer < Measurement + def self.from_event(event) + timer = super + timer.value = event.duration + timer + end - def initialize - @instrument = ActiveSupport::OrderedOptions.new + def ms + value end - def adapter=(val) - if val.is_a? Symbol - @adapter = "Harness::#{val.to_s.camelize}Adapter".constantize.new - else - @adapter = val - end + def log + Harness.timing name, ms, sample_rate end + end - def queue=(val) - if val.is_a? Symbol - @queue = "Harness::#{val.to_s.camelize}Queue".constantize.new + class Counter < Measurement + def log + if value.nil? + Harness.increment name, sample_rate else - @queue = val + Harness.count name, value, sample_rate end end - - def method_missing(name, *args, &block) - begin - "Harness::#{name.to_s.camelize}Adapter".constantize.config - rescue NameError - super - end - end end def self.config @config ||= Config.new end - def self.log(measurement) - config.queue.push measurement + def self.increment(*args) + queue.push [:increment, args] end - def self.logger - @logger + def self.decrement(*args) + queue.push [:decrement, args] end - def self.logger=(logger) - @logger = logger + def self.timing(*args) + queue.push [:timing, args] end - def self.redis=(redis) - @redis = redis + def self.count(*args) + queue.push [:count, args] end - def self.redis - @redis + def self.time(stat, sample_rate = 1) + start = Time.now + result = yield + timing(stat, ((Time.now - start) * 1000).round, sample_rate) + result end - def self.reset_counters! - counters.each do |counter| - redis.set counter, 0 - end + def self.gauge(*args) + queue.push [:gauge, args] end - def self.counters - redis.keys "counters/*" + def self.queue + config.queue end + + def self.collector + config.collector + end end -require 'harness/measurement' -require 'harness/counter' -require 'harness/gauge' +require 'harness/sync_queue' +require 'harness/async_queue' +require 'harness/null_collector' +require 'harness/fake_collector' + require 'harness/instrumentation' +require 'harness/subscription' -require 'harness/job' - -require 'harness/queues/synchronous_queue' - -require 'harness/adapters/librato_adapter' -require 'harness/adapters/memory_adapter' -require 'harness/adapters/null_adapter' - -require 'harness/railtie' if defined?(Rails) - -require 'logger' - -Harness.logger = Logger.new $stdout +Harness.config.queue = Harness::AsyncQueue.new