Sha256: a985e6260d375a1bb963a5f09474113291ccc8167d4dc6006f9c32b61ac225be
Contents?: true
Size: 1.92 KB
Versions: 1
Compression:
Stored size: 1.92 KB
Contents
# Since the gem can be used in many ways (sucker daemon, sinatra app, instrument, ad-hoc, don't include anything) require 'mouth/version' module Mouth class << self attr_accessor :logger attr_accessor :mongo_host attr_accessor :mongo_port # Returns a mongo connection (NOT an em-mongo connection) def mongo @mongo ||= begin require 'mongo' # require mongo here, as opposed to the top, because we don't want mongo included in the reactor (use em-mongo for that) Mongo::Connection.new(self.mongo_host || "localhost", self.mongo_port || 27017, :pool_size => 5, :pool_timeout => 20).db("mouth") end end def collection(collection_name) @collections ||= {} @collections[collection_name] ||= begin c = mongo.collection(collection_name) c.ensure_index([["t", 1]], {:background => true, :unique => true}) c end end def mongo_collection_name(namespace) "mouth_#{namespace}" end def sanitize_namespace(key) key.gsub(/\s+/, '_').gsub(/\//, '-').gsub(/[^a-zA-Z_\-0-9]/, '') end def sanitize_metric(key) key.gsub(/\s+/, '_').gsub(/[^a-zA-Z0-9\-_\/]/, '') end # Parses a key into two parts: namespace, and metric. Also sanitizes each field # Returns an array of values: [namespace, metric] # eg, # parse_key("Ticket.process_new_ticket") # => ["Ticket", "process_new_ticket"] # parse_key("Forum List.other! stuff.ok") # => ["Forum_List", "other_stuff-ok"] # parse_key("no_namespace") # => ["default", "no_namespace"] def parse_key(key) parts = key.split(".") namespace = nil metric = nil if parts.length > 1 namespace = parts.shift metric = parts.join("-") else namespace = "default" metric = parts.shift end [sanitize_namespace(namespace), sanitize_metric(metric)] end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mouth-0.8.1 | lib/mouth.rb |