Module: LanGrove::LogMonitor::Sampler

Defined in:
lib/langrove/ext/log_monitor.rb

Class Method Summary collapse

Class Method Details

.init(root) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/langrove/ext/log_monitor.rb', line 9

def init( root )

  return if @initialized

  @root = root

  @root.logger.debug "#{self}.init( #{root} )"

  @root.metrix.register_sampler( 

    :type => :handler, 
    :sampler => self,
    :accumulator => proc { |component_type, component, state_doc|

      #
      # The component will only be sampled 
      # if it is a LogMonitor
      #

      self.sample_accumulate( 

        component_type, component, state_doc 

      ) if component.respond_to?( :log_monitor_event )

    }

  )

  @initialized = true

  reset

  return self

end

.resetObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/langrove/ext/log_monitor.rb', line 174

def reset

  #
  # This may be quite nasty to the garbage collector...
  # 

  @counters = {

    :count => 0,

    #
    # current state
    #
    :current_success => 0,
    :current_fail  => 0,

    #
    # ever succeeded
    #
    :success_ever => 0,
    :success_never => 0,

    #
    # age in seconds of each success
    #
    :success_age => [],
    :last_run_age => [],

    #
    # known errors 
    #
    :known_errors => {},
    :known_errors_instance => 0,
    :known_errors_total => 0,    # includes repeat count

    #
    # unknown errors
    #
    :unknown_errors => {},
    :unknown_errors_instance => 0,
    :unknown_errors_total => 0


  }

end

.sample_accumulate(component_type, component, state_doc) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/langrove/ext/log_monitor.rb', line 46

def sample_accumulate( component_type, component, state_doc )

  #@root.logger.debug "#{self}.sample_accumulate( #{component_type}, #{component}, #{state_doc} )"

  @name = component.class.to_s.split(' ')[0]
  
  @counters[:count] += 1

  if component_type == :handler

    if component.capsule['success']

      @counters[:current_success] += 1

    else 

      @counters[:current_fail] += 1

    end

    now = Time.now.to_i

    if component.capsule['success_at']

      @counters[:success_ever] += 1

      at = component.capsule['success_at'].strftime( '%s' )

      @counters[:success_age] << now - at.to_i

    else 

      @counters[:success_never] += 1

    end

    if component.capsule['last_ran_at']

      at = component.capsule['last_ran_at'].strftime( '%s' )

      @counters[:last_run_age] << now - at.to_i

    end

    if component.capsule['known_errors']

      component.capsule['known_errors'].each do |k,v|

        if @counters[:known_errors][k].nil?

          @counters[:known_errors][k] = 1

        else 

          @counters[:known_errors][k] += 1

        end

        @counters[:known_errors_instance] += 1
        @counters[:known_errors_total] += v

      end

    end

    if component.capsule['unknown_errors']

      component.capsule['unknown_errors'].each do |k,v|

        if @counters[:unknown_errors][k].nil?

          @counters[:unknown_errors][k] = 1

        else 

          @counters[:unknown_errors][k] += 1

        end

        @counters[:unknown_errors_instance] += 1
        @counters[:unknown_errors_total] += v

      end

    end

  end

end

.sample_collate(component_type, state_doc) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/langrove/ext/log_monitor.rb', line 136

def sample_collate( component_type, state_doc )

  #@root.logger.debug "#{self}.sample_collate( #{component_type}, #{state_doc} )"

  success_age = 0.0
  @counters[:success_age].each { |v| success_age += v }

  last_run_age = 0.0
  @counters[:last_run_age].each { |v| last_run_age += v }

  state_doc[ @name ] = {

    :count => @counters[:count],

    :current_success => @counters[:current_success],
    :current_fail => @counters[:current_fail],

    :success_ever => @counters[:success_ever],
    :success_never => @counters[:success_never],

    :avg_success_age => success_age  / @counters[:success_age].length,
    :age_last_run_age => last_run_age  / @counters[:last_run_age].length,

    :known_errors_instance => @counters[:known_errors_instance],
    :known_errors_total => @counters[:known_errors_total],

    :unknown_errors_instance => @counters[:unknown_errors_instance],
    :unknown_errors_total => @counters[:unknown_errors_total],

    :known_errors => @counters[:known_errors],
    :unknown_errors => @counters[:unknown_errors],

  }

  reset 

end