Skip to content

Commit

Permalink
add server connection count metric (#464)
Browse files Browse the repository at this point in the history
* add server connection count metric

* add error check for open metric

* update to noop on false

* update metric name

* make transport wrapper private

* Update thriftbp/server_transport.go

Co-authored-by: Kyle Lemons <[email protected]>

* Update config.go

* switch for gauge for connection metrics

* remove metrics config flag

* update docs

Co-authored-by: Kyle Lemons <[email protected]>
  • Loading branch information
marcoferrer and kylelemons authored Feb 1, 2022
1 parent 71d3d5e commit 2c2736a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
12 changes: 11 additions & 1 deletion thriftbp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ type ServerConfig struct {
// If not set none of the requests will be sampled.
ReportPayloadSizeMetricsSampleRate float64

// Optional, used by NewBaseplateServer and NewServer.
//
// Report the number of clients connected to the server as a runtime gauge
// with metric name of 'thrift.connections'
ReportConnectionCount bool

// Optional, used only by NewServer.
// In NewBaseplateServer the address set in bp.Config() will be used instead.
//
Expand All @@ -69,7 +75,7 @@ type ServerConfig struct {
// and protocol to serve the given TProcessor which is wrapped with the
// given ProcessorMiddlewares.
func NewServer(cfg ServerConfig) (*thrift.TSimpleServer, error) {
var transport *thrift.TServerSocket
var transport thrift.TServerTransport
if cfg.Socket == nil {
var err error
transport, err = thrift.NewTServerSocket(cfg.Addr)
Expand All @@ -80,6 +86,10 @@ func NewServer(cfg ServerConfig) (*thrift.TSimpleServer, error) {
transport = cfg.Socket
}

if cfg.ReportConnectionCount {
transport = &CountedTServerTransport{transport}
}

server := thrift.NewTSimpleServer4(
thrift.WrapProcessor(cfg.Processor, cfg.Middlewares...),
transport,
Expand Down
54 changes: 54 additions & 0 deletions thriftbp/server_transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package thriftbp

import (
"sync"

"github.com/apache/thrift/lib/go/thrift"
"github.com/go-kit/kit/metrics"

"github.com/reddit/baseplate.go/metricsbp"
)

const meterNameTransportConnCounter = "thrift.connections"

type CountedTServerTransport struct {
thrift.TServerTransport
}

func (m *CountedTServerTransport) Accept() (thrift.TTransport, error) {
transport, err := m.TServerTransport.Accept()
if err != nil {
return nil, err
}

return newCountedTTransport(transport), nil
}

type countedTTransport struct {
thrift.TTransport

gauge metrics.Gauge
closeOnce sync.Once
}

func newCountedTTransport(transport thrift.TTransport) thrift.TTransport {
return &countedTTransport{
TTransport: transport,
gauge: metricsbp.M.RuntimeGauge(meterNameTransportConnCounter),
}
}

func (m *countedTTransport) Close() error {
m.closeOnce.Do(func() {
m.gauge.Add(-1)
})
return m.TTransport.Close()
}

func (m *countedTTransport) Open() error {
if err := m.TTransport.Open(); err != nil {
return err
}
m.gauge.Add(1)
return nil
}

0 comments on commit 2c2736a

Please sign in to comment.