Class: Observed::ConfigBuilder

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/observed/config_builder.rb

Defined Under Namespace

Classes: ObserverCompatibilityAdapter

Instance Method Summary collapse

Methods included from Configurable

#configure, #get_attribute_value, #has_attribute_value?, included

Constructor Details

#initialize(args) ⇒ ConfigBuilder

Returns a new instance of ConfigBuilder.



49
50
51
52
53
54
55
56
57
# File 'lib/observed/config_builder.rb', line 49

def initialize(args)
  @group_mutex = ::Mutex.new
  @context = args[:context]
  @observer_plugins = args[:observer_plugins] if args[:observer_plugins]
  @reporter_plugins = args[:reporter_plugins] if args[:reporter_plugins]
  @translator_plugins = args[:translator_plugins] if args[:translator_plugins]
  @system = args[:system] || fail("The key :system must be in #{args}")
  configure args
end

Instance Method Details

#buildObject



83
84
85
86
87
88
# File 'lib/observed/config_builder.rb', line 83

def build
  Observed::Config.new(
      observers: observers,
      reporters: reporters
  )
end

#emit(tag) ⇒ Object



200
201
202
203
204
# File 'lib/observed/config_builder.rb', line 200

def emit(tag)
  e = @context.event_bus.emit(tag)
  e.name = "emit to #{tag}"
  e
end

#group(name, observations = nil) ⇒ Object

Updates or get the observations belongs to the group named ‘name`



211
212
213
214
215
216
217
# File 'lib/observed/config_builder.rb', line 211

def group(name, observations=nil)
  @group_mutex.synchronize do
    @observations ||= {}
    @observations[name] = observations if observations
    @observations[name] || []
  end
end

#observe(tag = nil, args = {}, &block) ⇒ Object

reporters later use or which reader plugin to use (in combination with the default observer plugin) (2) initialization parameters to instantiate the observer/reader plugin

Parameters:

  • tag (String) (defaults to: nil)

    The tag which is assigned to data which is generated from this observer, and is sent to

  • args (Hash) (defaults to: {})

    The configuration for each observer which may or may not contain (1) which observer plugin to



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
173
174
175
176
177
178
179
180
181
# File 'lib/observed/config_builder.rb', line 148

def observe(tag=nil, args={}, &block)
  if tag.is_a? ::Hash
    args = tag
    tag = nil
  end
  observer = if args[:via] || args[:using]
               via = args[:via] || args[:using] ||
                   fail(RuntimeError, %Q|Missing observer plugin name for the tag "#{tag}" in "#{args}"|)
               with = args[:with] || args[:which] || {}
               plugin = observer_plugins[via] ||
                   fail(RuntimeError, %Q|The observer plugin named "#{via}" is not found in "#{observer_plugins}"|)
               observer = plugin.new(({logger: logger}).merge(with).merge(tag: tag, system: system))
               ObserverCompatibilityAdapter.new(
                 system: system,
                 observer: observer,
                 tag: tag
               )
             elsif block_given?
               Observed::ProcObserver.new &block
             else
               fail "No args valid args (in args=#{args}) or a block given"
             end
  observe_that = convert_to_task(observer)
  result = if tag
    a = observe_that.then(emit(tag))
    group tag, (group(tag) + [a])
    a
  else
    observe_that
  end
  observers << result
  result.name = "observe"
  result
end

#observer_pluginsObject



63
64
65
# File 'lib/observed/config_builder.rb', line 63

def observer_plugins
  @observer_plugins || select_named_plugins_of(Observed::Observer)
end

#observersObject



227
228
229
# File 'lib/observed/config_builder.rb', line 227

def observers
  @observers ||= []
end

#receive(pattern) ⇒ Object



206
207
208
# File 'lib/observed/config_builder.rb', line 206

def receive(pattern)
  @context.event_bus.receive(pattern)
end

#report(tag_pattern = nil, args = {}, &block) ⇒ Object

use or which writer plugin to use (in combination with the default reporter plugin) (2) initialization parameters to instantiate the reporter/writer plugin

Parameters:

  • tag_pattern (Regexp) (defaults to: nil)

    The pattern to match tags added to data from observers

  • args (Hash) (defaults to: {})

    The configuration for each reporter which may or may not contain (1) which reporter plugin to



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
# File 'lib/observed/config_builder.rb', line 94

def report(tag_pattern=nil, args={}, &block)
  if tag_pattern.is_a? ::Hash
    args = tag_pattern
    tag_pattern = nil
  end
  reporter = if args[:via] || args[:using]
               via = args[:via] || args[:using]
               with = args[:with] || args[:which] || {}
               with = ({logger: @logger}).merge(with).merge({tag_pattern: tag_pattern, system: system})
               plugin = reporter_plugins[via] ||
                   fail(RuntimeError, %Q|The reporter plugin named "#{via}" is not found in "#{reporter_plugins}"|)
               plugin.new(with)
             elsif block_given?
               Observed::ProcReporter.new tag_pattern, &block
             else
               fail "Invalid combination of arguments: #{tag_pattern} #{args}"
             end

  reporters << reporter
  report_it = convert_to_task(reporter)
  if tag_pattern
    receive(tag_pattern).then(report_it)
  end
  report_it
end

#reporter_pluginsObject



67
68
69
# File 'lib/observed/config_builder.rb', line 67

def reporter_plugins
  @reporter_plugins || select_named_plugins_of(Observed::Reporter)
end

#reportersObject



223
224
225
# File 'lib/observed/config_builder.rb', line 223

def reporters
  @reporters ||= []
end

#run_group(name) ⇒ Object



219
220
221
# File 'lib/observed/config_builder.rb', line 219

def run_group(name)
  @context.task_factory.parallel(group(name))
end

#select_named_plugins_of(klass) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/observed/config_builder.rb', line 75

def select_named_plugins_of(klass)
  plugins = {}
  klass.select_named_plugins.each do |plugin|
    plugins[plugin.plugin_name] = plugin
  end
  plugins
end

#systemObject



59
60
61
# File 'lib/observed/config_builder.rb', line 59

def system
  @system
end

#translate(args = {}, &block) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/observed/config_builder.rb', line 183

def translate(args={}, &block)
  translator = if args[:via] || args[:using]
                 #tag_pattern || fail("Tag pattern missing: #{tag_pattern} where args: #{args}")
                 via = args[:via] || args[:using]
                 with = args[:with] || args[:which] || {}
                 with = ({logger: logger}).merge(with).merge({system: system})
                 plugin = translator_plugins[via] ||
                     fail(RuntimeError, %Q|The reporter plugin named "#{via}" is not found in "#{translator_plugins}"|)
                 plugin.new(with)
               else
                 Observed::ProcTranslator.new &block
             end
  task = convert_to_task(translator)
  task.name = "translate"
  task
end

#translator_pluginsObject



71
72
73
# File 'lib/observed/config_builder.rb', line 71

def translator_plugins
  @translator_plugins || select_named_plugins_of(Observed::Translator)
end