-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging middleware, sentry middleware
- Loading branch information
Showing
10 changed files
with
226 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"runtime/debug" | ||
"time" | ||
|
||
"github.com/getsentry/raven-go" | ||
"github.com/kataras/iris" | ||
"github.com/uber-go/zap" | ||
) | ||
|
||
//VersionMiddleware automatically adds a version header to response | ||
type VersionMiddleware struct { | ||
App *App | ||
} | ||
|
||
// Serve automatically adds a version header to response | ||
func (m *VersionMiddleware) Serve(c *iris.Context) { | ||
c.SetHeader("MQTTBOT-VERSION", VERSION) | ||
c.Next() | ||
} | ||
|
||
//RecoveryMiddleware recovers from errors in Iris | ||
type RecoveryMiddleware struct { | ||
OnError func(error, []byte) | ||
} | ||
|
||
//Serve executes on error handler when errors happen | ||
func (r RecoveryMiddleware) Serve(ctx *iris.Context) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
if r.OnError != nil { | ||
switch err.(type) { | ||
case error: | ||
r.OnError(err.(error), debug.Stack()) | ||
default: | ||
r.OnError(fmt.Errorf("%v", err), debug.Stack()) | ||
} | ||
} | ||
ctx.Panic() | ||
} | ||
}() | ||
ctx.Next() | ||
} | ||
|
||
//LoggerMiddleware is responsible for logging to Zap all requests | ||
type LoggerMiddleware struct { | ||
Logger zap.Logger | ||
} | ||
|
||
// Serve serves the middleware | ||
func (l *LoggerMiddleware) Serve(ctx *iris.Context) { | ||
log := l.Logger.With( | ||
zap.String("source", "request"), | ||
) | ||
|
||
//all except latency to string | ||
var ip, method, path string | ||
var status int | ||
var latency time.Duration | ||
var startTime, endTime time.Time | ||
|
||
path = ctx.PathString() | ||
method = ctx.MethodString() | ||
|
||
startTime = time.Now() | ||
|
||
ctx.Next() | ||
|
||
//no time.Since in order to format it well after | ||
endTime = time.Now() | ||
latency = endTime.Sub(startTime) | ||
|
||
status = ctx.Response.StatusCode() | ||
ip = ctx.RemoteAddr() | ||
|
||
reqLog := log.With( | ||
zap.Time("endTime", endTime), | ||
zap.Int("statusCode", status), | ||
zap.Duration("latency", latency), | ||
zap.String("ip", ip), | ||
zap.String("method", method), | ||
zap.String("path", path), | ||
) | ||
|
||
//request failed | ||
if status > 399 && status < 500 { | ||
reqLog.Warn("Request failed.") | ||
return | ||
} | ||
|
||
//request is ok, but server failed | ||
if status > 499 { | ||
reqLog.Error("Response failed.") | ||
return | ||
} | ||
|
||
//Everything went ok | ||
reqLog.Info("Request successful.") | ||
} | ||
|
||
// NewLoggerMiddleware returns the logger middleware | ||
func NewLoggerMiddleware(theLogger zap.Logger) iris.HandlerFunc { | ||
l := &LoggerMiddleware{Logger: theLogger} | ||
return l.Serve | ||
} | ||
|
||
//SentryMiddleware is responsible for sending all exceptions to sentry | ||
type SentryMiddleware struct { | ||
App *App | ||
} | ||
|
||
// Serve serves the middleware | ||
func (l *SentryMiddleware) Serve(ctx *iris.Context) { | ||
ctx.Next() | ||
|
||
if ctx.Response.StatusCode() > 499 { | ||
tags := map[string]string{ | ||
"source": "app", | ||
"type": "Internal server error", | ||
"url": ctx.Request.URI().String(), | ||
} | ||
raven.CaptureError(fmt.Errorf("%s", string(ctx.Response.Body())), tags) | ||
} | ||
} |
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,3 +1,3 @@ | ||
package app | ||
|
||
var VERSION = "0.9.6" | ||
var VERSION = "1.0.0" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Submodule raven-go
added at
ba4db4
Submodule atomic
added at
0c9e68
Submodule zap
added at
67b225