Class: Txray::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/txray/monitor.rb

Constant Summary collapse

CAP =
2000
BUCKETS =
[ [ 10, "<10ms" ], [ 50, "<50ms" ], [ 100, "<100ms" ], [ 250, "<250ms" ],
[ 1000, "<1s" ], [ Float::INFINITY, "1s+" ] ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold_ms: 250) ⇒ Monitor

Returns a new instance of Monitor.



11
12
13
14
15
16
17
# File 'lib/txray/monitor.rb', line 11

def initialize(threshold_ms: 250)
  @threshold_ms = threshold_ms
  @transactions = []
  @violations = []
  @recent = []
  @started_at = Time.now
end

Instance Attribute Details

#started_atObject (readonly)

Returns the value of attribute started_at.



9
10
11
# File 'lib/txray/monitor.rb', line 9

def started_at
  @started_at
end

#transactionsObject (readonly)

Returns the value of attribute transactions.



9
10
11
# File 'lib/txray/monitor.rb', line 9

def transactions
  @transactions
end

#violationsObject (readonly)

Returns the value of attribute violations.



9
10
11
# File 'lib/txray/monitor.rb', line 9

def violations
  @violations
end

Instance Method Details

#absorb(line) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/txray/monitor.rb', line 19

def absorb(line)
  event = JSON.parse(line, symbolize_names: true)
  case event[:type]
  when "transaction" then add(@transactions, event)
  when "violation" then add(@violations, event)
  else return nil
  end
  @recent.unshift(event)
  @recent.pop while @recent.size > 40
  event
rescue JSON::ParserError
  nil
end

#breakdownObject



39
40
41
42
43
# File 'lib/txray/monitor.rb', line 39

def breakdown
  flagged = @transactions.count { |event| event[:violations].to_a.any? }
  slow = @transactions.count { |event| slow?(event) && event[:violations].to_a.empty? }
  { ok: @transactions.size - flagged - slow, slow: slow, flagged: flagged }
end

#durationsObject



52
# File 'lib/txray/monitor.rb', line 52

def durations = @transactions.map { |event| event[:duration_ms].to_f }

#empty?Boolean

Returns:

  • (Boolean)


57
# File 'lib/txray/monitor.rb', line 57

def empty? = @transactions.empty? && @violations.empty?

#findingsObject



66
67
68
69
70
71
# File 'lib/txray/monitor.rb', line 66

def findings
  nested = @transactions.flat_map { |event| event[:violations].to_a }
  slow = @transactions.select { |event| event[:duration_ms].to_f >= @threshold_ms }
                      .map { |event| { rule: "slow-transaction", source: event[:source] } }
  @violations + nested + slow
end

#flaggedObject



55
# File 'lib/txray/monitor.rb', line 55

def flagged = @transactions.count { |event| event[:violations].to_a.any? }

#histogramObject



45
46
47
48
49
50
# File 'lib/txray/monitor.rb', line 45

def histogram
  BUCKETS.map do |limit, label|
    { label: label, limit: limit,
      count: durations.count { |value| value < limit && value >= previous_limit(limit) } }
  end
end

#hotspots(limit) ⇒ Object



73
74
75
76
77
78
# File 'lib/txray/monitor.rb', line 73

def hotspots(limit)
  findings.group_by { |event| [ event[:rule], event[:source] ] }
          .map { |(rule, source), events| { rule: rule, source: source, count: events.size } }
          .sort_by { |entry| -entry[:count] }
          .first(limit)
end

#percentile(fraction) ⇒ Object



59
60
61
62
63
64
# File 'lib/txray/monitor.rb', line 59

def percentile(fraction)
  values = durations.sort
  return 0.0 if values.empty?

  values[[ (values.size * fraction).ceil - 1, 0 ].max]
end

#pidsObject



37
# File 'lib/txray/monitor.rb', line 37

def pids = @transactions.map { |event| event[:pid] }.uniq.size

#recent(limit) ⇒ Object



36
# File 'lib/txray/monitor.rb', line 36

def recent(limit) = @recent.first(limit)

#slowObject



53
# File 'lib/txray/monitor.rb', line 53

def slow = @transactions.count { |event| slow?(event) }

#slow?(event) ⇒ Boolean

Returns:

  • (Boolean)


54
# File 'lib/txray/monitor.rb', line 54

def slow?(event) = event[:duration_ms].to_f >= @threshold_ms

#uptimeObject



56
# File 'lib/txray/monitor.rb', line 56

def uptime = Time.now - @started_at