Class: Mettric

Inherits:
Object
  • Object
show all
Defined in:
lib/mettric/mettric.rb,
lib/mettric/version.rb

Defined Under Namespace

Classes: Client, CouldNotStartWorkerThread, Error, MissingAppName, MissingHostName, MissingService, SidekiqMiddleware, Worker

Constant Summary collapse

QUEUE =
Queue.new
LOCK =
Mutex.new
VERSION =
"0.3.1"

Class Method Summary collapse

Class Method Details

.configObject



5
6
7
# File 'lib/mettric/mettric.rb', line 5

def self.config
  @config
end

.config=(config) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mettric/mettric.rb', line 9

def self.config=(config)
  # See if the config is valid
  config_test = Client.new(config)
  config_test.close

  # Set the config
  @config = config

  # Attempt to install sidekiq middleware unless
  # we're told not to
  if config.delete(:sidekiq_middleware) != false
    Mettric::SidekiqMiddleware.install
  end

  @config
end

.ensure_worker_runningObject



66
67
68
69
70
71
72
# File 'lib/mettric/mettric.rb', line 66

def self.ensure_worker_running
  return if worker_running?
  LOCK.synchronize do
    return if worker_running?
    start_worker
  end
end

.event(payload) ⇒ Object

To track events



39
40
41
42
43
# File 'lib/mettric/mettric.rb', line 39

def self.event(payload)
  payload[:tags] ||= []
  payload[:tags] << :event
  track(payload)
end

.meter(payload) ⇒ Object

Tracking meter readings is the default



34
35
36
# File 'lib/mettric/mettric.rb', line 34

def self.meter(payload)
  track(payload)
end

.start_workerObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mettric/mettric.rb', line 78

def self.start_worker
  exception = nil
  worker = Mettric::Worker.new(QUEUE, @config)
  @worker_thread = Thread.new do
    begin
    worker.start
    rescue => e
      exception = e
    end
  end
  sleep 0.5
  return if @worker_thread.alive?
  raise Mettric::CouldNotStartWorkerThread, exception
end

.time(payload) ⇒ Object

To track durations



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mettric/mettric.rb', line 46

def self.time(payload)
  exception = nil
  result = nil
  state = 'success'
  start = Time.now
  begin
    result = yield
  rescue => e
    exception = e
    state = 'failure'
  end
  payload[:metric] = ((Time.now - start) * 1000).to_i
  payload[:description] = [payload[:description], "(ms)"].compact.join(' ')
  payload[:tags] ||= []
  payload[:tags] << :timing
  track(payload)
  raise exception if exception
  result
end

.track(payload) ⇒ Object



26
27
28
29
30
31
# File 'lib/mettric/mettric.rb', line 26

def self.track(payload)
  return false unless @config
  ensure_worker_running
  QUEUE << payload
  QUEUE.size
end

.worker_running?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/mettric/mettric.rb', line 74

def self.worker_running?
  @worker_thread && @worker_thread.alive?
end