From 9e37ff5c8a34db428026fc9e4c56637318b1b7b3 Mon Sep 17 00:00:00 2001 From: Thibaut Courouble Date: Sun, 18 Nov 2018 11:29:50 -0500 Subject: [PATCH] Add instrumentation for slow response processing --- lib/docs/core/requester.rb | 10 +++++++--- lib/docs/subscribers/requester_subscriber.rb | 13 +++++++++++++ lib/tasks/docs.thor | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 lib/docs/subscribers/requester_subscriber.rb diff --git a/lib/docs/core/requester.rb b/lib/docs/core/requester.rb index a4657b3a..2e7a8571 100644 --- a/lib/docs/core/requester.rb +++ b/lib/docs/core/requester.rb @@ -1,5 +1,7 @@ module Docs class Requester < Typhoeus::Hydra + include Instrumentable + attr_reader :request_options def self.run(urls, options = {}, &block) @@ -52,9 +54,11 @@ module Docs end def handle_response(response) - on_response.each do |callback| - result = callback.call(response) - result.each { |url| request(url) } if result.is_a? Array + instrument 'handle_response.requester', url: response.url do + on_response.each do |callback| + result = callback.call(response) + result.each { |url| request(url) } if result.is_a?(Array) + end end end end diff --git a/lib/docs/subscribers/requester_subscriber.rb b/lib/docs/subscribers/requester_subscriber.rb new file mode 100644 index 00000000..4629e9f3 --- /dev/null +++ b/lib/docs/subscribers/requester_subscriber.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Docs + class RequesterSubscriber < Subscriber + self.namespace = 'requester' + + def handle_response(event) + if event.duration > 10_000 + log "WARN: #{format_url event.payload[:url]} was slow to process (#{(event.duration / 1000).round}s)" + end + end + end +end diff --git a/lib/tasks/docs.thor b/lib/tasks/docs.thor index 7839821e..59fdf9d4 100644 --- a/lib/tasks/docs.thor +++ b/lib/tasks/docs.thor @@ -62,7 +62,7 @@ class DocsCLI < Thor Docs.rescue_errors = true Docs.install_report :store if options[:verbose] Docs.install_report :scraper if options[:debug] - Docs.install_report :progress_bar, :doc, :image if $stdout.tty? + Docs.install_report :progress_bar, :doc, :image, :requester if $stdout.tty? require 'unix_utils' if options[:package]