Class: L2meter::Emitter

Inherits:
Object
  • Object
show all
Defined in:
lib/l2meter/emitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: Configuration.new) ⇒ Emitter

Returns a new instance of Emitter.



5
6
7
8
9
10
11
12
# File 'lib/l2meter/emitter.rb', line 5

def initialize(configuration: Configuration.new)
  @configuration = configuration
  @buffer = {}
  @autoflush = true
  @start_times = []
  @contexts = []
  @outputs = []
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



3
4
5
# File 'lib/l2meter/emitter.rb', line 3

def configuration
  @configuration
end

Instance Method Details

#batchObject



79
80
81
82
83
84
85
# File 'lib/l2meter/emitter.rb', line 79

def batch
  @autoflush = false
  yield
ensure
  @autoflush = true
  flush_buffer
end

#cloneObject



71
72
73
74
75
76
77
# File 'lib/l2meter/emitter.rb', line 71

def clone
  cloned_contexts = @contexts.clone
  self.class.new(configuration: configuration).instance_eval do
    @contexts = cloned_contexts
    self
  end
end

#context(*context_data) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/l2meter/emitter.rb', line 63

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



55
56
57
# File 'lib/l2meter/emitter.rb', line 55

def count(metric, value = 1)
  log_with_prefix :count, metric, value
end

#log(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/l2meter/emitter.rb', line 14

def log(*args)
  params = unwrap(*args)
  params = merge_contexts(params)

  if block_given?
    wrap params, &proc
  else
    write params
  end
end

#measure(metric, value, unit: nil) ⇒ Object



47
48
49
# File 'lib/l2meter/emitter.rb', line 47

def measure(metric, value, unit: nil)
  log_with_prefix :measure, metric, value, unit: unit
end

#sample(metric, value, unit: nil) ⇒ Object



51
52
53
# File 'lib/l2meter/emitter.rb', line 51

def sample(metric, value, unit: nil)
  log_with_prefix :sample, metric, value, unit: unit
end

#silenceObject



32
33
34
35
36
37
# File 'lib/l2meter/emitter.rb', line 32

def silence
  silence!
  yield
ensure
  unsilence!
end

#silence!Object



39
40
41
# File 'lib/l2meter/emitter.rb', line 39

def silence!
  @outputs.push NullObject.new
end

#unique(metric, value) ⇒ Object



59
60
61
# File 'lib/l2meter/emitter.rb', line 59

def unique(metric, value)
  log_with_prefix :unique, metric, value
end

#unsilence!Object



43
44
45
# File 'lib/l2meter/emitter.rb', line 43

def unsilence!
  @outputs.pop
end

#with_elapsedObject



25
26
27
28
29
30
# File 'lib/l2meter/emitter.rb', line 25

def with_elapsed
  @start_times << Time.now
  yield
ensure
  @start_times.pop
end