Skip to content

Warn when enabling socket_telemetry on unsupported platform#23

Open
stevenharman wants to merge 1 commit intobabbel:mainfrom
stevenharman:handle_socket_telemetry_on_macos
Open

Warn when enabling socket_telemetry on unsupported platform#23
stevenharman wants to merge 1 commit intobabbel:mainfrom
stevenharman:handle_socket_telemetry_on_macos

Conversation

@stevenharman
Copy link
Contributor

@stevenharman stevenharman commented May 13, 2024

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, and we can unpack the binary info thusly:

require 'socket'

sock = TCPSocket.new('example.com', 80)
info = sock.getsockopt(Socket::IPPROTO_TCP, Socket::TCP_CONNECTION_INFO)

# Struct: 2x uint8 (C2), 26x uint32 (L26)
fields = info.unpack("C2L26")

tcp_connection_info = {
  tcpi_state: fields[0],
  tcpi_snd_wscale: fields[1] >> 4,
  tcpi_rcv_wscale: fields[1] & 0x0F,
  tcpi_options: fields[2],
  tcpi_flags: fields[3],
  tcpi_rto: fields[4],
  tcpi_maxseg: fields[5],
  tcpi_snd_ssthresh: fields[6],
  tcpi_snd_cwnd: fields[7],
  tcpi_snd_wnd: fields[8],
  tcpi_rcv_wnd: fields[9],
  tcpi_rttcur: fields[10],
  tcpi_srtt: fields[11],
  tcpi_rttvar: fields[12],
  tcpi_txpackets: fields[13],
  tcpi_rxpackets: fields[14],
  tcpi_txbytes: fields[15],
  tcpi_rxbytes: fields[16],
  tcpi_last_data_recv: fields[17],
  tcpi_last_ack_recv: fields[18],
  tcpi_last_data_sent: fields[19],
  tcpi_last_ack_sent: fields[20],
  tcpi_pmtu: fields[21],
  tcpi_rcv_ssthresh: fields[22],
  tcpi_snd_rexmitpack: fields[23],
  tcpi_rcv_ooopack: fields[24],
  tcpi_snd_zerowin: fields[25],
  # add more fields as needed
}

puts tcp_connection_info

However, macOS doesn't expose the number of unacked connections like various Linux kernels do. There is, as of now, to my knowledge, no way to get this info without using some other OS level tool.

@stevenharman stevenharman force-pushed the handle_socket_telemetry_on_macos branch from 9e6a3cd to 5746684 Compare May 13, 2024 13:36
Comment on lines -126 to +130
expect(possible_lines.include?(line)).to eq(true)
expect(possible_lines).to include(line)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in a more useful error message if the line doesn't match.

@stevenharman stevenharman force-pushed the handle_socket_telemetry_on_macos branch from 5746684 to 04b2f10 Compare May 14, 2024 00:24
@stevenharman
Copy link
Contributor Author

Anyone opposed to this one? It's a nice experience improvement - certainly better than blowing up!

@stevenharman stevenharman force-pushed the handle_socket_telemetry_on_macos branch from 04b2f10 to 469ccaf Compare July 3, 2024 18:30
@stevenharman
Copy link
Contributor Author

@unsign3d Thoughts on getting this one in as well? It's a nice quality of life improvement for folks running on macOS locally, while being fully compatible with how things work now.

@stevenharman stevenharman force-pushed the handle_socket_telemetry_on_macos branch 2 times, most recently from c1d08a7 to 0ae608f Compare September 17, 2025 19:48
@stevenharman
Copy link
Contributor Author

👋 Hello.

I've rebased this little nice-to-have atop the latest. I'm not sure who to tag as it's unclear who's maintaining this repo (and the published Gem). So I'll try a few of the folks who seem to have been active lately - @unsign3d @stoyanzhekov @jtsaito @amrrbakry

We've been running it in production for over a year now. We are very interested in the future of this plugin and I would be happy to help with maintenance if you need a hand.

Thank you!

@stevenharman
Copy link
Contributor Author

Oh, and I should note - without this change, it's impossible to successfully run the test suite on macOS. You have to skip the it 'logs socket telemetry' spec from spec/integration/plugin_spec.rb to get a green run of the suite.

stevenharman added a commit to stevenharman/puma-plugin-telemetry_too that referenced this pull request Sep 19, 2025
Until babbel#35 is merged, we need to hack in our own Puma 7 support.
babbel#35

Similarly, this has a hack for babbel#23 so we can run tests on macOS.
babbel#23
stevenharman added a commit to stevenharman/puma-plugin-telemetry_too that referenced this pull request Sep 23, 2025
Until babbel#35 is merged, we need to hack in our own Puma 7 support.
babbel#35

Similarly, this has a hack for babbel#23 so we can run tests on macOS.
babbel#23
@stevenharman stevenharman force-pushed the handle_socket_telemetry_on_macos branch from 0ae608f to 2e8c736 Compare September 24, 2025 18:09
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, and we can unpack the
binary info thusly:

```ruby
require 'socket'

sock = TCPSocket.new('example.com', 80)
info = sock.getsockopt(Socket::IPPROTO_TCP, Socket::TCP_CONNECTION_INFO)

 # Struct: 2x uint8 (C2), 26x uint32 (L26)
fields = info.unpack("C2L26")

tcp_connection_info = {
  tcpi_state: fields[0],
  tcpi_snd_wscale: fields[1] >> 4,
  tcpi_rcv_wscale: fields[1] & 0x0F,
  tcpi_options: fields[2],
  tcpi_flags: fields[3],
  tcpi_rto: fields[4],
  tcpi_maxseg: fields[5],
  tcpi_snd_ssthresh: fields[6],
  tcpi_snd_cwnd: fields[7],
  tcpi_snd_wnd: fields[8],
  tcpi_rcv_wnd: fields[9],
  tcpi_rttcur: fields[10],
  tcpi_srtt: fields[11],
  tcpi_rttvar: fields[12],
  tcpi_txpackets: fields[13],
  tcpi_rxpackets: fields[14],
  tcpi_txbytes: fields[15],
  tcpi_rxbytes: fields[16],
  tcpi_last_data_recv: fields[17],
  tcpi_last_ack_recv: fields[18],
  tcpi_last_data_sent: fields[19],
  tcpi_last_ack_sent: fields[20],
  tcpi_pmtu: fields[21],
  tcpi_rcv_ssthresh: fields[22],
  tcpi_snd_rexmitpack: fields[23],
  tcpi_rcv_ooopack: fields[24],
  tcpi_snd_zerowin: fields[25],
  # add more fields as needed
}

puts tcp_connection_info
```

However, macOS doesn't expose the number of unacked connections like
various Linux kernels do. There is, as of now, to my knowledge, no way
to get this info without using some other OS level tool.
@stevenharman stevenharman force-pushed the handle_socket_telemetry_on_macos branch from 2e8c736 to f591323 Compare September 24, 2025 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants