-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
log connection metrics on connection closed
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,76 @@ | ||
package smokescreen | ||
|
||
import ( | ||
"fmt" | ||
"github.com/sirupsen/logrus" | ||
"net" | ||
"time" | ||
) | ||
|
||
type ConnExt struct { | ||
net.Conn | ||
Config *Config | ||
Role string | ||
OutboundHost string | ||
StartTime time.Time | ||
|
||
BytesIn int | ||
BytesOut int | ||
Wakeups int | ||
} | ||
|
||
func NewConnExt( | ||
conn net.Conn, | ||
config *Config, role, | ||
outboundHost string, | ||
startTime time.Time) *ConnExt { | ||
return &ConnExt{ | ||
conn, | ||
config, | ||
role, | ||
outboundHost, | ||
startTime, | ||
0, | ||
0, | ||
0, | ||
} | ||
} | ||
|
||
func (c *ConnExt) Close() error { | ||
c.Config.StatsdClient.Incr("cn.close", []string{}, 1) | ||
endTime := time.Now() | ||
duration := endTime.Sub(c.StartTime).Seconds() | ||
|
||
tags := []string{ | ||
fmt.Sprintf("role:%s", c.Role), | ||
} | ||
|
||
c.Config.StatsdClient.Incr("cn.close", tags, 1) | ||
c.Config.StatsdClient.Histogram("cn.duration", duration, tags, 1) | ||
c.Config.StatsdClient.Histogram("cn.bytes_in", float64(c.BytesIn), tags, 1) | ||
c.Config.StatsdClient.Histogram("cn.bytes_out", float64(c.BytesOut), tags, 1) | ||
|
||
c.Config.Log.WithFields(logrus.Fields{ | ||
"bytes_in": c.BytesIn, | ||
"bytes_out": c.BytesOut, | ||
"role": c.Role, | ||
"req_host": c.OutboundHost, | ||
"remote_addr": c.Conn.RemoteAddr(), | ||
"start_time": c.StartTime.UTC(), | ||
"end_time": endTime.UTC(), | ||
"duration": duration, | ||
"wakeups": c.Wakeups, | ||
}).Info("CANONICAL-PROXY-CN-CLOSE") | ||
return c.Conn.Close() | ||
} | ||
|
||
func (c *ConnExt) Read(b []byte) (n int, err error) { | ||
c.BytesIn += len(b) | ||
c.Wakeups += 1 | ||
return c.Conn.Read(b) | ||
} | ||
|
||
func (c *ConnExt) Write(b []byte) (n int, err error) { | ||
c.BytesOut += len(b) | ||
c.Wakeups += 1 | ||
return c.Conn.Write(b) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.