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

feat: add trace for executables #744

Merged
merged 7 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion cmd/notation/cert/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Example - List all certificate files from trust store "wabbit-networks" of type

func listCerts(ctx context.Context, opts *certListOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
ctx = opts.LoggingFlagOpts.InitializeLogger(ctx)
logger := log.GetLogger(ctx)

namedStore := opts.namedStore
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/cert/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Example - Show details of certificate "cert2.pem" with type "signingAuthority" f

func showCerts(ctx context.Context, opts *certShowOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
ctx = opts.LoggingFlagOpts.InitializeLogger(ctx)
logger := log.GetLogger(ctx)

storeType := opts.storeType
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Example - [Experimental] Inspect signatures on an OCI artifact identified by a d

func runInspect(command *cobra.Command, opts *inspectOpts) error {
// set log level
ctx := opts.LoggingFlagOpts.SetLoggerLevel(command.Context())
ctx := opts.LoggingFlagOpts.InitializeLogger(command.Context())

if opts.outputFormat != cmd.OutputJSON && opts.outputFormat != cmd.OutputPlaintext {
return fmt.Errorf("unrecognized output format %s", opts.outputFormat)
Expand Down
6 changes: 3 additions & 3 deletions cmd/notation/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func keyDeleteCommand(opts *keyDeleteOpts) *cobra.Command {

func addKey(ctx context.Context, opts *keyAddOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
ctx = opts.LoggingFlagOpts.InitializeLogger(ctx)

pluginConfig, err := cmd.ParseFlagMap(opts.pluginConfig, cmd.PflagPluginConfig.Name)
if err != nil {
Expand All @@ -188,7 +188,7 @@ func addKey(ctx context.Context, opts *keyAddOpts) error {

func updateKey(ctx context.Context, opts *keyUpdateOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
ctx = opts.LoggingFlagOpts.InitializeLogger(ctx)
logger := log.GetLogger(ctx)

if !opts.isDefault {
Expand Down Expand Up @@ -222,7 +222,7 @@ func listKeys() error {

func deleteKeys(ctx context.Context, opts *keyDeleteOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
ctx = opts.LoggingFlagOpts.InitializeLogger(ctx)
logger := log.GetLogger(ctx)

// core process
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func listCommand(opts *listOpts) *cobra.Command {

func runList(ctx context.Context, opts *listOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
ctx = opts.LoggingFlagOpts.InitializeLogger(ctx)

// initialize
reference := opts.reference
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Example - Login using $NOTATION_USERNAME $NOTATION_PASSWORD variables:

func runLogin(ctx context.Context, opts *loginOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
ctx = opts.LoggingFlagOpts.InitializeLogger(ctx)

// initialize
serverAddress := opts.server
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func logoutCommand(opts *logoutOpts) *cobra.Command {

func runLogout(ctx context.Context, opts *logoutOpts) error {
// set log level
ctx = opts.LoggingFlagOpts.SetLoggerLevel(ctx)
ctx = opts.LoggingFlagOpts.InitializeLogger(ctx)
credsStore, err := auth.NewCredentialsStore()
if err != nil {
return fmt.Errorf("failed to get credentials store: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Example - [Experimental] Sign an OCI artifact identified by a tag and referenced

func runSign(command *cobra.Command, cmdOpts *signOpts) error {
// set log level
ctx := cmdOpts.LoggingFlagOpts.SetLoggerLevel(command.Context())
ctx := cmdOpts.LoggingFlagOpts.InitializeLogger(command.Context())

// initialize
signer, err := cmd.GetSigner(ctx, &cmdOpts.SignerFlagOpts)
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Example - [Experimental] Verify a signature on an OCI artifact identified by a t

func runVerify(command *cobra.Command, opts *verifyOpts) error {
// set log level
ctx := opts.LoggingFlagOpts.SetLoggerLevel(command.Context())
ctx := opts.LoggingFlagOpts.InitializeLogger(command.Context())

// initialize
sigVerifier, err := verifier.NewFromConfig()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/notaryproject/notation-go v1.0.0-rc.6
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc4
github.com/oras-project/oras-credentials-go v0.2.0
github.com/oras-project/oras-credentials-go v0.3.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc4 h1:oOxKUJWnFC4YGHCCMNql1x4YaDfYBTS5Y4x/Cgeo1E0=
github.com/opencontainers/image-spec v1.1.0-rc4/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8=
github.com/oras-project/oras-credentials-go v0.2.0 h1:BvWAXo0e5unWR6Hfxyb0K04mHNHreQz/Zclw6IzCYJo=
github.com/oras-project/oras-credentials-go v0.2.0/go.mod h1:JVdg7a5k7hzTrEeeouwag0aCv7OLrS77r7/6w3gVirU=
github.com/oras-project/oras-credentials-go v0.3.0 h1:Bg1d9iAmgo50RlaIy2XI5MQs7qL00DB3R9Q4JRP1VWs=
github.com/oras-project/oras-credentials-go v0.3.0/go.mod h1:fFCebDQo0Do+gnM96uV9YUnRay0pwuRQupypvofsp4s=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
30 changes: 26 additions & 4 deletions internal/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cmd
import (
"context"

"github.com/notaryproject/notation-go/log"
"github.com/notaryproject/notation/internal/trace"
credentialstrace "github.com/oras-project/oras-credentials-go/trace"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -41,12 +43,32 @@ func (opts *LoggingFlagOpts) ApplyFlags(fs *pflag.FlagSet) {
fs.BoolVarP(&opts.Verbose, "verbose", "v", false, "verbose mode")
}

// SetLoggerLevel sets up the logger based on common options.
func (opts *LoggingFlagOpts) SetLoggerLevel(ctx context.Context) context.Context {
// InitializeLogger sets up the logger based on common options.
func (opts *LoggingFlagOpts) InitializeLogger(ctx context.Context) context.Context {
if opts.Debug {
return trace.WithLoggerLevel(ctx, logrus.DebugLevel)
ctx = trace.WithLoggerLevel(ctx, logrus.DebugLevel)
} else if opts.Verbose {
return trace.WithLoggerLevel(ctx, logrus.InfoLevel)
ctx = trace.WithLoggerLevel(ctx, logrus.InfoLevel)
} else {
return ctx
}
return withExecutableTrace(ctx)
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
}

// withExecutableTrace adds tracing for credential helper executables.
func withExecutableTrace(ctx context.Context) context.Context {
logger := log.GetLogger(ctx)
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
ctx = credentialstrace.WithExecutableTrace(ctx, &credentialstrace.ExecutableTrace{
ExecuteStart: func(executableName, action string) {
logger.Debugf("started executing credential helper program %s with action %s", executableName, action)
},
ExecuteDone: func(executableName, action string, err error) {
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.Errorf("finished executing credential helper program %s with action %s and got error %w", executableName, action, err)
} else {
logger.Debugf("finished executing credential helper program %s with action %s", executableName, action)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
logger.Debugf("finished executing credential helper program %s with action %s", executableName, action)
logger.Debugf("successfully finished executing credential helper program %s with action %s", executableName, action)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed accordingly

}
},
})
return ctx
}
Loading