Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLOB-1001] Add custom error value formatter to zerolog logging library #31

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
cmtcfg "github.com/cometbft/cometbft/config"
dbm "github.com/cosmos/cosmos-db"
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -29,6 +30,7 @@ import (
"cosmossdk.io/store/snapshots"
snapshottypes "cosmossdk.io/store/snapshots/types"
storetypes "cosmossdk.io/store/types"
errorspkg "github.com/pkg/errors"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -110,7 +112,7 @@ func InterceptConfigsPreRunHandler(cmd *cobra.Command, customAppConfigTemplate s
if err != nil {
return err
}
serverCtx.Logger = logger.With(log.ModuleKey, "server")
serverCtx.Logger = logger.With(log.ModuleKey, "server").With("source", "go")

// set server context
return SetCmdServerContext(cmd, serverCtx)
Expand Down Expand Up @@ -197,9 +199,39 @@ func CreateSDKLogger(ctx *Context, out io.Writer) (log.Logger, error) {
// Check if the CometBFT flag for trace logging is set and enable stack traces if so.
opts = append(opts, log.TraceOption(ctx.Viper.GetBool("trace"))) // cmtcli.TraceFlag

// Error fields should be set under error object
zerolog.ErrorFieldName = "error"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default in the library so we don't need to do it.
https://pkg.go.dev/github.com/rs/zerolog#section-readme


// Add the kind and message field
zerolog.ErrorMarshalFunc = func(err error) interface{} {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should guard this behind a flag to allow people to opt out of the custom marshalling function in case they aren't using datadog.

stackArr, ok := pkgerrors.MarshalStack(errorspkg.WithStack(err)).([]map[string]string)
if !ok {
return struct{}{}
}
objectToReturn := DatadogErrorTrackingObject{
// Discard common prefix stack traces from zerolog call sites
Stack: stackArr[5:],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a test for this to ensure that we are discarding exactly how much we want in case the stack depth changes due to a change in the implementation of this code or the underlying library.

Kind: "Exception",
Message: err.Error(),
}
return objectToReturn
}

return log.NewLogger(out, opts...), nil
}

type DatadogErrorTrackingObject struct {
Stack []map[string]string
Message string
Kind string
}

func (obj DatadogErrorTrackingObject) MarshalZerologObject(e *zerolog.Event) {
e.Interface("stack", obj.Stack).
Str("message", obj.Message).
Str("kind", obj.Kind)
}

// GetServerContextFromCmd returns a Context from a command or an empty Context
// if it has not been set.
func GetServerContextFromCmd(cmd *cobra.Command) *Context {
Expand Down
Loading