add a rollbar example

Former-commit-id: 4cbe61a217f4863fe38e23848252126d864d9f5c
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-06-09 02:58:17 +03:00
parent 46ff69e28e
commit 2042637e41
3 changed files with 111 additions and 1 deletions

View File

@ -371,7 +371,7 @@ Other Improvements:
![DBUG routes](https://iris-go.com/images/v12.2.0-dbug2.png?v=0) ![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. - New builtin [requestid](https://github.com/kataras/iris/tree/master/middleware/requestid) middleware.
@ -445,6 +445,8 @@ New Context Methods:
Breaking Changes: 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. - `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`. - `iris.CookieEncode` and `CookieDecode` are replaced with the `iris.CookieEncoding`.
- `sessions#Config.Encode` and `Decode` are removed in favor of (the existing) `Encoding` field. - `sessions#Config.Encode` and `Decode` are removed in favor of (the existing) `Encoding` field.

View File

@ -70,6 +70,7 @@
* [Log Requests to a JSON File](logging/request-logger/request-logger-file-json/main.go) * [Log Requests to a JSON File](logging/request-logger/request-logger-file-json/main.go)
* [Application File Logger](logging/file-logger/main.go) * [Application File Logger](logging/file-logger/main.go)
* [Application JSON Logger](logging/json-logger/main.go) * [Application JSON Logger](logging/json-logger/main.go)
* [Rollbar](logging/rollbar/main.go)
* API Documentation * API Documentation
* [Yaag](apidoc/yaag/main.go) * [Yaag](apidoc/yaag/main.go)
* [Swagger](https://github.com/iris-contrib/swagger/tree/master/example) * [Swagger](https://github.com/iris-contrib/swagger/tree/master/example)

View File

@ -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("<h1> Index Page </h1>")
}
func panicMe(ctx iris.Context) {
panic("a critical error message here")
}