gobrake

package module
v3.7.4+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2019 License: BSD-3-Clause Imports: 23 Imported by: 41

README

Airbrake Golang Notifier Build Status

Example

package main

import (
    "errors"

    "github.com/airbrake/gobrake"
)

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)
}

Ignoring notices

airbrake.AddFilter(func(notice *gobrake.Notice) *gobrake.Notice {
    if notice.Context["environment"] == "development" {
        // Ignore notices in development environment.
        return nil
    }
    return notice
})

Setting severity

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.Notice("operation failed", nil, 3)
notice.Context["severity"] = "critical"
airbrake.Notify(notice, nil)

Logging

You can use glog fork to send your logs to Airbrake.

Sending requests stats

In order to collect some basic requests stats you can instrument your application using Notifier.NotifyRequest API:

notifier.NotifyRequest(&gobrake.RequestInfo{
    Method:     "GET",
    Route:      "/hello/:name",
    StatusCode: http.StatusOK,
    Start:      startTime,
    End:        time.Now(),
})

We also prepared HTTP middlewares for Gin and Beego users.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBlacklistKeysFilter

func NewBlacklistKeysFilter(keys ...interface{}) func(*Notice) *Notice
Example
package main

import (
	"regexp"

	"github.com/airbrake/gobrake"
)

func main() {
	notifier := gobrake.NewNotifier(1, "key")
	filter := gobrake.NewBlacklistKeysFilter("password", regexp.MustCompile("(?i)(user)"))
	notifier.AddFilter(filter)

	notice := &gobrake.Notice{
		Params: map[string]interface{}{
			"password": "slds2&LP",
			"User":     "username",
			"email":    "john@example.com",
		},
	}
	notifier.Notify(notice, nil)
}
Output:

func SetLogger

func SetLogger(l *log.Logger)

Types

type Error

type Error struct {
	Type      string       `json:"type"`
	Message   string       `json:"message"`
	Backtrace []StackFrame `json:"backtrace"`
}

type Notice

type Notice struct {
	Id    string `json:"-"` // id returned by SendNotice
	Error error  `json:"-"` // error returned by SendNotice

	Errors  []Error                `json:"errors"`
	Context map[string]interface{} `json:"context"`
	Env     map[string]interface{} `json:"environment"`
	Session map[string]interface{} `json:"session"`
	Params  map[string]interface{} `json:"params"`
}

func NewNotice

func NewNotice(e interface{}, req *http.Request, depth int) *Notice

func (*Notice) SetRequest

func (n *Notice) SetRequest(req *http.Request)

func (*Notice) String

func (n *Notice) String() string

type Notifier

type Notifier struct {
	// contains filtered or unexported fields
}

func NewNotifier

func NewNotifier(projectId int64, projectKey string) *Notifier

func NewNotifierWithOptions

func NewNotifierWithOptions(opt *NotifierOptions) *Notifier

func (*Notifier) AddFilter

func (n *Notifier) AddFilter(fn func(*Notice) *Notice)

AddFilter adds filter that can change notice or ignore it by returning nil.

func (*Notifier) Close

func (n *Notifier) Close() error

func (*Notifier) CloseTimeout

func (n *Notifier) CloseTimeout(timeout time.Duration) error

CloseTimeout waits for pending requests to finish and then closes the notifier.

func (*Notifier) Flush

func (n *Notifier) Flush()

Flush waits for pending requests to finish.

func (*Notifier) Notice

func (n *Notifier) Notice(err interface{}, req *http.Request, depth int) *Notice

Notice returns Aibrake notice created from error and request. depth determines which call frame to use when constructing backtrace.

func (*Notifier) Notify

func (n *Notifier) Notify(e interface{}, req *http.Request)

Notify notifies Airbrake about the error.

func (*Notifier) NotifyOnPanic

func (n *Notifier) NotifyOnPanic()

NotifyOnPanic notifies Airbrake about the panic and should be used with defer statement.

func (*Notifier) NotifyRequest

func (n *Notifier) NotifyRequest(req *RequestInfo) error

NotifyRequest notifies Airbrake about the request.

func (*Notifier) SendNotice

func (n *Notifier) SendNotice(notice *Notice) (string, error)

SendNotice sends notice to Airbrake.

func (*Notifier) SendNoticeAsync

func (n *Notifier) SendNoticeAsync(notice *Notice)

SendNoticeAsync is like SendNotice, but sends notice asynchronously. Pending notices can be flushed with Flush.

type NotifierOptions

type NotifierOptions struct {
	// Airbrake project id.
	ProjectId int64
	// Airbrake project key.
	ProjectKey string
	// Airbrake host name. Default is https://airbrake.io.
	Host string

	// Environment such as production or development.
	Environment string
	// Git revision. Default is SOURCE_VERSION on Heroku.
	Revision string
	// List of keys containing sensitive information that must be filtered out.
	// Default is password, secret.
	KeysBlacklist []interface{}

	// http.Client that is used to interact with Airbrake API.
	HTTPClient *http.Client
}

type RequestInfo

type RequestInfo struct {
	Method     string
	Route      string
	StatusCode int
	Start      time.Time
	End        time.Time
}

type StackFrame

type StackFrame struct {
	File string         `json:"file"`
	Line int            `json:"line"`
	Func string         `json:"function"`
	Code map[int]string `json:"code,omitempty"`
}

Directories

Path Synopsis
examples
gin
internal
negroni module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL