connectrpc / connect-go

The Go implementation of Connect: Protobuf RPC that works.
https://connectrpc.com
Apache License 2.0
2.76k stars 90 forks source link

How can I find out the number of connections to the server in interceptor? #761

Open blackmarllbor0 opened 4 days ago

blackmarllbor0 commented 4 days ago

To collect metrics I first tried to use otel-connect, but it did not fit the functionality, and I started to make my own interceptor, and faced the problem of getting active connections to the server. How can I find out how many active connections there are now and how can I monitor if a connection has been disconnected?

jhump commented 3 days ago

@blackmarllbor0, nothing in Connect (or even in net/http, if using lower-level middleware) exposes socket details. So if you need to capture that sort of state for metrics or logging, you'd need to provide a custom listener when creating the server, and instrument that listener to handle accepting of new connections. The listener would also need to wrap the accepted net.Conn with a custom implementation that is instrumented to detect when the connection is closed and handle that.

There's a 3rd party library that does exactly this: https://github.com/mwitkow/go-conntrack Note that we do not have experience with that particular library and don't necessarily endorse it, but it looks like the "right shape" for what you're trying to do.