Class: L2meter::Emitter
- Inherits:
-
Object
- Object
- L2meter::Emitter
- Defined in:
- lib/l2meter/emitter.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
- #batch ⇒ Object
- #clone ⇒ Object
- #context(*context_data) ⇒ Object
- #count(metric, value = 1) ⇒ Object
- #fire! ⇒ Object
-
#initialize(configuration: Configuration.new) ⇒ Emitter
constructor
A new instance of Emitter.
- #log(*args) ⇒ Object
- #measure(metric, value, unit: nil) ⇒ Object
- #merge!(*args) ⇒ Object
- #sample(metric, value, unit: nil) ⇒ Object
- #silence ⇒ Object
- #silence! ⇒ Object
- #unique(metric, value) ⇒ Object
- #unsilence! ⇒ Object
- #with_elapsed(start_time = Time.now, &block) ⇒ Object
- #with_output(output) ⇒ Object
Constructor Details
#initialize(configuration: Configuration.new) ⇒ Emitter
Returns a new instance of Emitter.
7 8 9 10 11 12 13 |
# File 'lib/l2meter/emitter.rb', line 7 def initialize(configuration: Configuration.new) @configuration = configuration @buffer = {} @autoflush = true @contexts = [] @outputs = [] end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
5 6 7 |
# File 'lib/l2meter/emitter.rb', line 5 def configuration @configuration end |
Instance Method Details
#batch ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/l2meter/emitter.rb', line 82 def batch autoflush = @autoflush @autoflush = false yield ensure @autoflush = autoflush write end |
#clone ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/l2meter/emitter.rb', line 72 def clone cloned_contexts = @contexts.clone cloned_outputs = @outputs.clone self.class.new(configuration: configuration).instance_eval do @contexts = cloned_contexts @outputs = cloned_outputs self end end |
#context(*context_data) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/l2meter/emitter.rb', line 64 def context(*context_data) return clone_with_context(context_data) unless block_given? push_context context_data yield ensure context_data.length.times { @contexts.pop } if block_given? end |
#count(metric, value = 1) ⇒ Object
56 57 58 |
# File 'lib/l2meter/emitter.rb', line 56 def count(metric, value = 1) log_with_prefix :count, metric, value end |
#fire! ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/l2meter/emitter.rb', line 98 def fire! tokens = @buffer.map { |key, value| build_token(key, value) } tokens.compact! tokens.sort! if configuration.sort? output_queue.last.print tokens.join(SPACE) << NL if tokens.any? ensure @buffer.clear end |
#log(*args) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/l2meter/emitter.rb', line 15 def log(*args) merge! *current_contexts, *args if block_given? wrap &proc else write end end |
#measure(metric, value, unit: nil) ⇒ Object
48 49 50 |
# File 'lib/l2meter/emitter.rb', line 48 def measure(metric, value, unit: nil) log_with_prefix :measure, metric, value, unit: unit end |
#merge!(*args) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/l2meter/emitter.rb', line 91 def merge!(*args) unwrap(args).each do |key, value| key = configuration.key_formatter.call(key) @buffer[key] = value end end |
#sample(metric, value, unit: nil) ⇒ Object
52 53 54 |
# File 'lib/l2meter/emitter.rb', line 52 def sample(metric, value, unit: nil) log_with_prefix :sample, metric, value, unit: unit end |
#silence ⇒ Object
36 37 38 |
# File 'lib/l2meter/emitter.rb', line 36 def silence with_output(NullObject.new, &proc) end |
#silence! ⇒ Object
40 41 42 |
# File 'lib/l2meter/emitter.rb', line 40 def silence! @outputs.push NullObject.new end |
#unique(metric, value) ⇒ Object
60 61 62 |
# File 'lib/l2meter/emitter.rb', line 60 def unique(metric, value) log_with_prefix :unique, metric, value end |
#unsilence! ⇒ Object
44 45 46 |
# File 'lib/l2meter/emitter.rb', line 44 def unsilence! @outputs.pop end |
#with_elapsed(start_time = Time.now, &block) ⇒ Object
25 26 27 |
# File 'lib/l2meter/emitter.rb', line 25 def with_elapsed(start_time = Time.now, &block) context(elapsed_context(start_time), &block) end |
#with_output(output) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/l2meter/emitter.rb', line 29 def with_output(output) @outputs.push output yield ensure @outputs.pop end |