Skip to content

Commit

Permalink
chore: improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jul 30, 2024
1 parent da3b990 commit db0fc6b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
}()
//handle graceful shutdown
<-svc.Ctx.Done()
svc.Logger.Infof("Shutting down echo server...")
svc.Logger.Info("Shutting down echo server...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
e.Shutdown(ctx)
Expand Down
41 changes: 23 additions & 18 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (svc *Service) InfoHandler(c echo.Context) error {
svc.Logger.WithFields(logrus.Fields{
"relay_url": requestData.RelayUrl,
"wallet_pubkey": requestData.WalletPubkey,
}).Info("Subscribing to info event")
}).Debug("Subscribing to info event")

filter := nostr.Filter{
Authors: []string{requestData.WalletPubkey},
Expand All @@ -205,7 +205,7 @@ func (svc *Service) InfoHandler(c echo.Context) error {
svc.Logger.WithFields(logrus.Fields{
"relay_url": requestData.RelayUrl,
"wallet_pubkey": requestData.WalletPubkey,
}).Info("Exiting info subscription without receiving")
}).Error("Exiting info subscription without receiving event")
return c.JSON(http.StatusRequestTimeout, ErrorResponse{
Message: "Request canceled or timed out",
Error: ctx.Err().Error(),
Expand Down Expand Up @@ -261,7 +261,7 @@ func (svc *Service) PublishHandler(c echo.Context) error {
svc.Logger.WithFields(logrus.Fields{
"event_id": requestData.SignedEvent.ID,
"relay_url": requestData.RelayUrl,
}).Info("Publishing event")
}).Debug("Publishing event")

err = relay.Publish(ctx, *requestData.SignedEvent)
if err != nil {
Expand All @@ -277,7 +277,7 @@ func (svc *Service) PublishHandler(c echo.Context) error {
}

svc.Logger.WithFields(logrus.Fields{
"request_event_id": requestData.SignedEvent.ID,
"event_id": requestData.SignedEvent.ID,
"relay_url": requestData.RelayUrl,
}).Info("Published event")

Expand Down Expand Up @@ -353,7 +353,7 @@ func (svc *Service) NIP47Handler(c echo.Context) error {

ctx, cancel := context.WithTimeout(c.Request().Context(), 90*time.Second)
defer cancel()
go svc.startSubscription(ctx, &subscription, svc.publishEvent, svc.handleResponseEvent)
go svc.startSubscription(ctx, &subscription, svc.publishRequestEvent, svc.handleResponseEvent)

select {
case <-ctx.Done():
Expand Down Expand Up @@ -452,7 +452,7 @@ func (svc *Service) NIP47WebhookHandler(c echo.Context) error {
ctx, cancel := context.WithTimeout(svc.Ctx, 90*time.Second)
defer cancel()

go svc.startSubscription(ctx, &subscription, svc.publishEvent, svc.handleResponseEvent)
go svc.startSubscription(ctx, &subscription, svc.publishRequestEvent, svc.handleResponseEvent)
return c.JSON(http.StatusOK, NIP47Response{
State: WEBHOOK_RECEIVED,
})
Expand Down Expand Up @@ -510,7 +510,7 @@ func (svc *Service) NIP47NotificationHandler(c echo.Context) error {
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
"webhook_url": requestData.WebhookUrl,
}).Info("Subscribing to notifications")
}).Debug("Subscribing to notifications")

subscription := Subscription{
RelayUrl: requestData.RelayUrl,
Expand Down Expand Up @@ -647,7 +647,7 @@ func (svc *Service) StopSubscriptionHandler(c echo.Context) error {

svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
}).Debug("Stopped subscription")
}).Info("Stopped subscription")

return c.JSON(http.StatusOK, StopSubscriptionResponse{
Message: "Subscription stopped successfully",
Expand All @@ -674,6 +674,7 @@ func (svc *Service) stopSubscription(subscription *Subscription) error {
func (svc *Service) startSubscription(ctx context.Context, subscription *Subscription, onReceiveEOS OnReceiveEOSFunc, handleEvent HandleEventFunc) {
svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Debug("Starting subscription")

Expand Down Expand Up @@ -720,6 +721,7 @@ func (svc *Service) startSubscription(ctx context.Context, subscription *Subscri

svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Debug("Started subscription")

Expand Down Expand Up @@ -758,24 +760,24 @@ func (svc *Service) startSubscription(ctx context.Context, subscription *Subscri
}
}

func (svc *Service) publishEvent(ctx context.Context, subscription *Subscription) {
func (svc *Service) publishRequestEvent(ctx context.Context, subscription *Subscription) {
svc.subscriptionsMutex.Lock()
sub := svc.subscriptions[subscription.Uuid]
svc.subscriptionsMutex.Unlock()
err := sub.Relay.Publish(ctx, *subscription.RequestEvent)
if err != nil {
// TODO: notify user about publish failure
svc.Logger.WithError(err).WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
"request_event_id": subscription.RequestEvent.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Error("Failed to publish to relay")
sub.Unsub()
} else {
svc.Logger.WithFields(logrus.Fields{
"publish_status": REQUEST_EVENT_PUBLISH_CONFIRMED,
"event_id": subscription.RequestEvent.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
"request_event_id": subscription.RequestEvent.ID,
"relay_url": subscription.RelayUrl,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Published request event successfully")
subscription.RequestEventDB.State = REQUEST_EVENT_PUBLISH_CONFIRMED
}
Expand Down Expand Up @@ -811,7 +813,8 @@ func (svc *Service) handleSubscribedEvent(event *nostr.Event, subscription *Subs
"event_kind": event.Kind,
"subscription_id": subscription.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Received event")
"relay_url": subscription.RelayUrl,
}).Info("Received subscribed event")
responseEvent := ResponseEvent{
NostrId: event.ID,
Content: event.Content,
Expand All @@ -833,6 +836,7 @@ func (svc *Service) processEvents(ctx context.Context, subscription *Subscriptio
svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
"relay_url": subscription.RelayUrl,
}).Debug("Received EOS")

if (onReceiveEOS != nil) {
Expand All @@ -847,6 +851,7 @@ func (svc *Service) processEvents(ctx context.Context, subscription *Subscriptio
svc.Logger.WithFields(logrus.Fields{
"subscription_id": subscription.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
"relay_url": subscription.RelayUrl,
}).Debug("Relay subscription events channel ended")
}()

Expand All @@ -864,7 +869,7 @@ func (svc *Service) getRelayConnection(ctx context.Context, customRelayURL strin
if customRelayURL != "" && customRelayURL != svc.Cfg.DefaultRelayURL {
svc.Logger.WithFields(logrus.Fields{
"custom_relay_url": customRelayURL,
}).Infof("Connecting to custom relay")
}).Info("Connecting to custom relay")
relay, err := nostr.RelayConnect(ctx, customRelayURL)
return relay, true, err // true means custom and the relay should be closed
}
Expand Down Expand Up @@ -909,7 +914,7 @@ func (svc *Service) postEventToWebhook(event *nostr.Event, webhookURL string) {
"event_id": event.ID,
"event_kind": event.Kind,
"webhook_url": webhookURL,
}).Infof("Successfully posted event to webhook")
}).Info("Successfully posted event to webhook")
}

func (svc *Service) subscriptionToFilter(subscription *Subscription) (*nostr.Filter){
Expand Down

0 comments on commit db0fc6b

Please sign in to comment.