Module: Mouth

Defined in:
lib/mouth.rb,
lib/mouth/graph.rb,
lib/mouth/record.rb,
lib/mouth/runner.rb,
lib/mouth/source.rb,
lib/mouth/sucker.rb,
lib/mouth/version.rb,
lib/mouth/recorder.rb,
lib/mouth/dashboard.rb,
lib/mouth/endoscope.rb,
lib/mouth/instrument.rb,
lib/mouth/sequence_query.rb

Defined Under Namespace

Modules: Recorder Classes: Dashboard, Endoscope, Graph, Record, Runner, SequenceQuery, Source, Sucker, SuckerConnection

Constant Summary collapse

VERSION =
"0.8.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.daemon_hostObject

Returns the value of attribute daemon_host.



7
8
9
# File 'lib/mouth/instrument.rb', line 7

def daemon_host
  @daemon_host
end

.daemon_portObject

Returns the value of attribute daemon_port.



7
8
9
# File 'lib/mouth/instrument.rb', line 7

def daemon_port
  @daemon_port
end

.disabledObject

Returns the value of attribute disabled.



7
8
9
# File 'lib/mouth/instrument.rb', line 7

def disabled
  @disabled
end

.loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/mouth.rb', line 6

def logger
  @logger
end

.mongoObject

Returns a mongo connection (NOT an em-mongo connection)



9
10
11
# File 'lib/mouth.rb', line 9

def mongo
  @mongo
end

.mongo_db_nameObject

Info to connect to mongo



12
13
14
# File 'lib/mouth.rb', line 12

def mongo_db_name
  @mongo_db_name
end

.mongo_hostportsObject

Returns the value of attribute mongo_hostports.



13
14
15
# File 'lib/mouth.rb', line 13

def mongo_hostports
  @mongo_hostports
end

Class Method Details

.collection(collection_name) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/mouth.rb', line 39

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

.collection_for(namespace) ⇒ Object



52
53
54
# File 'lib/mouth.rb', line 52

def collection_for(namespace)
  collection(mongo_collection_name(namespace))
end

.current_timestampObject



85
86
87
# File 'lib/mouth.rb', line 85

def current_timestamp
  timestamp_for(Time.now)
end

.daemon_hostport=(hostport) ⇒ Object

Mouth.server = ‘localhost:1234’



10
11
12
13
# File 'lib/mouth/instrument.rb', line 10

def daemon_hostport=(hostport)
  self.daemon_host, port = hostport.split(':')
  self.daemon_port = port.to_i
end

.gauge(key, value) ⇒ Object



36
37
38
# File 'lib/mouth/instrument.rb', line 36

def gauge(key, value)
  write(key, value, :g)
end

.increment(key, delta = 1, sample_rate = nil) ⇒ Object



32
33
34
# File 'lib/mouth/instrument.rb', line 32

def increment(key, delta = 1, sample_rate = nil)
  write(key, delta, :c, sample_rate)
end

.measure(key, milli = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/mouth/instrument.rb', line 23

def measure(key, milli = nil)
  result = nil
  ms = milli || (Benchmark.realtime { result = yield } * 1000).to_i

  write(key, ms, :ms)

  result
end

.mongo_collection_name(namespace) ⇒ Object



48
49
50
# File 'lib/mouth.rb', line 48

def mongo_collection_name(namespace)
  "mouth_#{namespace}"
end

.parse_key(key) ⇒ Object

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”]



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mouth.rb', line 70

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

.sanitize_metric(key) ⇒ Object



60
61
62
# File 'lib/mouth.rb', line 60

def sanitize_metric(key)
  key.gsub(/\s+/, '_').gsub(/[^a-zA-Z0-9\-_\/]/, '')
end

.sanitize_namespace(key) ⇒ Object



56
57
58
# File 'lib/mouth.rb', line 56

def sanitize_namespace(key)
  key.gsub(/\s+/, '_').gsub(/\//, '-').gsub(/[^a-zA-Z_\-0-9]/, '')
end

.timestamp_for(time) ⇒ Object



89
90
91
# File 'lib/mouth.rb', line 89

def timestamp_for(time)
  time.to_i / 60
end