Skip to content

Commit

Permalink
Warn when enabling socket_telemetry on unsupported platform
Browse files Browse the repository at this point in the history
The structs we depend on for collecting socket telemetry (`SOL_TCP` and
`TCP_INFO`) don't exist on all platforms. In such cases, these constants
won't be defined, and so we should WARN users and not enabled the
`socket_telemetry` option.

NOTE: On macOS, similar structs do exist: `Socket::IPPROTO_TCP` and
`Socket::TCP_CONNECTION_INFO`, respectively, but I couldn't figure out
the binary layout of the data, so couldn't unpack it. Maybe someone else
knows more about those details and can dig in at some point.
  • Loading branch information
stevenharman committed May 14, 2024
1 parent 03cd32b commit 04b2f10
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/puma/plugin/telemetry/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ def enabled?
end

def socket_telemetry!
@socket_telemetry = true
# These structs are platform specific, and not available on macOS,
# for example. If they're undefined, then we cannot capture socket
# telemetry. We'll warn in that case.
if defined?(Socket::SOL_TCP) && defined?(Socket::TCP_INFO)
@socket_telemetry = true
else
warn("Cannot capture socket telemetry on this platform (#{RUBY_PLATFORM}); socket_telemetry is disabled.")
end
end

def socket_telemetry?
Expand Down
6 changes: 5 additions & 1 deletion spec/integration/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def make_request
end

it 'logs socket telemetry' do
unless defined?(Socket::SOL_TCP) && defined?(Socket::TCP_INFO)
skip("Socket::SOL_TCP not defined on #{RUBY_PLATFORM}")
end

threads = Array.new(2) { make_request }

sleep 0.1
Expand All @@ -123,7 +127,7 @@ def make_request
possible_lines = ['queue.backlog=1 sockets.backlog=5',
'queue.backlog=0 sockets.backlog=6']

expect(possible_lines.include?(line)).to eq(true)
expect(possible_lines).to include(line)

total = line.split.sum { |kv| kv.split('=').last.to_i }
expect(total).to eq 6
Expand Down

0 comments on commit 04b2f10

Please sign in to comment.