-
Notifications
You must be signed in to change notification settings - Fork 783
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a NATS error handler option when connecting to NATS
When a NATS error occurs, it was not logged using the logrus formatter. This commits fixes it.
- Loading branch information
Showing
7 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package nats | ||
|
||
import ( | ||
"github.com/benthosdev/benthos/v4/internal/log" | ||
"github.com/benthosdev/benthos/v4/public/service" | ||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
func errorHandlerOption(logger *service.Logger) nats.Option { | ||
return nats.ErrorHandler(func(nc *nats.Conn, sub *nats.Subscription, err error) { | ||
if nc != nil { | ||
logger = logger.With("connection-status", nc.Status()) | ||
} | ||
if sub != nil { | ||
logger = logger.With("subject", sub.Subject) | ||
if c, ok := sub.ConsumerInfo(); ok != nil { | ||
logger = logger.With("consumer", c.Name) | ||
} | ||
} | ||
logger.Errorf("nats operation failed: %v\n", err) | ||
}) | ||
} | ||
|
||
func errorHandlerOptionFromModularLogger(logger log.Modular) nats.Option { | ||
return nats.ErrorHandler(func(nc *nats.Conn, sub *nats.Subscription, err error) { | ||
if nc != nil { | ||
logger = logger.With("connection-status", nc.Status()) | ||
} | ||
if sub != nil { | ||
logger = logger.With("subject", sub.Subject) | ||
if c, ok := sub.ConsumerInfo(); ok != nil { | ||
logger = logger.With("consumer", c.Name) | ||
} | ||
} | ||
logger.Errorf("nats operation failed: %v\n", err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters