Class: L2meter::Emitter

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

Constant Summary collapse

BARE_VALUE_SENTINEL =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: Configuration.new) ⇒ Emitter

Returns a new instance of Emitter.



9
10
11
# File 'lib/l2meter/emitter.rb', line 9

def initialize(configuration: Configuration.new)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



5
6
7
# File 'lib/l2meter/emitter.rb', line 5

def configuration
  @configuration
end

Instance Method Details

#batchObject



57
58
59
60
61
62
63
64
# File 'lib/l2meter/emitter.rb', line 57

def batch
  old_state = in_batch?
  in_batch!
  yield
ensure
  reset_in_batch(old_state)
  write
end

#cloneObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/l2meter/emitter.rb', line 82

def clone
  original_contexts = dynamic_contexts
  original_output = output
  self.class.new(configuration: configuration).tap do |clone|
    clone.instance_eval do
      dynamic_contexts.concat(original_contexts)
      set_output original_output
    end
  end
end

#context(*context_data, &block) ⇒ Object



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

def context(*context_data, &block)
  if block
    wrap_context(context_data, &block)
  else
    contexted(context_data)
  end
end

#count(metric, value = 1) ⇒ Object



74
75
76
# File 'lib/l2meter/emitter.rb', line 74

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

#log(*args, &block) ⇒ Object



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

def log(*args, &block)
  merge!(current_context, *args)

  if block
    wrap(&block)
  else
    write
  end
end

#measure(metric, value, **args) ⇒ Object



66
67
68
# File 'lib/l2meter/emitter.rb', line 66

def measure(metric, value, **args)
  log_with_prefix(:measure, metric, value, **args)
end

#sample(metric, value, **args) ⇒ Object



70
71
72
# File 'lib/l2meter/emitter.rb', line 70

def sample(metric, value, **args)
  log_with_prefix(:sample, metric, value, **args)
end

#silence(&block) ⇒ Object



37
38
39
# File 'lib/l2meter/emitter.rb', line 37

def silence(&block)
  with_output(NullOutput.new, &block)
end

#silence!Object



41
42
43
# File 'lib/l2meter/emitter.rb', line 41

def silence!
  set_output(NullOutput.new)
end

#unique(metric, value) ⇒ Object



78
79
80
# File 'lib/l2meter/emitter.rb', line 78

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

#unsilence!Object



45
46
47
# File 'lib/l2meter/emitter.rb', line 45

def unsilence!
  set_output(nil)
end

#with_elapsedObject



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

def with_elapsed
  context elapsed: elapse do
    yield
  end
end

#with_output(new_output) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/l2meter/emitter.rb', line 49

def with_output(new_output)
  old_output = output
  set_output(new_output)
  yield
ensure
  set_output(old_output)
end