File: app.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (149 lines) | stat: -rw-r--r-- 3,160 bytes parent folder | download
1
2
3
4
5
6
7
8
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
45
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true

require 'mongo'
require 'json'

class StatsAggregator

  def initialize
    @open_connections = 0
    @heartbeats_count = 0
    @total_heartbeat_time = 0
    @commands_count = 0
    @total_command_time = 0
  end

  def add_command(duration)
    @commands_count += 1
    @total_command_time += duration
  end

  def add_heartbeat(duration)
    @heartbeats_count += 1
    @total_heartbeat_time += duration
  end

  def add_connection
    @open_connections += 1
  end

  def remove_connection
    @open_connections -= 1
  end

  def average_heartbeat_time
    if @heartbeats_count == 0
      0
    else
      @total_heartbeat_time / @heartbeats_count
    end
  end

  def average_command_time
    if @commands_count == 0
      0
    else
      @total_command_time / @commands_count
    end
  end

  def reset
    @open_connections = 0
    @heartbeats_count = 0
    @total_heartbeat_time = 0
    @commands_count = 0
    @total_command_time = 0
  end

  def result
    {
      average_heartbeat_time: average_heartbeat_time,
      average_command_time: average_command_time,
      heartbeats_count: @heartbeats_count,
      open_connections: @open_connections,
    }
  end
end

class CommandMonitor

  def initialize(stats_aggregator)
    @stats_aggregator = stats_aggregator
  end

  def started(event); end

  def failed(event)
    @stats_aggregator.add_command(event.duration)
  end

  def succeeded(event)
    @stats_aggregator.add_command(event.duration)
  end
end

class HeartbeatMonitor

  def initialize(stats_aggregator)
    @stats_aggregator = stats_aggregator
  end

  def started(event); end

  def succeeded(event)
    @stats_aggregator.add_heartbeat(event.duration)
  end

  def failed(event)
    @stats_aggregator.add_heartbeat(event.duration)
  end
end

class PoolMonitor

  def initialize(stats_aggregator)
    @stats_aggregator = stats_aggregator
  end

  def published(event)
    case event
    when Mongo::Monitoring::Event::Cmap::ConnectionCreated
      @stats_aggregator.add_connection
    when Mongo::Monitoring::Event::Cmap::ConnectionClosed
      @stats_aggregator.remove_connection
    end
  end
end

$stats_aggregator = StatsAggregator.new

command_monitor = CommandMonitor.new($stats_aggregator)
heartbeat_monitor = HeartbeatMonitor.new($stats_aggregator)
pool_monitor = PoolMonitor.new($stats_aggregator)

sdam_proc = proc do |client|
  client.subscribe(Mongo::Monitoring::COMMAND, command_monitor)
  client.subscribe(Mongo::Monitoring::SERVER_HEARTBEAT, heartbeat_monitor)
  client.subscribe(Mongo::Monitoring::CONNECTION_POOL, pool_monitor)
end

puts 'Connecting'
$client = Mongo::Client.new(ENV['MONGODB_URI'], sdam_proc: sdam_proc)
# Populate the connection pool
$client.use('lambda_test').database.list_collections
puts 'Connected'

def lambda_handler(event:, context:)
  db = $client.use('lambda_test')
  collection = db[:test_collection]
  result = collection.insert_one({ name: 'test' })
  collection.delete_one({ _id: result.inserted_id })
  response = $stats_aggregator.result.to_json
  $stats_aggregator.reset
  puts "Response: #{response}"

  {
    statusCode: 200,
    body: response
  }
end