require "influxdb/rails/middleware/subscriber" module InfluxDB module Rails module Middleware # Subscriber acts as base class for different *Subscriber classes, # which are intended as ActiveSupport::Notifications.subscribe # consumers. class SimpleSubscriber < Subscriber attr_reader :series_name def initialize(configuration, series_name) super(configuration) @series_name = series_name end def call(_name, started, finished, _unique_id, payload) return unless enabled? begin # InfluxDB::Rails.client.write_point series_name, # values: values(started, finished, payload), # tags: tags(payload), # timestamp: timestamp(finished.utc) InfluxDB::Rails.client.write_point series_name, values: values(started, finished, payload), tags: tags(payload), timestamp: timestamp(started.utc) rescue StandardError => e log :error, "[InfluxDB::Rails] Unable to write points: #{e.message}" end end private def values(started, finished, _payload) x_request_id = "AL_NONE" al_source = "AL_NONE" al_request_id = "AL_NONE" al_request_guid = "AL_NONE" al_request_client_id = "AL_NONE" x_request_id = _payload[:headers][:HTTP_X_REQUEST_ID] if(_payload[:headers] && _payload[:headers][:HTTP_X_REQUEST_ID]) #al_source = _payload[:headers][:HTTP_AL_SOURCE] if(_payload[:headers] && _payload[:headers][:HTTP_AL_SOURCE]) #al_request_id = _payload[:headers][:HTTP_AL_REQUEST_ID] if(_payload[:headers] && _payload[:headers][:HTTP_AL_REQUEST_ID]) #al_request_guid = _payload[:headers][:HTTP_AL_REQUEST_GUID] if(_payload[:headers] && _payload[:headers][:HTTP_AL_REQUEST_GUID]) #al_request_client_id = _payload[:headers][:HTTP_AL_REQUEST_CLIENT_ID] if(_payload[:headers] && _payload[:headers][:HTTP_AL_REQUEST_CLIENT_ID]) al_request_id = Thread.current["al_request_id"] if(Thread.current["al_request_id"]) al_request_guid = Thread.current["al_request_guid"] if(Thread.current["al_request_guid"]) al_source = Thread.current["al_source"] al_request_client_id = Thread.current["al_request_client_id"] { value: ((finished - started) * 1000).ceil , x_request_id: x_request_id, al_request_id: al_request_id, al_request_guid: al_request_guid, al_request_source_id: al_source, al_request_client_id: al_request_client_id} end def timestamp(finished) InfluxDB.convert_timestamp(finished.utc, configuration.time_precision) end def enabled? super && series_name.present? end end end end end