Module: FnordMetric::GaugeModifiers

Included in:
Context, Gauge
Defined in:
lib/fnordmetric/gauge_modifiers.rb

Instance Method Summary collapse

Instance Method Details

#assure_has_series!Object



100
101
102
103
# File 'lib/fnordmetric/gauge_modifiers.rb', line 100

def assure_has_series!
  return true if has_series?
  error! "error: #{caller[0].split(" ")[-1]} can only be used with series gauges" 
end

#assure_non_progressive!(gauge) ⇒ Object



115
116
117
118
# File 'lib/fnordmetric/gauge_modifiers.rb', line 115

def assure_non_progressive!(gauge)
  return true unless gauge.progressive?
  error! "error: #{caller[0].split(" ")[-1]} can only be used with non-progressive gauges" 
end

#assure_series_exists!(series_name) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fnordmetric/gauge_modifiers.rb', line 120

def assure_series_exists!(series_name)
  if series_name == :default && @opts[:series].size > 1
    error! "gauge '#{name}': don't know which series to increment"
  elsif series_name == :default
    return @opts[:series].first
  elsif !series_name.respond_to?(:to_sym) || !@opts[:series].include?(series_name.to_sym)
    error! "gauge '#{name}': unknown series: #{series_name}"
  else 
    return series_name
  end
end

#assure_three_dimensional!(gauge) ⇒ Object



110
111
112
113
# File 'lib/fnordmetric/gauge_modifiers.rb', line 110

def assure_three_dimensional!(gauge)
  return true unless gauge.two_dimensional?
  error! "error: #{caller[0].split(" ")[-1]} can only be used with 3-dimensional gauges" 
end

#assure_two_dimensional!(gauge) ⇒ Object



105
106
107
108
# File 'lib/fnordmetric/gauge_modifiers.rb', line 105

def assure_two_dimensional!(gauge)
  return true if gauge.two_dimensional?
  error! "error: #{caller[0].split(" ")[-1]} can only be used with 2-dimensional gauges" 
end

#incr(gauge_name, value = 1) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/fnordmetric/gauge_modifiers.rb', line 3

def incr(gauge_name, value=1)
  value = value.to_i
  gauge = fetch_gauge(gauge_name)
  assure_two_dimensional!(gauge)
  if gauge.unique? 
    incr_uniq(gauge, value)
  elsif gauge.average? 
    incr_avg(gauge, value)
  else
    incr_tick(gauge, value)
  end
end

#incr_avg(gauge, value) ⇒ Object



42
43
44
45
46
# File 'lib/fnordmetric/gauge_modifiers.rb', line 42

def incr_avg(gauge, value)
  @redis.hincrby(gauge.key(:"mean-counts"), gauge.tick_at(time), 1).callback do
    incr_tick(gauge, value)
  end
end

#incr_denominator(ctx, series_name = :default, value = 1, prog = false) ⇒ Object



80
81
82
# File 'lib/fnordmetric/gauge_modifiers.rb', line 80

def incr_denominator(ctx, series_name=:default, value=1, prog=false)
  incr_fraction(ctx, series_name, :denominator, value, prog)
end

#incr_field(gauge_name, field_name, value = 1) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/fnordmetric/gauge_modifiers.rb', line 48

def incr_field(gauge_name, field_name, value=1)
  gauge = fetch_gauge(gauge_name)
  assure_three_dimensional!(gauge)
  if gauge.unique? 
    incr_uniq(gauge, value, field_name)
  else
    incr_field_by(gauge, field_name, value)
  end
end

#incr_field_by(gauge, field_name, value) ⇒ Object



58
59
60
61
62
# File 'lib/fnordmetric/gauge_modifiers.rb', line 58

def incr_field_by(gauge, field_name, value)
  @redis.zincrby(gauge.tick_key(time), value, field_name).callback do
    @redis.incrby(gauge.tick_key(time, :count), 1)
  end
end

#incr_fraction(ctx, series_name, part, value, prog) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fnordmetric/gauge_modifiers.rb', line 84

def incr_fraction(ctx, series_name, part, value, prog)
  return unless series_name = assure_series_exists!(series_name)
  assure_has_series!

  at = ctx.send(:time)
  value = parse_numeric(value)

  if prog
    raise "FIXPAUL: not yet implemented: progressive fraction gauges"
  end

  ctx.redis_exec(:hincrby, retention_key(at, series_name), "#{tick_at(at)}-#{part}", value).callback do 
    ctx.redis_exec :expire,  retention_key(at, series_name)
  end
end

#incr_numerator(ctx, series_name = :default, value = 1, prog = false) ⇒ Object



76
77
78
# File 'lib/fnordmetric/gauge_modifiers.rb', line 76

def incr_numerator(ctx, series_name=:default, value=1, prog=false)
  incr_fraction(ctx, series_name, :numerator, value, prog)
end

#incr_tick(gauge, value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fnordmetric/gauge_modifiers.rb', line 16

def incr_tick(gauge, value)
  if gauge.progressive?
    @redis.incrby(gauge.key(:head), value).callback do |head|
      @redis.hsetnx(gauge.key, gauge.tick_at(time), head).callback do |_new|
        @redis.hincrby(gauge.key, gauge.tick_at(time), value) unless _new
      end
    end
  else
    @redis.hsetnx(gauge.key, gauge.tick_at(time), 0).callback do
      @redis.hincrby(gauge.key, gauge.tick_at(time), value)
    end
  end
end

#incr_uniq(gauge, value, field_name = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fnordmetric/gauge_modifiers.rb', line 30

def incr_uniq(gauge, value, field_name=nil)
  return false if session_key.blank?
  @redis.sadd(gauge.tick_key(time, :sessions), session_key).callback do |_new|
    @redis.expire(gauge.tick_key(time, :sessions), gauge.tick)
    if (_new == 1) || (_new == true) #redis vs. em-redis
      @redis.incr(gauge.tick_key(time, :"sessions-count")).callback do |sc|
        field_name ? incr_field_by(gauge, field_name, value) : incr_tick(gauge, value)
      end
    end
  end
end

#parse_numeric(val) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/fnordmetric/gauge_modifiers.rb', line 132

def parse_numeric(val)
  if val.is_a?(Numeric)
    return val
  elsif val.is_a?(String) && val.match(/[0-9]+/)
    val.to_i
  elsif val.is_a?(String) && val.match(/[0-9]+(\.|,)[0-9]+/)
    val.to_f
  else
    error! "gauge '#{name}': incr called with non-numerical value: #{val}"
  end
end

#set_field(gauge_name, field_name, value) ⇒ Object



70
71
72
73
74
# File 'lib/fnordmetric/gauge_modifiers.rb', line 70

def set_field(gauge_name, field_name, value)
  gauge = fetch_gauge(gauge_name)
  assure_three_dimensional!(gauge)
  @redis.zadd(gauge.tick_key(time), value, field_name)
end

#set_value(gauge_name, value) ⇒ Object



64
65
66
67
68
# File 'lib/fnordmetric/gauge_modifiers.rb', line 64

def set_value(gauge_name, value)
  gauge = fetch_gauge(gauge_name)
  assure_two_dimensional!(gauge)
  @redis.hset(gauge.key, gauge.tick_at(time), value)
end