Skip to content

Instantly share code, notes, and snippets.

@suzuki-shunsuke
Created August 1, 2020 00:35
Show Gist options
  • Save suzuki-shunsuke/dcd6e93491fd4a64e3d2250aeb6b4c42 to your computer and use it in GitHub Desktop.
Save suzuki-shunsuke/dcd6e93491fd4a64e3d2250aeb6b4c42 to your computer and use it in GitHub Desktop.
Example of sentry-go
package sentry
import (
"time"
"github.com/getsentry/sentry-go"
)
type Event struct {
Error error
Tags map[string]string
Level sentry.Level
Contexts map[string]interface{}
// Message is set as a tag "message_"
Message string
// RequestID is set as a tag "request-id_"
RequestID string
}
type Client struct {
hub *sentry.Hub
}
func New(dsn string) (Client, error) {
hub := sentry.NewHub(nil, sentry.NewScope())
client, err := sentry.NewClient(sentry.ClientOptions{Dsn: dsn})
if err != nil {
return Client{}, err
}
hub.BindClient(client)
return Client{
hub: hub,
}, nil
}
func (cl Client) Flush() {
cl.hub.Flush(5 * time.Second)
}
func (cl Client) Send(ev Event) {
cl.hub.WithScope(func(scope *sentry.Scope) {
scope.SetTags(ev.Tags)
scope.SetContexts(ev.Contexts)
if ev.Level != "" {
scope.SetLevel(ev.Level)
}
if ev.Message != "" {
// suffix "_" is added to avoid the conflict with ev.Tags
scope.SetTag("message_", ev.Message)
}
if ev.RequestID != "" {
// suffix "_" is added to avoid the conflict with ev.Tags
scope.SetTag("request-id_", ev.RequestID)
}
cl.hub.CaptureException(ev.Error)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment