From 2042637e417ffac5cb6c04055c8fafc61da8154c Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Tue, 9 Jun 2020 02:58:17 +0300 Subject: [PATCH] add a rollbar example Former-commit-id: 4cbe61a217f4863fe38e23848252126d864d9f5c --- HISTORY.md | 4 +- _examples/README.md | 1 + _examples/logging/rollbar/main.go | 107 ++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 _examples/logging/rollbar/main.go diff --git a/HISTORY.md b/HISTORY.md index 69b8d712..68e80cbe 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -371,7 +371,7 @@ Other Improvements: ![DBUG routes](https://iris-go.com/images/v12.2.0-dbug2.png?v=0) -- Fixed handler's error response not be respected when response recorder or gzip writer was used instead of the common writer. Fixes [#1531](https://github.com/kataras/iris/issues/1531). It contains a **BREAKING CHANGE** of: the new `Configuration.ResetOnFireErrorCode` field should be set **to true** in order to behave as it used before this update (to reset the contents on recorder or gzip writer). +- New [rollbar example](https://github.com/kataras/iris/tree/master/_examples/logging/rollbar/main.go). - New builtin [requestid](https://github.com/kataras/iris/tree/master/middleware/requestid) middleware. @@ -445,6 +445,8 @@ New Context Methods: Breaking Changes: + +- Fixed handler's error response not be respected when response recorder or gzip writer was used instead of the common writer. Fixes [#1531](https://github.com/kataras/iris/issues/1531). It contains a **BREAKING CHANGE** of: the new `Configuration.ResetOnFireErrorCode` field should be set **to true** in order to behave as it used before this update (to reset the contents on recorder or gzip writer). - `Context.String()` (rarely used by end-developers) it does not return a unique string anymore, to achieve the old representation you must call the new `Context.SetID` method first. - `iris.CookieEncode` and `CookieDecode` are replaced with the `iris.CookieEncoding`. - `sessions#Config.Encode` and `Decode` are removed in favor of (the existing) `Encoding` field. diff --git a/_examples/README.md b/_examples/README.md index 14c84c95..2fde27e1 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -70,6 +70,7 @@ * [Log Requests to a JSON File](logging/request-logger/request-logger-file-json/main.go) * [Application File Logger](logging/file-logger/main.go) * [Application JSON Logger](logging/json-logger/main.go) + * [Rollbar](logging/rollbar/main.go) * API Documentation * [Yaag](apidoc/yaag/main.go) * [Swagger](https://github.com/iris-contrib/swagger/tree/master/example) diff --git a/_examples/logging/rollbar/main.go b/_examples/logging/rollbar/main.go new file mode 100644 index 00000000..d2b37c11 --- /dev/null +++ b/_examples/logging/rollbar/main.go @@ -0,0 +1,107 @@ +package main + +import ( + "errors" + "fmt" + "os" + "runtime/debug" + + "github.com/kataras/iris/v12" + "github.com/kataras/iris/v12/middleware/requestid" + + "github.com/rollbar/rollbar-go" +) + +// * https://rollbar.com/signup +// * https://docs.rollbar.com/docs/go +func init() { + token := os.Getenv("ROLLBAR_TOKEN") // replace that with your token. + if token == "" { + panic("ROLLBAR_TOKEN is missing") + } + + // rb := rollbar.NewAsync(token, "production", "", hostname, "github.com/kataras/iris") + // Or use the package-level instance: + rollbar.SetToken(token) + // defaults to "development" + rollbar.SetEnvironment("production") + // optional Git hash/branch/tag (required for GitHub integration) + // rollbar.SetCodeVersion("v2") + // optional override; defaults to hostname + // rollbar.SetServerHost("web.1") + // path of project (required for GitHub integration and non-project stacktrace collapsing) + rollbar.SetServerRoot("github.com/kataras/iris") + +} + +func main() { + app := iris.New() + // A middleware which sets the ctx.GetID (or requestid.Get(ctx)). + app.Use(requestid.New()) + // A recover middleware which sends the error trace to the rollbar. + app.Use(func(ctx iris.Context) { + defer func() { + if r := recover(); r != nil { + debug.PrintStack() + + file, line := ctx.HandlerFileLine() // the failed handler's source code position. + + // cause other info + rollbar.Critical(errors.New(fmt.Sprint(r)), iris.Map{ + "request_id": ctx.GetID(), + "request_ip": ctx.RemoteAddr(), + "request_uri": ctx.FullRequestURI(), + "handler": iris.Map{ + "name": ctx.HandlerName(), // the handler which failed. + "file": fmt.Sprintf("%s:%d", file, line), + }, + }) + + ctx.StopWithStatus(iris.StatusInternalServerError) + } + }() + + ctx.Next() + }) + + app.Get("/", index) + app.Get("/panic", panicMe) + + // http://localhost:8080 should add an info message to the rollbar's "Items" dashboard. + // http://localhost:8080/panic should add a critical message to the rollbar's "Items" dashboard, + // with the corresponding information appending on its "Occurrences" tab item, e.g: + // Timestamp (PDT) + // * 2020-06-08 04:47 pm + // + // server.host + // * DESKTOP-HOSTNAME + // + // trace_chain.0.exception.message + // * a critical error message here + // + // custom.handler.file + // * C:/mygopath/src/github.com/kataras/iris/_examples/loggin/rollbar/main.go:76 + // + // custom.handler.name + // * main.panicMe + // + // custom.request_id + // * cce61665-0c1b-4fb5-8547-06a3537e477c + // + // custom.request_ip + // * ::1 + // + // custom.request_uri + // * http://localhost:8080/panic + app.Listen(":8080") +} + +func index(ctx iris.Context) { + rollbar.Info(fmt.Sprintf("Index page requested by %s", ctx.RemoteAddr())) + + ctx.HTML("

Index Page

") +} + +func panicMe(ctx iris.Context) { + panic("a critical error message here") +}