package main
import (
"errors"
"github.com/airbrake/gobrake/v4"
)
var airbrake = gobrake.NewNotifierWithOptions(&gobrake.NotifierOptions{
ProjectId: 123456,
ProjectKey: "FIXME",
Environment: "production",
})
func init() {
airbrake.AddFilter(func(notice *gobrake.Notice) *gobrake.Notice {
notice.Params["user"] = map[string]string{
"id": "1",
"username": "johnsmith",
"name": "John Smith",
}
return notice
})
}
func main() {
defer airbrake.Close()
defer airbrake.NotifyOnPanic()
airbrake.Notify(errors.New("operation failed"), nil)
}
airbrake.AddFilter(func(notice *gobrake.Notice) *gobrake.Notice {
if notice.Context["environment"] == "development" {
// Ignore notices in development environment.
return nil
}
return notice
})
Severity allows
categorizing how severe an error is. By default, it's set to error
. To
redefine severity, simply overwrite context/severity
of a notice object. For
example:
notice := airbrake.NewNotice("operation failed", nil, 3)
notice.Context["severity"] = "critical"
airbrake.Notify(notice, nil)
You can use glog fork to send your logs to Airbrake.
In order to collect some basic routes stats you can instrument your application using notifier.Routes.Notify
API:
notifier.Routes.Notify(ctx, &gobrake.RouteTrace{
Method: c.Request.Method,
Route: routeName,
StatusCode: c.Writer.Status(),
StartTime: startTime,
EndTime: time.Now(),
})
We also prepared HTTP middlewares for Gin and Beego users.
To get more detailed timing you can wrap important blocks of code into spans. For example, you can create 2 spans sql
and http
to measure timing of specific operations:
trace := &gobrake.RouteTrace{
Method: c.Request.Method,
Route: routeName,
StartTime: time.Now(),
}
trace.StartSpan("sql")
users, err := fetchUser(ctx, userID)
trace.EndSpan("sql")
trace.StartSpan("http")
resp, err := http.Get("http://example.com/")
trace.EndSpan("http")
trace.StatusCode = http.StatusOK
notifier.Routes.Notify(ctx, trace)
You can also collect stats about individual SQL queries performance using following API:
notifier.Queries.Notify(&gobrake.QueryInfo{
Query: "SELECT * FROM users WHERE id = ?", // query must be normalized
Func: "fetchUser", // optional
File: "models/user.go", // optional
Line: 123, // optional
StartTime: startTime,
EndTime: time.Now(),
})