File: sdam_formatter_integration.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 (119 lines) | stat: -rw-r--r-- 3,538 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
# frozen_string_literal: true
# rubocop:todo all

$sdam_formatter_lock = Mutex.new

module SdamFormatterIntegration
  def log_entries
    @log_entries ||= []
  end
  module_function :log_entries

  def clear_log_entries
    @log_entries = []
  end
  module_function :clear_log_entries

  def assign_log_entries(example_id)
    $sdam_formatter_lock.synchronize do
      @log_entries_by_example_id ||= {}
      @log_entries_by_example_id[example_id] ||= []
      @log_entries_by_example_id[example_id] += log_entries
      clear_log_entries
    end
  end
  module_function :assign_log_entries

  def example_log_entries(example_id)
    $sdam_formatter_lock.synchronize do
      @log_entries_by_example_id ||= {}
      @log_entries_by_example_id[example_id]
    end
  end
  module_function :example_log_entries

  def subscribe
    topology_opening_subscriber = TopologyOpeningLogSubscriber.new
    server_opening_subscriber = ServerOpeningLogSubscriber.new
    server_description_changed_subscriber = ServerDescriptionChangedLogSubscriber.new
    topology_changed_subscriber = TopologyChangedLogSubscriber.new
    server_closed_subscriber = ServerClosedLogSubscriber.new
    topology_closed_subscriber = TopologyClosedLogSubscriber.new

    Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_OPENING,
      topology_opening_subscriber)
    Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_OPENING,
      server_opening_subscriber)
    Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_DESCRIPTION_CHANGED,
      server_description_changed_subscriber)
    Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_CHANGED,
      topology_changed_subscriber)
    Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_CLOSED,
      server_closed_subscriber)
    Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_CLOSED,
      topology_closed_subscriber)
  end
  module_function :subscribe

  class SDAMLogSubscriber
    def succeeded(event)
      SdamFormatterIntegration.log_entries <<
        Time.now.strftime('%Y-%m-%d %H:%M:%S.%L %z') + ' | ' + format_event(event)
    end
  end

  class TopologyOpeningLogSubscriber < SDAMLogSubscriber
    private

    def format_event(event)
      "Topology type '#{event.topology.display_name}' initializing."
    end
  end

  class ServerOpeningLogSubscriber < SDAMLogSubscriber
    private

    def format_event(event)
      "Server #{event.address} initializing."
    end
  end

  class ServerDescriptionChangedLogSubscriber < SDAMLogSubscriber
    private

    def format_event(event)
      "Server description for #{event.address} changed from " +
      "'#{event.previous_description.server_type}' to '#{event.new_description.server_type}'."
    end
  end

  class TopologyChangedLogSubscriber < SDAMLogSubscriber
    private

    def format_event(event)
      if event.previous_topology != event.new_topology
        "Topology type '#{event.previous_topology.display_name}' changed to " +
        "type '#{event.new_topology.display_name}'."
      else
        "There was a change in the members of the '#{event.new_topology.display_name}' " +
        "topology."
      end
    end
  end

  class ServerClosedLogSubscriber < SDAMLogSubscriber
    private

    def format_event(event)
      "Server #{event.address} connection closed."
    end
  end

  class TopologyClosedLogSubscriber < SDAMLogSubscriber
    private

    def format_event(event)
      "Topology type '#{event.topology.display_name}' closed."
    end
  end
end