class OML4R::MPBase
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)
Public Class Methods
Returns the definition of this MP
# 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 the definition of further MPs
# 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
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
# 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
# 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
Set the useOML flag. If set to false, make 'inject' a NOOP
# File lib/oml4r.rb, line 77 def self.__useOML__() @@useOML = true end
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.
# File lib/oml4r.rb, line 116 def self.channel(channel, domain = :default) (@@channelNames[self] ||= []) << [channel, domain] end
Execute a block for each defined MP
# File lib/oml4r.rb, line 66 def self.each_mp(&block) @@defs.each(&block) end
# 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 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
# 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.inject_metadata("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 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
# File lib/oml4r.rb, line 142 def self.inject_metadata(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
Set a name for this MP
param opts Options opts add_prefix Add app name as prefix to table. Default: true
# 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
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 ] }
# 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
Inject measurement metadata from this Measurement Point to the OML Server.
def self.inject_metadata(key, value, fname)
MPBase::__inject_metadata__(@name, key, value, fname)
end
# File lib/oml4r.rb, line 256 def self.start_time() @@start_time end