Sha256: 796f8f14250e266cb1a2ed4f949336b03d2b0d555f9345f1312fd9733c1e338a

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

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
        def initialize(configuration:, hook_name:, start:, finish:, payload:)
          @configuration = configuration
          @hook_name = hook_name
          @start = start
          @finish = finish
          @payload = payload
        end

        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

        attr_reader :configuration, :hook_name, :start, :finish, :payload

        def metric
          InfluxDB::Rails::Metric.new(
            values:        values,
            tags:          tags,
            configuration: configuration,
            timestamp:     finish
          )
        end

        def tags
          raise NotImplementedError, "must be implemented in subclass"
        end

        def values
          raise NotImplementedError, "must be implemented in subclass"
        end

        def duration
          ((finish - start) * 1000).ceil
        end

        def disabled?
          configuration.ignore_current_environment? ||
            configuration.ignored_hooks.include?(hook_name)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
influxdb-rails-1.0.3 lib/influxdb/rails/middleware/subscriber.rb
influxdb-rails-1.0.2 lib/influxdb/rails/middleware/subscriber.rb
influxdb-rails-1.0.1 lib/influxdb/rails/middleware/subscriber.rb
influxdb-rails-1.0.1.beta3 lib/influxdb/rails/middleware/subscriber.rb
influxdb-rails-1.0.1.beta2 lib/influxdb/rails/middleware/subscriber.rb
influxdb-rails-1.0.1.beta1 lib/influxdb/rails/middleware/subscriber.rb