add bugsnag logrus hook

Signed-off-by: Matt Tescher <matthew.tescher@docker.com>
This commit is contained in:
Matt Tescher 2018-10-25 14:38:26 -07:00
parent 93e082742a
commit 7c4d584e58
5 changed files with 156 additions and 13 deletions

21
vendor/github.com/Shopify/logrus-bugsnag/LICENSE generated vendored Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 Shopify
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

24
vendor/github.com/Shopify/logrus-bugsnag/README.md generated vendored Normal file
View file

@ -0,0 +1,24 @@
## logrus-bugsnag
[![Build Status](https://travis-ci.org/Shopify/logrus-bugsnag.svg)](https://travis-ci.org/Shopify/logrus-bugsnag)
logrus-bugsnag is a hook that allows [Logrus](https://github.com/sirupsen/logrus) to interface with [Bugsnag](https://bugsnag.com).
#### Usage
```go
import (
log "github.com/sirupsen/logrus"
"github.com/Shopify/logrus-bugsnag"
bugsnag "github.com/bugsnag/bugsnag-go"
)
func init() {
bugsnag.Configure(bugsnag.Configuration{
APIKey: apiKey,
})
hook, err := logrus_bugsnag.NewBugsnagHook()
logrus.StandardLogger().Hooks.Add(hook)
}
```

81
vendor/github.com/Shopify/logrus-bugsnag/bugsnag.go generated vendored Normal file
View file

@ -0,0 +1,81 @@
package logrus_bugsnag
import (
"errors"
"github.com/sirupsen/logrus"
"github.com/bugsnag/bugsnag-go"
bugsnag_errors "github.com/bugsnag/bugsnag-go/errors"
)
type bugsnagHook struct{}
// ErrBugsnagUnconfigured is returned if NewBugsnagHook is called before
// bugsnag.Configure. Bugsnag must be configured before the hook.
var ErrBugsnagUnconfigured = errors.New("bugsnag must be configured before installing this logrus hook")
// ErrBugsnagSendFailed indicates that the hook failed to submit an error to
// bugsnag. The error was successfully generated, but `bugsnag.Notify()`
// failed.
type ErrBugsnagSendFailed struct {
err error
}
func (e ErrBugsnagSendFailed) Error() string {
return "failed to send error to Bugsnag: " + e.err.Error()
}
// NewBugsnagHook initializes a logrus hook which sends exceptions to an
// exception-tracking service compatible with the Bugsnag API. Before using
// this hook, you must call bugsnag.Configure(). The returned object should be
// registered with a log via `AddHook()`
//
// Entries that trigger an Error, Fatal or Panic should now include an "error"
// field to send to Bugsnag.
func NewBugsnagHook() (*bugsnagHook, error) {
if bugsnag.Config.APIKey == "" {
return nil, ErrBugsnagUnconfigured
}
return &bugsnagHook{}, nil
}
// skipStackFrames skips logrus stack frames before logging to Bugsnag.
const skipStackFrames = 4
// Fire forwards an error to Bugsnag. Given a logrus.Entry, it extracts the
// "error" field (or the Message if the error isn't present) and sends it off.
func (hook *bugsnagHook) Fire(entry *logrus.Entry) error {
var notifyErr error
err, ok := entry.Data["error"].(error)
if ok {
notifyErr = err
} else {
notifyErr = errors.New(entry.Message)
}
metadata := bugsnag.MetaData{}
metadata["metadata"] = make(map[string]interface{})
for key, val := range entry.Data {
if key != "error" {
metadata["metadata"][key] = val
}
}
errWithStack := bugsnag_errors.New(notifyErr, skipStackFrames)
bugsnagErr := bugsnag.Notify(errWithStack, metadata)
if bugsnagErr != nil {
return ErrBugsnagSendFailed{bugsnagErr}
}
return nil
}
// Levels enumerates the log levels on which the error should be forwarded to
// bugsnag: everything at or above the "Error" level.
func (hook *bugsnagHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}