Skip to content

Commit

Permalink
chore: add capture exceptions in sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Aug 1, 2024
1 parent 028f79f commit b1f0db3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 6 additions & 4 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ func main() {
logrus.Fatalf("Failed to initialize service: %v", err)
}

e := echo.New()
e.HideBanner = true
echologrus.Logger = svc.Logger
e.Use(echologrus.Middleware())

if svc.Cfg.SentryDSN != "" {
if err = sentry.Init(sentry.ClientOptions{
Dsn: svc.Cfg.SentryDSN,
}); err != nil {
logrus.Error(err)
svc.Logger.WithError(err).Error("Failed to init Sentry")
}
}

echologrus.Logger = svc.Logger
e := echo.New()
if svc.Cfg.DatadogAgentUrl != "" {
tracer.Start(tracer.WithService("http-nostr"))
defer tracer.Stop()
Expand All @@ -47,7 +50,6 @@ func main() {
e.POST("/publish", svc.PublishHandler)
e.POST("/subscriptions", svc.SubscriptionHandler)
e.DELETE("/subscriptions/:id", svc.StopSubscriptionHandler)
e.Use(echologrus.Middleware())

//start Echo server
go func() {
Expand Down
16 changes: 12 additions & 4 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"
"time"

"github.com/getsentry/sentry-go"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
Expand Down Expand Up @@ -109,6 +110,7 @@ func NewService(ctx context.Context) (*Service, error) {
logger.Info("Connecting to the relay...")
relay, err := nostr.RelayConnect(ctx, cfg.DefaultRelayURL)
if err != nil {
sentry.CaptureException(err)
logger.WithError(err).Error("Failed to connect to default relay")
return nil, err
}
Expand All @@ -130,6 +132,7 @@ func NewService(ctx context.Context) (*Service, error) {

var openSubscriptions []Subscription
if err := svc.db.Where("open = ?", true).Find(&openSubscriptions).Error; err != nil {
sentry.CaptureException(err)
logger.WithError(err).Error("Failed to query open subscriptions")
return nil, err
}
Expand Down Expand Up @@ -265,6 +268,7 @@ func (svc *Service) PublishHandler(c echo.Context) error {

err = relay.Publish(ctx, *requestData.SignedEvent)
if err != nil {
sentry.CaptureException(err)
svc.Logger.WithError(err).WithFields(logrus.Fields{
"event_id": requestData.SignedEvent.ID,
"relay_url": requestData.RelayUrl,
Expand Down Expand Up @@ -337,6 +341,7 @@ func (svc *Service) NIP47Handler(c echo.Context) error {
}

if err := svc.db.Create(&requestEvent).Error; err != nil {
sentry.CaptureException(err)
svc.Logger.WithError(err).WithFields(logrus.Fields{
"request_event_id": requestData.SignedEvent.ID,
"client_pubkey": requestData.SignedEvent.PubKey,
Expand Down Expand Up @@ -437,6 +442,7 @@ func (svc *Service) NIP47WebhookHandler(c echo.Context) error {
}

if err := svc.db.Create(&requestEvent).Error; err != nil {
sentry.CaptureException(err)
svc.Logger.WithError(err).WithFields(logrus.Fields{
"request_event_id": requestData.SignedEvent.ID,
"wallet_pubkey": requestData.WalletPubkey,
Expand Down Expand Up @@ -530,6 +536,7 @@ func (svc *Service) NIP47NotificationHandler(c echo.Context) error {
err := svc.db.Create(&subscription).Error

if err != nil {
sentry.CaptureException(err)
svc.Logger.WithError(err).WithFields(logrus.Fields{
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
Expand Down Expand Up @@ -764,6 +771,7 @@ func (svc *Service) publishRequestEvent(ctx context.Context, subscription *Subsc
err := sub.Relay.Publish(ctx, *subscription.RequestEvent.SignedEvent)
if err != nil {
// TODO: notify user about publish failure
sentry.CaptureException(err)
svc.Logger.WithError(err).WithFields(logrus.Fields{
"request_event_id": subscription.RequestEvent.NostrId,
"relay_url": subscription.RelayUrl,
Expand Down Expand Up @@ -792,10 +800,10 @@ func (svc *Service) handleResponseEvent(event *nostr.Event, subscription *Subscr
"client_pubkey": clientPubkey,
"relay_url": subscription.RelayUrl,
}).Info("Received response event")
if (subscription.RequestEvent != nil) {
subscription.RequestEvent.ResponseReceivedAt = time.Now()
svc.db.Save(&subscription.RequestEvent)
}

subscription.RequestEvent.ResponseReceivedAt = time.Now()
svc.db.Save(&subscription.RequestEvent)

responseEvent := ResponseEvent{
NostrId: event.ID,
Content: event.Content,
Expand Down

0 comments on commit b1f0db3

Please sign in to comment.