Module: L2Metable

Defined in:
lib/l2metable.rb,
lib/l2metable/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#count(subcomponent, value = 1, extras = {}) ⇒ Object



64
65
66
67
# File 'lib/l2metable.rb', line 64

def count(subcomponent, value=1, extras ={})
  comp = log_component(subcomponent)
  log_hash({("count#"+ comp) => value}.merge(extras))
end

#dotted(*pieces) ⇒ Object



106
107
108
# File 'lib/l2metable.rb', line 106

def dotted(*pieces)
  (pieces.reject { |e| e.nil? || (e.respond_to?('empty?') && e.empty?) }).join('.')
end

#join_hash(hash = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/l2metable.rb', line 90

def join_hash(hash = {})
  hash.map do |k, v|
    if v.is_a? Float
      # workaround for old Ruby version on Bamboo not having parameterized round()
      v = (v * 1000).round() / 1000.0
    elsif v.to_s =~ /\s/
      v = "\"#{v}\""
    end
    "#{k}=#{v}"
  end.join(" ")
end

#log_base_hashObject

Descendants should override and implement a specific base hash for all log lines, if applicable e.g. => @app_id, :xid => context.xid



15
16
17
# File 'lib/l2metable.rb', line 15

def log_base_hash()
  {}
end

#log_component(subcomponent) ⇒ Object

Descendants should override and implement a specific component prefix for all log lines e.g. “widget#subcomponent”



8
9
10
# File 'lib/l2metable.rb', line 8

def log_component(subcomponent)
  subcomponent
end

#log_exception(subcomponent, e, extras = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/l2metable.rb', line 69

def log_exception(subcomponent, e, extras = {})
  exception_suffix = "exception"
  extras = {:at => dotted(log_component(subcomponent), exception_suffix)}.merge(extras)

  if e.nil? || !e.is_a?(Exception)
    measure(exception_suffix, 1, extras)
    return
  end

  exception_id = e.object_id.abs
  trace = e.backtrace.reduce do |memo, line|
    memo + line.gsub(/[`'"]/, "") + "\n"
  end
  measure(exception_suffix, 1, {
    :exception_id => exception_id,
    :class => e.class,
    :message => e.message,
    :trace => trace
  }.merge(extras))
end

#log_hash(hash = {}) ⇒ Object



102
103
104
# File 'lib/l2metable.rb', line 102

def log_hash(hash = {})
  log_write(join_hash(log_base_hash.merge(hash)))
end

#log_write(msg) ⇒ Object

Descendants should override and implement a specific log handler e.g. @logger.write(full_msg)



22
23
24
# File 'lib/l2metable.rb', line 22

def log_write(msg)
  puts msg
end

#measure(subcomponent, value, extras = {}) ⇒ Object



54
55
56
57
# File 'lib/l2metable.rb', line 54

def measure(subcomponent, value, extras = {})
  comp = log_component(subcomponent)
  log_hash({("measure#"+ comp) => value}.merge(extras))
end

#monitor(subcomponent, extras = {}, &blk) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/l2metable.rb', line 26

def monitor(subcomponent, extras = {}, &blk)
  unless block_given?
    # should never enter, but logging with something obvious instead of blowing up at runtime
    measure(dotted(subcomponent, "MONITOR_WITHOUT_BLOCK"), 1, extras)
    return
  end

  start = Time.now
  measure(dotted(subcomponent, "start"), 1, extras)

  res = nil
  ex = nil
  begin
    res = yield
  rescue => e
    log_exception(subcomponent, e, extras)
    ex = e
  end

  measure(dotted(subcomponent, "time"), Time.now - start, extras)

  if ex
    raise(ex)
  else
    res
  end
end

#sample(subcomponent, value, extras = {}) ⇒ Object



59
60
61
62
# File 'lib/l2metable.rb', line 59

def sample(subcomponent, value, extras = {})
  comp = log_component(subcomponent)
  log_hash({("sample#"+ comp) => value}.merge(extras))
end