зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-03 17:55:52 +03:00
v0.1.0
Этот коммит содержится в:
79
lib/telegram/bot/updates_controller/instrumentation.rb
Обычный файл
79
lib/telegram/bot/updates_controller/instrumentation.rb
Обычный файл
@@ -0,0 +1,79 @@
|
||||
module Telegram
|
||||
class Bot
|
||||
class UpdatesController
|
||||
# Most methods are taken from ActionController::Instrumentation,
|
||||
# some are slightly modified.
|
||||
module Instrumentation
|
||||
class << self
|
||||
def prepended(base)
|
||||
base.config_accessor :logger
|
||||
base.extend ClassMethods
|
||||
end
|
||||
|
||||
def instrument(action, *args, &block)
|
||||
ActiveSupport::Notifications.instrument(
|
||||
"#{action}.updates_controller.bot.telegram",
|
||||
*args,
|
||||
&block
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def process_action(*args)
|
||||
raw_payload = {
|
||||
controller: self.class.name,
|
||||
action: action_name,
|
||||
update: update,
|
||||
}
|
||||
Instrumentation.instrument(:start_processing, raw_payload.dup)
|
||||
Instrumentation.instrument(:process_action, raw_payload) do |payload|
|
||||
begin
|
||||
super
|
||||
ensure
|
||||
append_info_to_payload(payload)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def reply_with(type, *)
|
||||
Instrumentation.instrument(:reply_with, type: type) { super }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# A hook invoked every time a before callback is halted.
|
||||
def halted_callback_hook(filter)
|
||||
Instrumentation.instrument(:halted_callback, filter: filter)
|
||||
end
|
||||
|
||||
# A hook which allows you to clean up any time taken into account in
|
||||
# views wrongly, like database querying time.
|
||||
#
|
||||
# def cleanup_view_runtime
|
||||
# super - time_taken_in_something_expensive
|
||||
# end
|
||||
#
|
||||
# :api: plugin
|
||||
def cleanup_view_runtime #:nodoc:
|
||||
yield
|
||||
end
|
||||
|
||||
# Every time after an action is processed, this method is invoked
|
||||
# with the payload, so you can add more information.
|
||||
# :api: plugin
|
||||
def append_info_to_payload(_payload) #:nodoc:
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# A hook which allows other frameworks to log what happened during
|
||||
# controller process action. This method should return an array
|
||||
# with the messages to be added.
|
||||
# :api: plugin
|
||||
def log_process_action(_payload) #:nodoc:
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
38
lib/telegram/bot/updates_controller/log_subscriber.rb
Обычный файл
38
lib/telegram/bot/updates_controller/log_subscriber.rb
Обычный файл
@@ -0,0 +1,38 @@
|
||||
require 'active_support/log_subscriber'
|
||||
|
||||
module Telegram
|
||||
class Bot
|
||||
class UpdatesController
|
||||
class LogSubscriber < ActiveSupport::LogSubscriber
|
||||
def start_processing(event)
|
||||
info do
|
||||
payload = event.payload
|
||||
"Processing by #{payload[:controller]}##{payload[:action]}\n" \
|
||||
" Update: #{payload[:update].to_json}"
|
||||
end
|
||||
end
|
||||
|
||||
def process_action(event)
|
||||
info do
|
||||
payload = event.payload
|
||||
additions = UpdatesController.log_process_action(payload)
|
||||
message = "Completed in #{event.duration.round}ms"
|
||||
message << " (#{additions.join(' | ')})" unless additions.blank?
|
||||
message
|
||||
end
|
||||
end
|
||||
|
||||
def reply_with(event)
|
||||
info { "Replied with #{event.payload[:type]}" }
|
||||
end
|
||||
|
||||
def halted_callback(event)
|
||||
info { "Filter chain halted at #{event.payload[:filter].inspect}" }
|
||||
end
|
||||
|
||||
delegate :logger, to: UpdatesController
|
||||
attach_to 'updates_controller.bot.telegram'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Ссылка в новой задаче
Block a user