Skip to content

Instantly share code, notes, and snippets.

@pablodz
Created January 10, 2023 23:21
Show Gist options
  • Save pablodz/279b6c975d93a638e4378b225bbaea39 to your computer and use it in GitHub Desktop.
Save pablodz/279b6c975d93a638e4378b225bbaea39 to your computer and use it in GitHub Desktop.
Send a sentry error with custom fields simplistic way
package logger
import (
"fmt"
"log"
"os"
"time"
"github.com/getsentry/sentry-go"
)
var CUSTOM_SENTRY_DSN = ""
func SetDSN(dsn string) {
CUSTOM_SENTRY_DSN = dsn
}
func SendCustomSentryError(title string, message string, env string, tags map[string]string) error {
log.Println("Sending error to Sentry: ", title, message, tags)
// Create the Sentry client and scope
client, err := sentry.NewClient(sentry.ClientOptions{
Dsn: CUSTOM_SENTRY_DSN,
Environment: env,
AttachStacktrace: true,
ServerName: os.Getenv("HOSTNAME"),
MaxErrorDepth: 10,
})
if err != nil {
return fmt.Errorf("error creating Sentry client: %v", err)
}
scope := sentry.NewScope()
// Configure the scope with the provided tags and event processor
tagsFunc := func(scope *sentry.Scope) {
scope.AddEventProcessor(func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
event.Exception[0].Type = title
event.Exception[0].Value = message
return event
})
for key, value := range tags {
scope.SetTag(key, value)
}
}
// Capture the exception and return any errors
hub := sentry.NewHub(client, scope)
hub.ConfigureScope(tagsFunc)
hub.CaptureException(err)
hub.Flush(time.Second * 5)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment