Skip to content

Commit 560584b

Browse files
authored
Log trace ID on failure (#2556)
1 parent 96115f4 commit 560584b

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ Environment Variables:
6161
6262
`
6363

64-
const jfrogAppName = "jf"
64+
const (
65+
jfrogAppName = "jf"
66+
traceIdLogMsg = "Trace ID for JFrog Platform logs:"
67+
)
68+
69+
// Trace ID that is generated for the Uber Trace ID header.
70+
var traceID string
6571

6672
func main() {
6773
log.SetDefaultLogger()
@@ -129,18 +135,20 @@ func execMain() error {
129135
return nil
130136
}
131137
err = app.Run(args)
138+
logTraceIdOnFailure(err)
132139
return err
133140
}
134141

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

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

167+
func logTraceIdOnFailure(err error) {
168+
if err == nil || traceID == "" {
169+
return
170+
}
171+
clientlog.Info(traceIdLogMsg, traceID)
172+
}
173+
159174
// Detects typos and can identify one or more valid commands similar to the error command.
160175
// In Addition, if a subcommand is found with exact match, preferred it over similar commands, for example:
161176
// "jf bp" -> return "jf rt bp"

0 commit comments

Comments
 (0)