-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
107 lines (91 loc) · 2.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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/logging/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")
}