class Hallmonitor::Outputters::Influxdb
An outputter for InfluxDB
Constants
Attributes
Public Class Methods
new(influxdb_client, tags: {}, transformer: nil)
click to toggle source
Builds a new Influxdb
outputter @param influxdb_client [InfluxDB::Client] client instance to use @param tags [Hash] Set of default tags applied to all events output to
InfluxDB, will be overridden by tags set by events if they conflict
@param transformer [#transform(Event
, EventData
)] 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
@raise if influxdb_client does not respond to :write_point
(InfluxDB::Client contract)
Calls superclass method
Hallmonitor::Outputter::new
# 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
Public Instance Methods
process(event)
click to toggle source
Sends events to InfluxDB instance @param event [Hallmonitor::Event]
# File lib/hallmonitor/outputters/influxdb.rb, line 58 def process(event) event_data = build_event_data(event) transform_and_write(event, event_data) end
Private Instance Methods
build_counter_data(event)
click to toggle source
# File lib/hallmonitor/outputters/influxdb.rb, line 109 def build_counter_data(event) build_data(event, 'count', event.count) end
build_data(event, type, value)
click to toggle source
Builds an EventData
struct for the event
# File lib/hallmonitor/outputters/influxdb.rb, line 85 def build_data(event, type, value) data = EventData.new data.name = event.name data.tags = @tags.merge(event.tags.merge(type: type)) data.fields = value.is_a?(Hash) ? value : { value: value } data.timestamp = calc_timestamp(event) data end
build_event_data(event)
click to toggle source
Builds an {EventData} from the Hallmonitor::Event
# File lib/hallmonitor/outputters/influxdb.rb, line 74 def build_event_data(event) if event.is_a?(Hallmonitor::TimedEvent) build_timer_data(event) elsif event.is_a?(Hallmonitor::GaugeEvent) build_gauge_data(event) else build_counter_data(event) end end
build_gauge_data(event)
click to toggle source
# File lib/hallmonitor/outputters/influxdb.rb, line 105 def build_gauge_data(event) build_data(event, 'gauge', event.value) end
build_timer_data(event)
click to toggle source
# File lib/hallmonitor/outputters/influxdb.rb, line 101 def build_timer_data(event) build_data(event, 'timer', event.duration) end
calc_timestamp(event)
click to toggle source
Calculates a timestamp based on the indicated {Time} object and the influxdb client's precision @param event [Hallmonitor::Event]
# File lib/hallmonitor/outputters/influxdb.rb, line 97 def calc_timestamp(event) (event.time.to_r * @precision_mult).to_i if event.time.respond_to?(:to_r) end
transform_and_write(event, event_data)
click to toggle source
@param event [Event] The original event we're working with @param data [EventData] Struct of data we're building for InfluxDB
# File lib/hallmonitor/outputters/influxdb.rb, line 67 def transform_and_write(event, event_data) event_data = @transformer.transform(event, event_data) if @transformer data = { tags: event_data.tags, values: event_data.fields, timestamp: event_data.timestamp } @client.write_point(event_data.name, data) end