File: internal_telemetry.rb

package info (click to toggle)
ruby-httpx 1.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,816 kB
  • sloc: ruby: 12,209; makefile: 4
file content (107 lines) | stat: -rw-r--r-- 3,129 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
69
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
# frozen_string_literal: true

module HTTPX
  module Plugins
    #
    # The InternalTelemetry plugin is for internal use only. It is therefore undocumented, and
    # its use is disencouraged, as API compatiblity will **not be guaranteed**.
    #
    # The gist of it is: when debug_level of logger is enabled to 3 or greater, considered internal-only
    # supported log levels, it'll be loaded by default.
    #
    # Against a specific point of time, which will be by default the session initialization, but can be set
    # by the end user in $http_init_time, different diff metrics can be shown. The "point of time" is calculated
    # using the monotonic clock.
    module InternalTelemetry
      DEBUG_LEVEL = 3

      def self.extra_options(options)
        options.merge(debug_level: 3)
      end

      module TrackTimeMethods
        private

        def elapsed_time
          yield
        ensure
          meter_elapsed_time("#{self.class.superclass}##{caller_locations(1, 1)[0].label}")
        end

        def meter_elapsed_time(label)
          $http_init_time ||= Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
          prev_time = $http_init_time
          after_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
          # $http_init_time = after_time
          elapsed = after_time - prev_time
          # klass = self.class

          # until (class_name = klass.name)
          #   klass = klass.superclass
          # end
          log(
            level: DEBUG_LEVEL,
            color: :red,
            debug_level: @options ? @options.debug_level : DEBUG_LEVEL,
            debug: nil
          ) do
            "[ELAPSED TIME]: #{label}: #{elapsed} (ms)\e[0m"
          end
        end
      end

      module InstanceMethods
        def self.included(klass)
          klass.prepend TrackTimeMethods
          super
        end

        def initialize(*)
          meter_elapsed_time("Session: initializing...")
          super
          meter_elapsed_time("Session: initialized!!!")
        end

        def close(*)
          super
          meter_elapsed_time("Session -> close")
        end

        private

        def build_requests(*)
          elapsed_time { super }
        end

        def fetch_response(*)
          response = super
          meter_elapsed_time("Session -> response") if response
          response
        end

        def coalesce_connections(conn1, conn2, selector, *)
          result = super

          meter_elapsed_time("Connection##{conn2.object_id} coalescing to Connection##{conn1.object_id}") if result

          result
        end
      end

      module PoolMethods
        def self.included(klass)
          klass.prepend Loggable
          klass.prepend TrackTimeMethods
          super
        end

        def checkin_connection(connection)
          super.tap do
            meter_elapsed_time("Pool##{object_id}: checked in connection for Connection##{connection.object_id}[#{connection.origin}]}")
          end
        end
      end
    end
    register_plugin :internal_telemetry, InternalTelemetry
  end
end