Class: OML4R::MPBase

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

Overview

Measurement Point Class Ruby applications using this module should sub-class this MPBase class to define their own Measurement Point (see the example at the end of this file)

Constant Summary collapse

@@appName =

Some Class variables

nil
@@defs =
{}
@@channels =
{}
@@channelNames =
{}
@@frozen =
false
@@useOML =
false
@@start_time =
nil
@@newDefs =
Set[]

Class Method Summary collapse

Class Method Details

.__def__Object

Returns the definition of this MP



82
83
84
85
86
87
88
89
90
# File 'lib/oml4r.rb', line 82

def self.__def__()
  unless (defs = @@defs[self])
    defs = @@defs[self] = {}
    defs[:p_def] = []
    defs[:seq_no] = 0
    defs[:meta_no] = 0
  end
  defs
end

.__freeze__(appName, start_time) ⇒ Object

Freeze the definition of further MPs



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/oml4r.rb', line 262

def self.__freeze__(appName, start_time)
  return if @@frozen
  @@frozen = true
  @@appName = appName
  # create type array for easier processing in inject
  @@defs.each do |name, descr|
    descr[:types] = descr[:p_def].map {|h| h[:type]}
  end

  # replace channel names with channel object
  self.each_mp do |klass, defs|
    names = @@channelNames[klass] || []
    OML4R.logger.debug "'#{names.inspect}', '#{klass}'"
    chans = names.collect do |cname, domain|
      # return it in an array as we need to add the channel specific index
      [Channel[cname.to_sym, domain.to_sym]]
    end
    OML4R.logger.debug "Using channels '#{chans.inspect}"
    @@channels[klass] = chans.empty? ? [[Channel[]]] : chans
  end
  @@start_time = start_time
end

.__print_meta__(name_prefix = nil) ⇒ Object

Build the table schema for this MP and send it to the OML collection server

  • name_prefix = the name for this MP to use as a prefix for its table



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/oml4r.rb', line 296

def self.__print_meta__(name_prefix = nil)
  return unless @@frozen
  defs = __def__()

  # Do some sanity checks...
  unless (mp_name_def = defs[:name])
    raise MissingArgumentException.new "Missing 'name' declaration for '#{self}'"
  end

  # Build the schema
  mp_name = mp_name_def[:name]
  @@channels[self].each do |ca|
    OML4R.logger.debug "Setting up channel '#{ca.inspect}"
    schema_info = ca[0].build_schema(mp_name, mp_name_def[:opts][:add_prefix], defs[:p_def])
    ca << schema_info[0]
  end
end

.__unfreeze__Object



285
286
287
288
289
290
291
292
# File 'lib/oml4r.rb', line 285

def self.__unfreeze__()
  self.each_mp do |klass, defs|
    defs[:seq_no] = 0
  end
  @@channels = {}
  @@start_time = nil
  @@frozen = false
end

.__useOML__Object

Set the useOML flag. If set to false, make ‘inject’ a NOOP



77
78
79
# File 'lib/oml4r.rb', line 77

def self.__useOML__()
  @@useOML = true
end

.channel(channel, domain = :default) ⇒ Object

Set the channel these measurements should be sent out on. Multiple declarations are allowed, and ‘:default’ identifies the channel defined by the command line arguments or environment variables.



116
117
118
# File 'lib/oml4r.rb', line 116

def self.channel(channel, domain = :default)
  (@@channelNames[self] ||= []) << [channel, domain]
end

.each_mp(&block) ⇒ Object

Execute a block for each defined MP



66
67
68
# File 'lib/oml4r.rb', line 66

def self.each_mp(&block)
  @@defs.each(&block)
end

