lib/instana/util.rb in instana-0.15.0 vs lib/instana/util.rb in instana-1.0.1
- old
+ new
@@ -19,10 +19,28 @@
else
::Instana.logger.debug "No such method (#{method}) to alias on #{klass}"
end
end
+ # Calls on target_class to 'extend' cls
+ #
+ # @param target_cls [Object] the class/module to do the 'extending'
+ # @param cls [Object] the class/module to be 'extended'
+ #
+ def send_extend(target_cls, cls)
+ target_cls.send(:extend, cls) if defined?(target_cls)
+ end
+
+ # Calls on <target_cls> to include <cls> into itself.
+ #
+ # @param target_cls [Object] the class/module to do the 'including'
+ # @param cls [Object] the class/module to be 'included'
+ #
+ def send_include(target_cls, cls)
+ target_cls.send(:include, cls) if defined?(target_cls)
+ end
+
# Take two hashes, and make sure candidate does not have
# any of the same values as `last`. We only report
# when values change.
#
# Note this is not recursive, so only pass in the single
@@ -142,9 +160,68 @@
process[:pid] = Process.pid
# This is usually Process.pid but in the case of docker, the host agent
# will return to us the true host pid in which we use to report data.
process[:report_pid] = nil
process
+ end
+
+ # Get the current time in milliseconds
+ #
+ # @return [Integer] the current time in milliseconds
+ #
+ def ts_now
+ (Time.now.to_f * 1000).floor
+ end
+
+ # Convert a Time value to milliseconds
+ #
+ # @param time [Time]
+ #
+ def time_to_ms(time = Time.now)
+ (time.to_f * 1000).floor
+ end
+
+ # Generate a random 64bit ID
+ #
+ # @return [Integer] a random 64bit integer
+ #
+ def generate_id
+ # Max value is 9223372036854775807 (signed long in Java)
+ rand(-2**63..2**63-1)
+ end
+
+ # Convert an ID to a value appropriate to pass in a header.
+ #
+ # @param id [Integer] the id to be converted
+ #
+ # @return [String]
+ #
+ def id_to_header(id)
+ unless id.is_a?(Integer) || id.is_a?(String)
+ Instana.logger.debug "id_to_header received a #{id.class}: returning empty string"
+ return String.new
+ end
+ [id.to_i].pack('q>').unpack('H*')[0]
+ rescue => e
+ Instana.logger.error "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}"
+ Instana.logger.debug e.backtrace.join("\r\n")
+ end
+
+ # Convert a received header value into a valid ID
+ #
+ # @param header_id [String] the header value to be converted
+ #
+ # @return [Integer]
+ #
+ def header_to_id(header_id)
+ if !header_id.is_a?(String)
+ Instana.logger.debug "header_to_id received a #{header_id.class}: returning 0"
+ return 0
+ end
+ [header_id].pack("H*").unpack("q>")[0]
+ rescue => e
+ Instana.logger.error "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}"
+ Instana.logger.debug e.backtrace.join("\r\n")
end
end
end
end