Class: Puma::Plugin::Telemetry::SocketData

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/plugin/telemetry/data.rb

Overview

Pulls TCP INFO data from socket

Constant Summary collapse

UNACKED_REGEXP =
/\ unacked=(?<unacked>\d+)\ /.freeze

Instance Method Summary collapse

Constructor Details

#initialize(ios, parser) ⇒ SocketData

Returns a new instance of SocketData.



103
104
105
106
107
108
109
110
111
# File 'lib/puma/plugin/telemetry/data.rb', line 103

def initialize(ios, parser)
  @sockets = ios.select { |io| io.respond_to?(:getsockopt) && io.is_a?(TCPSocket) }
  @parser =
    case parser
    when :inspect then method(:parse_with_inspect)
    when :unpack then method(:parse_with_unpack)
    when Proc then parser
    end
end

Instance Method Details

#metricsObject



123
124
125
126
127
# File 'lib/puma/plugin/telemetry/data.rb', line 123

def metrics
  {
    'sockets.backlog' => unacked
  }
end

#unackedObject

Number of unacknowledged connections in the sockets, which we know as socket backlog.



116
117
118
119
120
121
# File 'lib/puma/plugin/telemetry/data.rb', line 116

def unacked
  @sockets.sum do |socket|
    @parser.call(socket.getsockopt(Socket::SOL_TCP,
                                   Socket::TCP_INFO))
  end
end