Created
July 3, 2019 02:08
-
-
Save rhaseven7h/0e0bff9b4d13e6c530c5af2cec6cc529 to your computer and use it in GitHub Desktop.
Sentry Raven Go (Golang) Usage, along with custom Type and Value (Exception Title and Message)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"time" | |
"github.com/brianvoe/gofakeit" | |
"github.com/getsentry/raven-go" | |
"github.com/pkg/errors" | |
"github.com/sirupsen/logrus" | |
) | |
const ( | |
DSN = "https://435780e423564ec0bf71911ef5e0eae2:[email protected]/2" | |
) | |
func init() { | |
gofakeit.Seed(time.Now().UnixNano()) | |
err := raven.SetDSN(DSN) | |
if err != nil { | |
panic(err) | |
} | |
raven.SetEnvironment("development") | |
raven.SetRelease("v0.0.1") | |
} | |
func c() { | |
sentryTimestamp := time.Now().Format(time.RFC3339Nano) | |
sentryError := errors.Wrapf( | |
errors.New("my fantastic base error"), | |
"%s's %s error", | |
gofakeit.HackerNoun(), | |
gofakeit.HackerIngverb(), | |
) | |
sentryTags := map[string]string{ | |
"error_stamp": sentryTimestamp, | |
"user_name": gofakeit.Name(), | |
"user_city": gofakeit.City(), | |
"beer_name": gofakeit.BeerName(), | |
"car_maker": gofakeit.CarMaker(), | |
"car_model": gofakeit.CarModel(), | |
} | |
logrusFields := logrus.Fields{} | |
for key, value := range sentryTags { | |
logrusFields[key] = value | |
} | |
eventID := raven.CaptureErrorAndWait(sentryError, nil) | |
logrus. | |
WithField("event_id", eventID). | |
Info("raven output") | |
logrus. | |
WithFields(logrusFields). | |
WithError(sentryError). | |
Error(sentryError) | |
os.Exit(128) | |
} | |
func SentryError(title string, message string, err error, tags map[string]string) { | |
exception := raven.NewException( | |
err, | |
raven.NewStacktrace(1, 5, nil), | |
) | |
exception.Type = title | |
exception.Value = message | |
packet := raven.NewPacket( | |
message, | |
exception, | |
) | |
raven.Capture(packet, tags) | |
raven.Wait() | |
} | |
func c1() { | |
logrus.Info("On c1") | |
SentryError( | |
"My error here", | |
"This is a message", | |
errors.New("My real error message"), | |
map[string]string{ | |
"alpha": "42", | |
}, | |
) | |
logrus.Info("done.") | |
} | |
func b() { | |
logrus.Info("On b") | |
c1() | |
} | |
func a() { | |
logrus.Info("On a") | |
b() | |
} | |
type BasicError struct { | |
} | |
func main() { | |
fmt.Println("Hello World!") | |
a() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment