Class: RedCross::Trackers::MonitorTracker

Inherits:
Base
  • Object
show all
Defined in:
lib/red_cross/trackers/monitor_tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, logger

Constructor Details

#initialize(database = 'test', host = 'localhost', port = 8086, write_timeout = 0.05, read_timeout = 0.05, logger = false, async = true) ⇒ MonitorTracker

Returns a new instance of MonitorTracker.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/red_cross/trackers/monitor_tracker.rb', line 6

def initialize(database = 'test', host = 'localhost', port = 8086, write_timeout = 0.05, read_timeout = 0.05,
               logger = false, async = true)
  InfluxDB::Logging.logger = logger
  async_options = async
  if async_options == true
    async_options = {
        num_worker_threads: 1
    }
  end

  @client = InfluxDB::Client.new database,
                                 host:  host,
                                 port:  port,
                                 async: async_options,
                                 retry: false,
                                 write_timeout:  write_timeout.to_f,
                                 read_timeout: read_timeout.to_f
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/red_cross/trackers/monitor_tracker.rb', line 4

def client
  @client
end

Instance Method Details

#compact(hash) ⇒ Object



67
68
69
# File 'lib/red_cross/trackers/monitor_tracker.rb', line 67

def compact(hash)
  hash.respond_to?(:compact) ? hash.compact : hash.delete_if {|k,v| v == nil }
end

#flushObject



33
34
35
# File 'lib/red_cross/trackers/monitor_tracker.rb', line 33

def flush
  {}
end

#group(attrs, additional_args = {}) ⇒ Object



37
38
39
# File 'lib/red_cross/trackers/monitor_tracker.rb', line 37

def group(attrs, additional_args = {})
  {}
end

#identify(attrs, additional_args = {}) ⇒ Object



29
30
31
# File 'lib/red_cross/trackers/monitor_tracker.rb', line 29

def identify(attrs, additional_args = {})
  {}
end

#monitor_request(attrs) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/red_cross/trackers/monitor_tracker.rb', line 41

def monitor_request(attrs)
  return if client.nil?
  event = attrs[:event].to_s
  properties = attrs[:properties] || {}
  values = { count: 1 }.merge((properties.delete(:fields) || {}))
  begin
    client.write_point(event, { values: compact(values), tags: compact(properties) })
    client.writer.worker.stop!
  rescue => e
    error_data = {
        log_message: 'Failed to send monitor data to InfluxDB',
        event_arguments: {
            event: event,
            tags: properties,
            fields: values
        },
        exception: {
            class: e.class.to_s,
            message: e.message,
            backtrace: e.backtrace
        }
    }
    log :error, error_data
  end
end

#track(attrs, additional_args = {}) ⇒ Object



25
26
27
# File 'lib/red_cross/trackers/monitor_tracker.rb', line 25

def track(attrs, additional_args = {})
  monitor_request(attrs.merge(additional_args))
end