Skip to content

Commit

Permalink
update to support prometheus (bfenetworks#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxinheihei authored and iyangsj committed Nov 5, 2019
1 parent 658f80f commit f24e561
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 35 deletions.
4 changes: 2 additions & 2 deletions bfe_http2/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1568,8 +1568,8 @@ func (fr *Framer) readMetaFrame(f *HeadersFrame) (*MetaHeadersFrame, error) {

// only count size when invalid == nil
// so the state of headerSize and blockSize keep consistency
state.H2ReqHeaderOriginalSize.Inc(int(headerSize))
state.H2ReqHeaderCompressSize.Inc(blockSize)
state.H2ReqHeaderOriginalSize.Inc(uint(headerSize))
state.H2ReqHeaderCompressSize.Inc(uint(blockSize))

return mh, nil
}
Expand Down
4 changes: 2 additions & 2 deletions bfe_http2/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ func (w *writeResHeaders) writeFrame(ctx writeContext) error {
panic("unexpected empty hpack")
}

state.H2ResHeaderOriginalSize.Inc(headerSize)
state.H2ResHeaderCompressSize.Inc(len(headerBlock))
state.H2ResHeaderOriginalSize.Inc(uint(headerSize))
state.H2ResHeaderCompressSize.Inc(uint(len(headerBlock)))

// For now we're lazy and just pick the minimum MAX_FRAME_SIZE
// that all peers must support (16KB). Later we could care
Expand Down
6 changes: 3 additions & 3 deletions bfe_server/http_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (c *conn) serve() {
c.close()

if len(session.Proto) > 0 {
proxyState.ClientConnActiveInc(session.Proto, -1)
proxyState.ClientConnActiveDec(session.Proto, 1)
}
if session.ReqNumActive != 0 {
proxyState.ClientConnUnfinishedReq.Inc(1)
Expand Down Expand Up @@ -478,7 +478,7 @@ func (c *conn) serve() {
switch nextProto {
case bfe_websocket.WebSocket:
// update counters for websocket
proxyState.ClientConnActiveInc(c.session.Proto, -1)
proxyState.ClientConnActiveDec(c.session.Proto, 1)
c.session.Proto = bfe_websocket.Scheme(c.rwc)
proxyState.ClientConnServedInc(c.session.Proto, 1)
proxyState.ClientConnActiveInc(c.session.Proto, 1)
Expand Down Expand Up @@ -555,7 +555,7 @@ func (c *conn) serveRequest(w bfe_http.ResponseWriter, request *bfe_basic.Reques

// modify state counters
session.IncReqNumActive(-1)
proxyState.ClientReqActiveInc(session.Proto, -1)
proxyState.ClientReqActiveDec(session.Proto, 1)
if request.ErrCode != nil {
proxyState.ClientReqFail.Inc(1)
} else {
Expand Down
68 changes: 51 additions & 17 deletions bfe_server/proxy_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ type ProxyState struct {
SpdyClientReqServed *metrics.Counter

// active request
ClientReqActive *metrics.Counter
HttpClientReqActive *metrics.Counter
HttpsClientReqActive *metrics.Counter
Http2ClientReqActive *metrics.Counter
SpdyClientReqActive *metrics.Counter
ClientReqActive *metrics.Gauge
HttpClientReqActive *metrics.Gauge
HttpsClientReqActive *metrics.Gauge
Http2ClientReqActive *metrics.Gauge
SpdyClientReqActive *metrics.Gauge

// connection successful accepted
ClientConnServed *metrics.Counter
Expand All @@ -110,17 +110,17 @@ type ProxyState struct {
WssClientConnServed *metrics.Counter

// active connection
ClientConnActive *metrics.Counter
HttpClientConnActive *metrics.Counter
HttpsClientConnActive *metrics.Counter
Http2ClientConnActive *metrics.Counter
SpdyClientConnActive *metrics.Counter
StreamClientConnActive *metrics.Counter
WsClientConnActive *metrics.Counter
WssClientConnActive *metrics.Counter
ClientConnActive *metrics.Gauge
HttpClientConnActive *metrics.Gauge
HttpsClientConnActive *metrics.Gauge
Http2ClientConnActive *metrics.Gauge
SpdyClientConnActive *metrics.Gauge
StreamClientConnActive *metrics.Gauge
WsClientConnActive *metrics.Gauge
WssClientConnActive *metrics.Gauge
}

func (s *ProxyState) ClientConnServedInc(proto string, value int) {
func (s *ProxyState) ClientConnServedInc(proto string, value uint) {
switch proto {
case "http":
s.HttpClientConnServed.Inc(value)
Expand All @@ -140,7 +140,7 @@ func (s *ProxyState) ClientConnServedInc(proto string, value int) {
s.ClientConnServed.Inc(value)
}

func (s *ProxyState) ClientConnActiveInc(proto string, value int) {
func (s *ProxyState) ClientConnActiveInc(proto string, value uint) {
switch proto {
case "http":
s.HttpClientConnActive.Inc(value)
Expand All @@ -160,7 +160,27 @@ func (s *ProxyState) ClientConnActiveInc(proto string, value int) {
s.ClientConnActive.Inc(value)
}

func (s *ProxyState) ClientReqServedInc(proto string, value int) {
func (s *ProxyState) ClientConnActiveDec(proto string, value uint) {
switch proto {
case "http":
s.HttpClientConnActive.Dec(value)
case "https":
s.HttpsClientConnActive.Dec(value)
case "h2":
s.Http2ClientConnActive.Dec(value)
case "spdy/3.1":
s.SpdyClientConnActive.Dec(value)
case "ws":
s.WsClientConnActive.Dec(value)
case "wss":
s.WssClientConnActive.Dec(value)
case "stream":
s.StreamClientConnActive.Dec(value)
}
s.ClientConnActive.Dec(value)
}

func (s *ProxyState) ClientReqServedInc(proto string, value uint) {
switch proto {
case "http":
s.HttpClientReqServed.Inc(value)
Expand All @@ -174,7 +194,7 @@ func (s *ProxyState) ClientReqServedInc(proto string, value int) {
s.ClientReqServed.Inc(value)
}

func (s *ProxyState) ClientReqActiveInc(proto string, value int) {
func (s *ProxyState) ClientReqActiveInc(proto string, value uint) {
switch proto {
case "http":
s.HttpClientReqActive.Inc(value)
Expand All @@ -187,3 +207,17 @@ func (s *ProxyState) ClientReqActiveInc(proto string, value int) {
}
s.ClientReqActive.Inc(value)
}

func (s *ProxyState) ClientReqActiveDec(proto string, value uint) {
switch proto {
case "http":
s.HttpClientReqActive.Dec(value)
case "https":
s.HttpsClientReqActive.Dec(value)
case "h2":
s.Http2ClientReqActive.Dec(value)
case "spdy/3.1":
s.SpdyClientReqActive.Dec(value)
}
s.ClientReqActive.Dec(value)
}
4 changes: 2 additions & 2 deletions bfe_spdy/frame_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ func (f *Framer) readSynStreamFrame(h ControlFrameHeader, frame *SynStreamFrame)
return &Error{ZeroStreamId, 0}
}

state.SpdyReqHeaderOriginalSize.Inc(int(headerLen))
state.SpdyReqHeaderCompressSize.Inc(int(h.length + 8)) // size of SynStream frame
state.SpdyReqHeaderOriginalSize.Inc(uint(headerLen))
state.SpdyReqHeaderCompressSize.Inc(uint(h.length + 8)) // size of SynStream frame

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions bfe_spdy/frame_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ func (f *Framer) writeSynReplyFrame(frame *SynReplyFrame) (err error) {
}
f.headerBuf.Reset()

state.SpdyResHeaderOriginalSize.Inc(headerLen)
state.SpdyResHeaderCompressSize.Inc(int(frame.CFHeader.length + 8)) // size of SynReply frame
state.SpdyResHeaderOriginalSize.Inc(uint(headerLen))
state.SpdyResHeaderCompressSize.Inc(uint(frame.CFHeader.length + 8)) // size of SynReply frame

return
}
Expand Down
4 changes: 2 additions & 2 deletions bfe_stream/server_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ func TLSProxyHandler(s *Server, c net.Conn, b net.Conn, errCh chan error) {
// TODO: add read/write timeout
go func() {
n, err := io.Copy(b, c)
state.StreamBytesRecv.Inc(int(n))
state.StreamBytesRecv.Inc(uint(n))
errCh <- err
}()

go func() {
n, err := io.Copy(c, b)
state.StreamBytesSent.Inc(int(n))
state.StreamBytesSent.Inc(uint(n))
errCh <- err
}()
}
4 changes: 2 additions & 2 deletions bfe_websocket/server_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ func (sc *serverConn) websocketDataTransfer() {
// proxy data from client to backend
go func() {
n, err := io.Copy(sc.bconn, sc.cconn)
state.WebSocketBytesRecv.Inc(int(n))
state.WebSocketBytesRecv.Inc(uint(n))
errCh <- err
}()

// proxy data from backend to client
go func() {
n, err := io.Copy(sc.cconn, sc.bconn)
state.WebSocketBytesSent.Inc(int(n))
state.WebSocketBytesSent.Inc(uint(n))
errCh <- err
}()
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.12

require (
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56 // indirect
github.com/baidu/go-lib v0.0.0-20190731032112-26c6ce93bc54
github.com/baidu/go-lib v0.0.0-20191102055223-5926b52ca0eb
github.com/gomodule/redigo v2.0.0+incompatible
github.com/pquerna/ffjson v0.0.0-20181028064349-e517b90714f7
github.com/spaolacci/murmur3 v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56 h1:Wi5Tgn8K+jDcBYL+dIMS1+qXYH2r7tpRAyBgqrWfQtw=
github.com/asergeyev/nradix v0.0.0-20170505151046-3872ab85bb56/go.mod h1:8BhOLuqtSuT5NZtZMwfvEibi09RO3u79uqfHZzfDTR4=
github.com/baidu/go-lib v0.0.0-20190731032112-26c6ce93bc54 h1:4HBPoPOT40rDqYOTR4c1me8Q7+YWioEY0KuwGh28KSM=
github.com/baidu/go-lib v0.0.0-20190731032112-26c6ce93bc54/go.mod h1:FneHDqz3wLeDGdWfRyW4CzBbCwaqesLGIFb09N80/ww=
github.com/baidu/go-lib v0.0.0-20191102055223-5926b52ca0eb h1:DDyxlHtwU2BjVvtpSgpF/ofmYkXj8K8mo5DS17OTiZg=
github.com/baidu/go-lib v0.0.0-20191102055223-5926b52ca0eb/go.mod h1:FneHDqz3wLeDGdWfRyW4CzBbCwaqesLGIFb09N80/ww=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4=
Expand Down

0 comments on commit f24e561

Please sign in to comment.