lib/statsd.rb in dogstatsd-ruby-1.5.0 vs lib/statsd.rb in dogstatsd-ruby-1.6.0

- old
+ new

@@ -67,11 +67,11 @@ attr_accessor :logger end # Return the current version of the library. def self.VERSION - "1.5.0" + "1.6.0" end # @param [String] host your statsd host # @param [Integer] port your statsd port # @option opts [String] :namespace set a namespace to be prepended to every metric name @@ -236,24 +236,19 @@ sc_string = "_sc|#{name}|#{status}" SC_OPT_KEYS.each do |name_key| if opts[name_key[0].to_sym] if name_key[0] == 'tags' - tags = opts[:tags] - tags.each do |tag| - rm_pipes tag - end + tags = opts[:tags].map {|tag| remove_pipes(tag) } tags = "#{tags.join(",")}" unless tags.empty? sc_string << "|##{tags}" elsif name_key[0] == 'message' - message = opts[:message] - rm_pipes message - escape_service_check_message message - sc_string << "|m:#{message}" + message = remove_pipes(opts[:message]) + escaped_message = escape_service_check_message(message) + sc_string << "|m:#{escaped_message}" else - value = opts[name_key[0].to_sym] - rm_pipes value + value = remove_pipes(opts[name_key[0].to_sym]) sc_string << "|#{name_key[1]}#{value}" end end end return sc_string @@ -298,48 +293,43 @@ flush_buffer alias :send_stat :send_to_socket end def format_event(title, text, opts={}) - escape_event_content title - escape_event_content text - event_string_data = "_e{#{title.length},#{text.length}}:#{title}|#{text}" + escaped_title = escape_event_content(title) + escaped_text = escape_event_content(text) + event_string_data = "_e{#{escaped_title.length},#{escaped_text.length}}:#{escaped_title}|#{escaped_text}" # We construct the string to be sent by adding '|key:value' parts to it when needed # All pipes ('|') in the metadata are removed. Title and Text can keep theirs OPTS_KEYS.each do |name_key| if name_key[0] != 'tags' && opts[name_key[0].to_sym] - value = opts[name_key[0].to_sym] - rm_pipes value + value = remove_pipes(opts[name_key[0].to_sym]) event_string_data << "|#{name_key[1]}:#{value}" end end - full_tags = tags + (opts[:tags] || []) # Tags are joined and added as last part to the string to be sent + full_tags = (tags + (opts[:tags] || [])).map {|tag| remove_pipes(tag) } unless full_tags.empty? - full_tags.each do |tag| - rm_pipes tag - end event_string_data << "|##{full_tags.join(',')}" end raise "Event #{title} payload is too big (more that 8KB), event discarded" if event_string_data.length > 8 * 1024 return event_string_data end private def escape_event_content(msg) - msg.gsub! "\n", "\\n" + msg.gsub "\n", "\\n" end - def rm_pipes(msg) - msg.gsub! "|", "" + def remove_pipes(msg) + msg.gsub "|", "" end def escape_service_check_message(msg) - msg.gsub! 'm:', 'm\:' - msg.gsub! "\n", "\\n" + msg.gsub('m:', 'm\:').gsub "\n", "\\n" end def time_since(stat, start, opts) timing(stat, ((Time.now - start) * 1000).round, opts) end