Module: DeadBro::HttpInstrumentation
- Defined in:
- lib/dead_bro/http_instrumentation.rb
Constant Summary collapse
- EVENT_NAME =
"outgoing.http"- THREAD_LOCAL_KEY =
:dead_bro_http_events- MAX_TRACKED_EVENTS =
1000
Class Method Summary collapse
- .install!(client: Client.new) ⇒ Object
- .install_net_http!(client) ⇒ Object
- .install_typhoeus!(client) ⇒ Object
-
.should_continue_tracking? ⇒ Boolean
Check if we should continue tracking based on count and time limits.
Class Method Details
.install!(client: Client.new) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/dead_bro/http_instrumentation.rb', line 12 def self.install!(client: Client.new) install_net_http!(client) install_typhoeus!(client) if defined?(::Typhoeus) rescue # Never raise from instrumentation install end |
.install_net_http!(client) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dead_bro/http_instrumentation.rb', line 19 def self.install_net_http!(client) mod = Module.new do define_method(:request) do |req, body = nil, &block| start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) response = nil error = nil begin response = super(req, body, &block) response rescue Exception => e error = e raise ensure finish_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) duration_ms = ((finish_time - start_time) * 1000.0).round(2) begin uri = begin URI.parse(req.uri ? req.uri.to_s : "http://#{@address}:#{@port}#{req.path}") rescue nil end # Skip instrumentation for our own APM endpoint to prevent infinite loops, # but do NOT alter the original method's return value/control flow. skip_instrumentation = uri && (uri.to_s.include?("localhost") || uri.to_s.include?("aberatii.com")) unless skip_instrumentation payload = { library: "net_http", method: req.method, url: uri && uri.to_s, host: (uri && uri.host) || @address, path: (uri && uri.path) || req.path, status: response && response.code.to_i, duration_ms: duration_ms, exception: error && error.class.name } # Accumulate per-request; only send with controller metric if Thread.current[THREAD_LOCAL_KEY] && should_continue_tracking? Thread.current[THREAD_LOCAL_KEY] << payload end end rescue end end end end ::Net::HTTP.prepend(mod) unless ::Net::HTTP.ancestors.include?(mod) end |
.install_typhoeus!(client) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/dead_bro/http_instrumentation.rb', line 70 def self.install_typhoeus!(client) mod = Module.new do define_method(:run) do |*args| start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) response = nil begin response = super(*args) response ensure finish_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) duration_ms = ((finish_time - start_time) * 1000.0).round(2) begin req_url = if respond_to?(:url) url else (respond_to?(:base_url) ? base_url : nil) end # Skip instrumentation for our own APM endpoint to prevent infinite loops, # but do NOT alter the original method's return value/control flow. skip_instrumentation = req_url && (req_url.include?("localhost:3100/apm/v1/metrics") || req_url.include?("deadbro.aberatii.com/apm/v1/metrics")) unless skip_instrumentation payload = { library: "typhoeus", method: (respond_to?(:options) && [:method]) ? [:method].to_s.upcase : nil, url: req_url, status: response && response.code, duration_ms: duration_ms } # Accumulate per-request; only send with controller metric if Thread.current[THREAD_LOCAL_KEY] && should_continue_tracking? Thread.current[THREAD_LOCAL_KEY] << payload end end rescue end end end end ::Typhoeus::Request.prepend(mod) unless ::Typhoeus::Request.ancestors.include?(mod) rescue end |
.should_continue_tracking? ⇒ Boolean
Check if we should continue tracking based on count and time limits
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/dead_bro/http_instrumentation.rb', line 116 def self.should_continue_tracking? events = Thread.current[THREAD_LOCAL_KEY] return false unless events # Check count limit return false if events.length >= MAX_TRACKED_EVENTS # Check time limit start_time = Thread.current[DeadBro::TRACKING_START_TIME_KEY] if start_time elapsed_seconds = Time.now - start_time return false if elapsed_seconds >= DeadBro::MAX_TRACKING_DURATION_SECONDS end true end |