-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
162 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= | ||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= | ||
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= | ||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= | ||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= | ||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= | ||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package tcp | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"math" | ||
"os" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
RemoteAddr = "addr" | ||
RequestLength = "req_size" | ||
ResponseLength = "resp_size" | ||
Latency = "latency" | ||
Hostname = "server" | ||
) | ||
|
||
func newMessage(req *Request) *message { | ||
// starts the UTC timer. | ||
m := &message{ | ||
start: time.Now().UTC(), | ||
req: req, | ||
} | ||
// reads the request body without closing it to get its size. | ||
if req.Body != nil { | ||
buf, _ := ioutil.ReadAll(req.Body) | ||
req.Body = ioutil.NopCloser(bytes.NewBuffer(buf)) | ||
m.reqSize = len(buf) | ||
} | ||
return m | ||
} | ||
|
||
type message struct { | ||
latency time.Duration | ||
req *Request | ||
reqSize int | ||
start time.Time | ||
} | ||
|
||
func (m *message) fields(w ResponseWriter, f logrus.Fields) logrus.Fields { | ||
d := make(logrus.Fields) | ||
for k := range f { | ||
switch k { | ||
case RemoteAddr: | ||
d[k] = m.req.RemoteAddr | ||
case RequestLength: | ||
d[k] = w.Size() | ||
case ResponseLength: | ||
d[k] = m.reqSize | ||
case Latency: | ||
m.latency = time.Since(m.start) | ||
d[k] = int(math.Ceil(float64(m.latency.Nanoseconds()) / 1000.0)) | ||
case Hostname: | ||
d[k], _ = os.Hostname() | ||
} | ||
} | ||
return d | ||
} | ||
|
||
// String implements the fmt.Stringer interface. | ||
func (m *message) String() string { | ||
sep := " | " | ||
return "[TCP] " + m.start.Format(time.RFC3339) + sep + m.req.Segment | ||
} | ||
|
||
// Logger returns a middleware to log each TCP request. | ||
func Logger(log *logrus.Logger, fields logrus.Fields) HandlerFunc { | ||
return func(c *Context) { | ||
// Initiates the timer | ||
m := newMessage(c.Request) | ||
// Processes the request | ||
c.Next() | ||
// Logs it. | ||
entry := logrus.NewEntry(log).WithFields(m.fields(c.ResponseWriter, fields)) | ||
if e := c.Err(); e == nil { | ||
entry.Info(m.String()) | ||
} else if e.Recovered() { | ||
entry.Error(m.String() + " " + e.Error()) | ||
} else { | ||
entry.Warn(m.String() + " " + e.Error()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters