Class: RequireProfiler::Plugins::HTTPPlugin

Inherits:
Base
  • Object
show all
Defined in:
lib/require_profiler/plugins/http_plugin.rb

Overview

Track HTTP calls using Sniffer and add the to the require profile

Instance Attribute Summary

Attributes inherited from Base

#reporter

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from RequireProfiler::Plugins::Base

Instance Method Details

#activate!Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/require_profiler/plugins/http_plugin.rb', line 7

def activate!
  begin
    require "sniffer"
  rescue LoadError
    return
  end

  Sniffer.config.logger = Logger.new(IO::NULL)

  Sniffer.config.middleware do |chain|
    chain.add HTTPPlugin, reporter
  end

  Sniffer::DataItem::Request.include(Module.new do
    def require_path
      @url ||= "#{method.to_s.upcase}:#{(port == 443) ? "https" : "http"}://#{host}#{query}"
    end
  end)

  Sniffer.enable!
end

#request(data_item) ⇒ Object

Sniffer hook interface



30
31
32
33
# File 'lib/require_profiler/plugins/http_plugin.rb', line 30

def request(data_item)
  reporter.handle_event(Reporter::Event.new(type: :start, kind: :http, path: data_item.request.require_path))
  yield
end

#response(data_item) ⇒ Object



35
36
37
38
39
# File 'lib/require_profiler/plugins/http_plugin.rb', line 35

def response(data_item)
  yield
  time = data_item.response.timing
  reporter.handle_event(Reporter::Event.new(type: :end, path: data_item.request.require_path, time:))
end