.has_mp?(name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/oml4r.rb', line 70

def self.has_mp?(name)
  exist = false
  @@defs.each { |m| exist = true if m[0].__def__[:name][:name]==name.to_sym }
  exist
end

.inject(*args) ⇒ Object

Inject a measurement from this Measurement Point to the OML Server However, if useOML flag is false, then only prints the measurement on stdout

  • args = a list of arguments (comma separated) which correspond to the

    different values of the metrics for the measurement to inject
    


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/oml4r.rb', line 190

def self.inject(*args)
  return unless @@useOML

  defs = __def__()

  # Do we need to send the schema?
  if @@newDefs.include? self
    # Identify MP details
    mp_name_def = defs[:name]
    mp_name = mp_name_def[:name]
    pdefs = defs[:p_def]
    defs[:types] = pdefs.map {|h| h[:type]}
    # Setup channel and schema
    channel = Channel[]
    schema_info = channel.build_schema(mp_name, mp_name_def[:opts][:add_prefix], pdefs)
    @@channels[self] = [[channel, schema_info[0]]]
    # Inject it!
    ExperimentMetadata.("schema", schema_info[1])
    @@newDefs.delete self
  end

  # Check that the list of values passed as argument matches the
  # definition of this Measurement Point
  pdef = defs[:p_def]
  types = defs[:types]
  if args.size != pdef.size
    raise ArgumentMismatchException.new "OML4R: Size mismatch between the measurement (#{args.size}) and the MP definition (#{pdef.size})!"
  end

  # Now prepare the measurement...
  t = Time.now - @@start_time
  a = []
  a << (defs[:seq_no] += 1)
  args.each_with_index do |arg, i|
    case types[i]
    when :double
      arg = "NaN" if arg.nil?
    when :string
      # Escape tabs and newlines
      arg = arg.to_s.gsub("\\", "\\\\").gsub("\r", "\\r").gsub("\n", "\\n").gsub("\t", "\\t")
    when :bool
      # Convert boolean value to appropriate literal
      arg = arg ? "True" : "False"
    when :blob
      arg = [arg].pack("m")
    end
    a << arg
  end
  # ...and inject it!
  msg = a.join("\t")
  @@channels[self].each do |ca|
    channel = ca[0]
    index = ca[1]
    channel.send "#{t}\t#{index}\t#{msg}"
  end

  args
end

.inject_metadata(key, value, fname = nil) ⇒ Object

Inject a metadata measurement from this Measurement Point ot the OML Server.

  • key = a string used to identify the key

  • value = the string value

  • fname = when not nil a string used to qualify the subject name



142
143
144
145
146
147
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
182
183
184
# File 'lib/oml4r.rb', line 142

def self.(key, value, fname = nil)

  return unless @@useOML

  # retrieve infos
  defs = @@defs[self]
  mp_name_def = defs[:name]
  mp_name = mp_name_def[:name]
  pdefs = defs[:p_def]
  defs[:types] = pdefs.map {|h| h[:type]}

  # construct the subject reference
  subject = "."
  if self != OML4R::ExperimentMetadata
    subject +=  "#{@@appName}_#{mp_name}"
    unless fname.nil?
      subject += ".#{fname}"
    end
  end

  # prepare the message header
  a = []
  a << Time.now - @@start_time
  a << "0"
  a << (defs[:meta_no] += 1)
  a << subject
  a << key
  a << value
  msg = a.join("\t")

  # Setup channels for the ExperimentMetadata MP
  chans = @@channels[self] || []
  if chans.empty?
    @@channels[self] = [[Channel[], 0]]
  end

  # now inject the schema
  @@channels[self].each do |ca|
    channel = ca[0]
    index = ca[1]
    channel.send msg
  end
end

.name(name, opts = {}) ⇒ Object

Set a name for this MP

param opts Options opts add_prefix Add app name as prefix to table. Default: true



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/oml4r.rb', line 97

def self.name(name, opts = {})

  # ok, lets add it then
  if opts[:add_prefix].nil?
    opts[:add_prefix] = true
  end
  __def__()[:name] = {:name => name, :opts => opts}

  # if we're frozen remember to inject schema before measurements
  if @@frozen
    @@newDefs.add self
  end

end

.param(name, opts = {}) ⇒ Object

Set a metric for this MP

  • name = name of the metric to set

  • opts = a Hash with the options for this metric

    Only supported option is :type = { :string | :int32 | :uint32 | :int64 | :uint64 | :double | :bool | :guid |
    [ DEPRECATED :long | :integer ] }
    


125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/oml4r.rb', line 125

def self.param(name, opts = {})
  o = opts.dup
  o[:name] = name
  o[:type] ||= :string
  if :long == o[:type] or :integer == o[:type]
	# XXX: :name in :name... See #1527 bullet point 3
    OML4R.logger.warn "Type #{o[:type]} for #{__def__()[:name][:name]}.#{o[:name]} is deprecated, use :int32 instead"
    o[:type] = :int32
  end
  __def__()[:p_def] << o
  nil
end

.start_timeObject

Inject measurement metadata from this Measurement Point to the OML Server.

def self.inject_metadata(key, value, fname)

MPBase::(@name, key, value, fname)

end



256
257
258
# File 'lib/oml4r.rb', line 256

def self.start_time()
  @@start_time
end