lib/influxdb/rails/middleware/subscriber.rb in influxdb-rails-1.0.0 vs lib/influxdb/rails/middleware/subscriber.rb in influxdb-rails-1.0.1.beta1

- old
+ new

@@ -1,54 +1,67 @@ +require "influxdb/rails/metric" + module InfluxDB module Rails module Middleware # Subscriber acts as base class for different *Subscriber classes, # which are intended as ActiveSupport::Notifications.subscribe # consumers. class Subscriber - attr_reader :configuration - attr_reader :hook_name - - def initialize(configuration, hook_name) + def initialize(configuration:, hook_name:, start:, finish:, payload:) @configuration = configuration @hook_name = hook_name + @start = start + @finish = finish + @payload = payload end - def call(*) - raise NotImplementedError, "must be implemented in subclass" + def self.call(name, start, finish, _id, payload) + new( + configuration: InfluxDB::Rails.configuration, + start: start, + finish: finish, + payload: payload, + hook_name: name + ).write end + def write + return if disabled? + + metric.write + rescue StandardError => e + ::Rails.logger.error("[InfluxDB::Rails] Unable to write points: #{e.message}") + end + private - def timestamp(time) - InfluxDB.convert_timestamp(time.utc, client.time_precision) + attr_reader :configuration, :hook_name, :start, :finish, :payload + + def metric + InfluxDB::Rails::Metric.new( + values: values, + tags: tags, + configuration: configuration, + timestamp: finish + ) end - def client - @client = configuration.client + def tags + raise NotImplementedError, "must be implemented in subclass" end - def tags(tags) - result = tags.merge(InfluxDB::Rails.current.tags) - result = configuration.tags_middleware.call(result) - result.reject! do |_, value| - value.nil? || value == "" - end - result + def values + raise NotImplementedError, "must be implemented in subclass" end - def enabled? - configuration.instrumentation_enabled? && - !configuration.ignore_current_environment? && - !configuration.ignored_hooks.include?(hook_name) + def duration + ((finish - start) * 1000).ceil end - def location - current = InfluxDB::Rails.current - [ - current.controller, - current.action, - ].reject(&:blank?).join("#") + def disabled? + configuration.ignore_current_environment? || + configuration.ignored_hooks.include?(hook_name) end end end end end