Module: Evidence

Defined in:
lib/evidence.rb,
lib/evidence/stream.rb,
lib/evidence/log_parser.rb,
lib/evidence/rails_action_parser.rb

Defined Under Namespace

Modules: Stream Classes: EnumStream, LogParser, MergedStream, PipeStream, RailsActionParser, SliceStream, SlicedStreams

Class Method Summary collapse

Class Method Details

.counterObject



189
190
191
192
193
# File 'lib/evidence/stream.rb', line 189

def counter
  count = 0
  counter = Enumerator.new { |y| loop { y << (count += 1) } }
  stream(counter)
end

.default_unmatched_processObject



48
49
50
# File 'lib/evidence.rb', line 48

def default_unmatched_process
  lambda {|log| }
end

.littles_law_analysisObject

Do the little’s law analysis on rails actions stream with request_timestamp_parser usage example:

log stream | rails_action_parser(pid, message) | request_timestamp_parser | slice_stream(lambda {|action| action[:request][:timestamp]}, 60) | littles_law_analysis


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/evidence.rb', line 33

def littles_law_analysis
  lambda do |output|
    lambda do |actions|
      statistics = actions[:stream].inject(sum: 0, count: 0) do |memo, action|
        memo[:count] += 1
        memo[:sum] += action[:response][:completed_time].to_i
        memo
      end
      avg_sec_arrival_rate = statistics[:count].to_f/(actions[:range].max - actions[:range].min)
      avg_sec_response_time = statistics[:sum].to_f / statistics[:count] /1000
      output[range: actions[:range], value: avg_sec_arrival_rate * avg_sec_response_time]
    end
  end
end

.log_parser(pattern, unmatched = default_unmatched_process) ⇒ Object

Parse log file stream by given pattern

pattern: ruby regex expression, has named group specified
unmatched processor: process all unmatched log


11
12
13
# File 'lib/evidence.rb', line 11

def log_parser(pattern, unmatched=default_unmatched_process)
  LogParser.new(pattern, unmatched)
end

.merge_streams(streams, comparator) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/evidence/stream.rb', line 157

def merge_streams(streams, comparator)
  loop do
    s1 = streams.shift
    return s1 if streams.empty?
    s2 = streams.shift
    streams << MergedStream.new([s1, s2], comparator)
  end
end

.rails_action_parser(pid, message, unmatched = default_unmatched_process) ⇒ Object

Parse out rails actions by given:

pid: a lambda returns process id used to group logs
message: a lambda returns rails log string message


18
19
20
# File 'lib/evidence.rb', line 18

def rails_action_parser(pid, message, unmatched=default_unmatched_process)
  RailsActionParser.new(pid, message, unmatched)
end

.request_timestamp_parser(format = "%Y-%m-%d %H:%M:%S") ⇒ Object

Rails action request timestamp parser

log stream | rails_action_parser(pid, message) | request_timestamp_parser


24
25
26
27
28
# File 'lib/evidence.rb', line 24

def request_timestamp_parser(format="%Y-%m-%d %H:%M:%S")
  stream_each do |action|
    action[:request][:timestamp] = Time.strptime(action[:request][:timestamp], format)
  end
end

.slice_stream(index, step, start_index = nil) ⇒ Object



166
167
168
169
# File 'lib/evidence/stream.rb', line 166

def slice_stream(index, step, start_index=nil)
  end_index = step.is_a?(Proc) ? step : lambda { |index| index + step }
  SliceStream.new(index, start_index, end_index)
end

.stream(obj) ⇒ Object



153
154
155
# File 'lib/evidence/stream.rb', line 153

def stream(obj)
  EnumStream.new(obj.to_enum)
end

.stream_each(&block) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/evidence/stream.rb', line 171

def stream_each(&block)
  lambda do |output|
    lambda do |i|
      block[i]
      output[i]
    end
  end
end

.stream_filter(&block) ⇒ Object Also known as: stream_select



184
185
186
# File 'lib/evidence/stream.rb', line 184

def stream_filter(&block)
  lambda { |output| lambda { |i| output[i] if block[i] } }
end

.stream_map(&block) ⇒ Object



180
181
182
# File 'lib/evidence/stream.rb', line 180

def stream_map(&block)
  lambda { |output| lambda { |i| output[block[i]] } }
end