-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* inceased outbox VARCHAR column length to 2048 (#155) * added reply to initiator functionality to sagas (#157) * added generic handler metrics with message type as the label (#144) * added generic handler metrics with message type as the label * add handler name label to the metrics * adding new metrics to the read me * Fix handle empty body (#156) * set the correct Type and Content-Type headers on out going messages (#160) * set the correct Type and Content-Type headers on out going messages * refactoring * fixing ReplyToInitiator not working when initiator sends a message via the RPC interface (#163) * fixing ReplyToInitiator not working when initiator sends a message via the RPC interface * Improved wording of saga documentation article (#164) * better wording for documentation * added golangcli lint configuration and fixed linting failures (#165) * fixed logging issues (#167) * allow getting the saga id of the current invoked saga (#168) * setting the target saga id on the saga correlation id field (#171) * support emperror (#174) * setting the target saga id on the saga correlation id field * added emperror support * Fix logging and added logging documentation (#176) * fixed logging issues and added documentation logging via the invocation interface was broken and did not add contextual data related to the invocation due to a bug in the way the Glogged structure is currently implemented. Also added documentation on how logging should be done within a handler including adding context to returned errors so that data gets logged * added missing documentation file * added documentation on serialization support (#177) * fixed emperror url format * added serialization documentation
- Loading branch information
Guy Baron
authored
Sep 28, 2019
1 parent
d13f2c2
commit 900f24f
Showing
34 changed files
with
950 additions
and
139 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 @@ | ||
linters-settings: | ||
golint: | ||
# minimal confidence for issues, default is 0.8 | ||
min-confidence: 0.8 | ||
gocyclo: | ||
min-complexity: 15 | ||
|
||
govet: | ||
# report about shadowed variables | ||
check-shadowing: false | ||
|
||
# settings per analyzer | ||
settings: | ||
printf: # analyzer name, run `go tool vet help` to see all analyzers | ||
funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer | ||
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof | ||
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf | ||
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf | ||
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf | ||
linters: | ||
disable-all: true | ||
enable: | ||
- deadcode | ||
# - errcheck | ||
- gosimple | ||
- govet | ||
- ineffassign | ||
# - staticcheck | ||
# - typecheck | ||
- unused | ||
# - varcheck | ||
# - deadcode | ||
# - dupl | ||
# - gocritic | ||
- gocyclo | ||
- golint | ||
- misspell |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,100 @@ | ||
# Logging | ||
|
||
### Logging within a handler | ||
|
||
grabbit supports structured logging via the [logrus](https://github.com/sirupsen/logrus) logging package. | ||
The logger is accessible to message handlers via the past in invocation instance. | ||
When logging via the logger exposed on the passed invocation each entry you log will be | ||
annotated with the following contextual data (added as logrus fields to the log entry) | ||
allowing for a better debugging experience. | ||
|
||
- _service: the service name | ||
- handler_name: the name of the handler being invoked | ||
- message_id: the id of the processed message | ||
- message_name: the type of the message that is being processed | ||
- routing_key: the routing_key of the message | ||
- saga_id: the id of the saga instance being invoked | ||
- saga_def: the type of the saga that is being invoked | ||
|
||
```go | ||
|
||
func SomeHandler(invocation gbus.Invocation, message *gbus.BusMessage) error{ | ||
invocation.Log().WithField("name", "rhinof").Info("handler invoked") | ||
invocation.Log().WithField("name", "rhinof").Info("performing some business logic") | ||
return nil | ||
} | ||
|
||
``` | ||
### Logging contextual data when a handler errors | ||
|
||
In cases a message handler errors it is common to log custom contextual data allowing | ||
service developers to diagnose the root cause of the error. | ||
|
||
```go | ||
package my_handlers | ||
|
||
import ( | ||
"gitub.com/wework/grabbit/gbus" | ||
) | ||
|
||
func SomeHandler(invocation gbus.Invocation, message *gbus.BusMessage) error{ | ||
invocation.Log().WithField("name", "rhinof").Info("performing some business logic") | ||
PlaceOrderCommand := message.Payload.(*PlaceOrderCommand) | ||
e := placeOrder(PlaceOrderCommand.CustomerID, PlaceOrderCommand.LineItems) | ||
if e != nil{ | ||
invocation.Log(). | ||
WithField("customer_id", PlaceOrderCommand.CustomerID). | ||
Error("failed placing order for customer") | ||
return e | ||
} | ||
return nil | ||
} | ||
``` | ||
grabbit makes it easier handling these cases and reduce the repetitive task of logging | ||
these custom contextual attributes in cases of errors by integrating the [emperror errors package](https://github.com/emperror/errors). | ||
emperror drop-in replacement for the default errors package allows developers to add the needed contextual data on the error instance and have graabit log the error with all contextual attribute. | ||
|
||
```go | ||
package my_handlers | ||
|
||
import ( | ||
"emperror.dev/errors" | ||
"gitub.com/wework/grabbit/gbus" | ||
) | ||
|
||
func SomeHandler(invocation gbus.Invocation, message *gbus.BusMessage) error{ | ||
invocation.Log().WithField("name", "rhinof").Info("performing some business logic") | ||
PlaceOrderCommand := message.Payload.(*PlaceOrderCommand) | ||
return placeOrder(PlaceOrderCommand.CustomerID, PlaceOrderCommand.LineItems) | ||
} | ||
|
||
func placeOrder(customerID string, lineItems LineItems[]) error{ | ||
|
||
if(someErrorCondition()){ | ||
return errors.WithDetails("failed placing order for customer", "customer_id", customerID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
``` | ||
|
||
|
||
|
||
### Setting a custom logger instance | ||
|
||
grabbit will create a default instance of logrus FieldLogger if no such logger is set when the bus is created. | ||
In order to set a custom logger when creating the bus you need to call the Builder.WithLogger method passing it | ||
To set a custom logger when creating the bus you need to call the Builder.WithLogger method passing it | ||
a logrus.FieldLogger instance. | ||
|
||
```go | ||
|
||
logger := logrus.New().WithField("my_custom_key", "my_custom_value") | ||
bus := builder. | ||
New(). | ||
Bus("rabbitmq connection string"). | ||
WithLogger(logger). | ||
WorkerNum(3, 1). | ||
WithConfirms(). | ||
Txnl("mysql", ""). | ||
Build("your service name") | ||
|
||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Serialization | ||
|
||
grabbit supports out of the box three serializers to format messages over the wire | ||
|
||
- gob (defualt) | ||
- protobuf | ||
- avro (experimental) | ||
|
||
To configure a bus to work with a different serializer than the default one you must call the WithSerializer function call on the builder interface passing it an instance of a gbus.Serializer. | ||
|
||
The following example configures the bus to work with the protobuf serializer | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"github.com/wework/grabbit/gbus" | ||
"github.com/wework/grabbit/gbus/builder" | ||
"github.com/wework/grabbit/gbus/serialization" | ||
) | ||
|
||
logger := logrus.New() | ||
bus := builder. | ||
New(). | ||
Bus("rabbitmq connection string"). | ||
WithLogger(logger). | ||
WithSerializer(serialization.NewProtoSerializer(logger)). | ||
WorkerNum(3, 1). | ||
WithConfirms(). | ||
Txnl("mysql", "database connection string"). | ||
Build("your service name") | ||
|
||
``` | ||
|
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
Oops, something went wrong.