Capture Sentry exceptions for errors in JetStreamConsumer

This commit is contained in:
Neil Alexander 2022-03-07 16:40:56 +00:00
parent 9fbaa1194b
commit 626d3f6cf5
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/getsentry/sentry-go"
"github.com/nats-io/nats.go" "github.com/nats-io/nats.go"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -29,6 +30,7 @@ func JetStreamConsumer(
name := durable + "Pull" name := durable + "Pull"
sub, err := js.PullSubscribe(subj, name, opts...) sub, err := js.PullSubscribe(subj, name, opts...)
if err != nil { if err != nil {
sentry.CaptureException(err)
return fmt.Errorf("nats.SubscribeSync: %w", err) return fmt.Errorf("nats.SubscribeSync: %w", err)
} }
go func() { go func() {
@ -55,6 +57,7 @@ func JetStreamConsumer(
} }
} else { } else {
// Something else went wrong, so we'll panic. // Something else went wrong, so we'll panic.
sentry.CaptureException(err)
logrus.WithContext(ctx).WithField("subject", subj).Fatal(err) logrus.WithContext(ctx).WithField("subject", subj).Fatal(err)
} }
} }
@ -64,15 +67,18 @@ func JetStreamConsumer(
msg := msgs[0] msg := msgs[0]
if err = msg.InProgress(); err != nil { if err = msg.InProgress(); err != nil {
logrus.WithContext(ctx).WithField("subject", subj).Warn(fmt.Errorf("msg.InProgress: %w", err)) logrus.WithContext(ctx).WithField("subject", subj).Warn(fmt.Errorf("msg.InProgress: %w", err))
sentry.CaptureException(err)
continue continue
} }
if f(ctx, msg) { if f(ctx, msg) {
if err = msg.Ack(); err != nil { if err = msg.Ack(); err != nil {
logrus.WithContext(ctx).WithField("subject", subj).Warn(fmt.Errorf("msg.Ack: %w", err)) logrus.WithContext(ctx).WithField("subject", subj).Warn(fmt.Errorf("msg.Ack: %w", err))
sentry.CaptureException(err)
} }
} else { } else {
if err = msg.Nak(); err != nil { if err = msg.Nak(); err != nil {
logrus.WithContext(ctx).WithField("subject", subj).Warn(fmt.Errorf("msg.Nak: %w", err)) logrus.WithContext(ctx).WithField("subject", subj).Warn(fmt.Errorf("msg.Nak: %w", err))
sentry.CaptureException(err)
} }
} }
} }