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 =
"1.0.2"

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



79
80
81
82
83
84
85
# File 'lib/mettric/mettric.rb', line 79

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mettric/mettric.rb', line 91

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
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

  # Be super conservative, always return the result
  begin
    timing_payload = payload.dup
    timing_payload[:service] = "#{payload[:service]}.duration"
    timing_payload[:metric] = ((Time.now - start) * 1000).to_i
    timing_payload[:description] = [payload[:description], "(ms)"].compact.join(' ')
    timing_payload[:tags] = (payload[:tags] || []) + [:timing]
    track(timing_payload)
  rescue
    result
  end

  if exception
    event(service: "#{payload[:service]}.failure", tags: payload[:tags], description: exception.to_s) rescue nil
    raise exception
  else
    event(service: "#{payload[:service]}.success", tags: payload[:tags]) rescue result
  end
  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)


87
88
89
# File 'lib/mettric/mettric.rb', line 87

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