module QuizApiClient module HttpRequest class Metrics extend Forwardable attr_reader :config, :method, :url, :code def_delegators :config, :metrics_handler, :metrics_namespace def initialize(config, method, url, code) @config = config @method = method @url = url @code = code end def increment return unless configured? case metrics_handler when :inststatsd InstStatsd::Statsd.increment count_metric_name, tags: tags end end def duration(start_time, end_time) return unless configured? duration_ms = ((end_time - start_time) * 1_000).round case metrics_handler when :inststatsd InstStatsd::Statsd.timing duration_metric_name, duration_ms, tags: tags end end private def configured? metrics_handler_present? && metrics_namespace_present? end def count_metric_name "#{metrics_namespace}.quiz_api_client.request.count" end def duration_metric_name "#{metrics_namespace}.quiz_api_client.request.duration_ms" end def metrics_handler_present? !metrics_handler.nil? end def metrics_namespace_present? !metrics_namespace.nil? end def tags { method: method, status: code, url: url } end end end end