Skip to content

Commit

Permalink
Support tracing all requests by a command (#2518)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino committed Apr 30, 2024
1 parent 3f177a5 commit b6c104e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ require (

replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240424135031-dac0f92a2aae

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240424133643-5bf715f66eac
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240430132902-9d4dfaf05041

// replace github.com/jfrog/jfrog-cli-security => github.com/jfrog/jfrog-cli-security v1.0.6-0.20240408061620-c9b84da33d5e

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240424135031-dac0f92a2aae h1:Lgn3
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240424135031-dac0f92a2aae/go.mod h1:/rNY4nIB8hIsL+MLGrZOgOYpY4QnJ+CHeTHPQDdGmOk=
github.com/jfrog/jfrog-cli-security v1.1.0 h1:ifCjFJSa1D1pWyW/ADYPqnMkOddzkAT/WY4vHAufn1g=
github.com/jfrog/jfrog-cli-security v1.1.0/go.mod h1:086t7e/einVAGfBXxRdEGDKovWt67I6SqUb1rcpdiZc=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240424133643-5bf715f66eac h1:e/QfkFN/Qtkn1rBg/p7JCOMFLmE2EgfPBx57m2yIF1k=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240424133643-5bf715f66eac/go.mod h1:FprEW0Sqhj6ZSFTFk9NCni+ovFAYMA3zCBmNX4hGXgQ=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240430132902-9d4dfaf05041 h1:cDTm4RxkI+RA8aNmdfM2BVxb5iCoXjLjhb+M+ReEIrE=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240430132902-9d4dfaf05041/go.mod h1:FprEW0Sqhj6ZSFTFk9NCni+ovFAYMA3zCBmNX4hGXgQ=
github.com/jszwec/csvutil v1.10.0 h1:upMDUxhQKqZ5ZDCs/wy+8Kib8rZR8I8lOR34yJkdqhI=
github.com/jszwec/csvutil v1.10.0/go.mod h1:/E4ONrmGkwmWsk9ae9jpXnv9QT8pLHEPcCirMFhxG9I=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/jfrog/jfrog-cli/general/ai"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"os"
"runtime"
"sort"
Expand Down Expand Up @@ -136,12 +140,39 @@ func execMain() error {
if warningMessage != "" {
clientlog.Warn(warningMessage)
}
if err = setUberTraceIdToken(); err != nil {
clientlog.Warn("failed generating a trace ID token:", err.Error())
}
return nil
}
err = app.Run(args)
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()
if err != nil {
return err
}
httpclient.SetUberTraceIdToken(traceID)
clientlog.Debug("Trace ID for JFrog Platform logs: ", traceID)
return nil
}

// Generates a 16 chars hexadecimal string to be used as a Trace ID token.
func generateTraceIdToken() (string, error) {
// Generate 8 random bytes.
buf := make([]byte, 8)
_, err := rand.Read(buf)
if err != nil {
return "", errorutils.CheckError(err)
}
// Convert the random bytes to a 16 chars hexadecimal string.
return hex.EncodeToString(buf), nil
}

// 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
8 changes: 8 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,11 @@ func TestIntro(t *testing.T) {
runJfrogCli(t, "intro")
assert.Contains(t, buffer.String(), "Thank you for installing version")
}

func TestGenerateAndLogTraceIdToken(t *testing.T) {
traceIdToken, err := generateTraceIdToken()
assert.NoError(t, err)
assert.Len(t, traceIdToken, 16)
_, err = strconv.ParseUint(traceIdToken, 16, 64)
assert.NoError(t, err, "unexpected: trace ID token contains non-hex characters")
}

0 comments on commit b6c104e

Please sign in to comment.