Class: Hallmonitor::Outputters::Influxdb

Inherits:
Hallmonitor::Outputter show all
Defined in:
lib/hallmonitor/outputters/influxdb.rb

Overview

An outputter for InfluxDB

Defined Under Namespace

Classes: EventData

Constant Summary collapse

PRECISION_MAP =

Maps Influxdb client precisions to multipliers for Time#to_r to get the value to send to influxdb for the timestamp

{
  'ns' => 10**9,
  'u' => 10**6,
  'ms' => 10**3,
  's' => 1,
  'm' => 1 / 60,
  'h' => 1 / 3600
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Hallmonitor::Outputter

#name

Instance Method Summary collapse

Constructor Details

#initialize(influxdb_client, tags: {}, transformer: nil) ⇒ Influxdb

Builds a new Influxdb outputter

Parameters:

  • influxdb_client (InfluxDB::Client)

    client instance to use

  • tags (Hash) (defaults to: {})

    Set of default tags applied to all events output to InfluxDB, will be overridden by tags set by events if they conflict

  • transformer (#transform(Event, EventData)) (defaults to: nil)

    An object that responds to #transform(Event, EventData). If supplied it will be passed the EventData struct that has been built so far and it should return an EventData struct that will be written out to InfluxDB. This allows a hook to modify data before it is written out to InfluxDB

Raises:

  • if influxdb_client does not respond to :write_point (InfluxDB::Client contract)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hallmonitor/outputters/influxdb.rb', line 40

def initialize(influxdb_client, tags: {}, transformer: nil)
  unless influxdb_client.respond_to?(:write_point)
    raise 'Supplied InfluxDB Client was not as expected'
  end

  if transformer && !transformer.respond_to?(:transform)
    raise 'Supplied transformer does not respond to :transform'
  end

  super('influxdb')
  @tags = {}.merge(tags)
  @client = influxdb_client || raise('Must supply an InfluxDb client')
  @transformer = transformer
  @precision_mult = PRECISION_MAP[@client.config.time_precision || 's']
end

Instance Attribute Details

#transformer#transform(Event, EventData)

Returns Object used to transform data before it is sent to InfluxDB.

Returns:

  • (#transform(Event, EventData))

    Object used to transform data before it is sent to InfluxDB



26
27
28
# File 'lib/hallmonitor/outputters/influxdb.rb', line 26

def transformer
  @transformer
end

Instance Method Details

#process(event) ⇒ Object

Sends events to InfluxDB instance

Parameters:



58
59
60
61
# File 'lib/hallmonitor/outputters/influxdb.rb', line 58

def process(event)
  event_data = build_event_data(event)
  transform_and_write(event, event_data)
end