Skip to content

Commit

Permalink
log trace id on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino committed May 22, 2024
1 parent f639b66 commit 5d3cdc9
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ Environment Variables:
`

const jfrogAppName = "jf"
const (
jfrogAppName = "jf"
traceIdLogMsg = "Trace ID for JFrog Platform logs:"
)

// Trace ID that is generated for the Uber Trace ID header.
var traceID string

func main() {
log.SetDefaultLogger()
Expand Down Expand Up @@ -129,18 +135,20 @@ func execMain() error {
return nil
}
err = app.Run(args)
logTraceIdOnFailure(err)
return err
}

// This command generates and sets an Uber Trace ID token which will be attached as a header to every request.
// This allows users to easily identify which logs on the server side are related to the command executed by the CLI.
func setUberTraceIdToken() error {
traceID, err := generateTraceIdToken()
var err error
traceID, err = generateTraceIdToken()
if err != nil {
return err
}
httpclient.SetUberTraceIdToken(traceID)
clientlog.Debug("Trace ID for JFrog Platform logs: ", traceID)
clientlog.Debug(traceIdLogMsg + " " + traceID)
return nil
}

Expand All @@ -156,6 +164,13 @@ func generateTraceIdToken() (string, error) {
return hex.EncodeToString(buf), nil
}

func logTraceIdOnFailure(err error) {
if err == nil || traceID == "" {
return
}
clientlog.Error(traceIdLogMsg + " " + traceID)
}

// Detects typos and can identify one or more valid commands similar to the error command.
// In Addition, if a subcommand is found with exact match, preferred it over similar commands, for example:
// "jf bp" -> return "jf rt bp"
Expand Down

0 comments on commit 5d3cdc9

Please sign in to comment.