Skip to content

Commit

Permalink
Support tracing all requests by a command
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino committed Apr 11, 2024
1 parent 011060f commit 4b5362d
Show file tree
Hide file tree
Showing 4 changed files with 44 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.20240408074156-13680c04f22e

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20240408071430-62ee0279ac58
replace github.com/jfrog/jfrog-client-go => github.com/RobiNino/jfrog-client-go v0.0.0-20240411150559-2faeebb5781d

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 @@ -14,6 +14,8 @@ github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7
github.com/Microsoft/hcsshim v0.11.4/go.mod h1:smjE4dvqPX9Zldna+t5FG3rnoHhaB7QYxPRqGcpAD9w=
github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78=
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/RobiNino/jfrog-client-go v0.0.0-20240411150559-2faeebb5781d h1:NgYUt3M+SphM2vZYRqHkzp1KSkZNqFdNJzAI7hk1Pww=
github.com/RobiNino/jfrog-client-go v0.0.0-20240411150559-2faeebb5781d/go.mod h1:tUyEmxznphh0nwAGo6xz9Sps7RRW/TBMxIJZteo+j2k=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
Expand Down Expand Up @@ -139,8 +141,6 @@ github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240408074156-13680c04f22e h1:PjCz
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20240408074156-13680c04f22e/go.mod h1:qXAP68g+DlyX2wk5znNbQdK2CcEHfOLOfYXPzdlnkxI=
github.com/jfrog/jfrog-cli-security v1.0.6-0.20240408061620-c9b84da33d5e h1:cB+UwVdZuds5fZ5BEcnvb6GqUvXFbo3oZa0PKdkylFc=
github.com/jfrog/jfrog-cli-security v1.0.6-0.20240408061620-c9b84da33d5e/go.mod h1:Bxir0QA3vDFCqnGP4GgPTPb0sWRaVONwm9+npDGx8kg=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240408071430-62ee0279ac58 h1:yyhOfECY3WGs6MsnJQWm/U7DYNIzxBiVlEwQ3RvqxwQ=
github.com/jfrog/jfrog-client-go v1.28.1-0.20240408071430-62ee0279ac58/go.mod h1:tUyEmxznphh0nwAGo6xz9Sps7RRW/TBMxIJZteo+j2k=
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
25 changes: 25 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,33 @@ func execMain() error {
if warningMessage != "" {
clientlog.Warn(warningMessage)
}
if err = generateAndLogTraceIdToken(); err != nil {
clientlog.Debug("failed generating a trace ID token:", err.Error())
}
return nil
}
err = app.Run(args)
return err
}

// This command generates a Trace ID token. The token will be used to create an Uber Trace ID header which will be attached 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 generateAndLogTraceIdToken() error {
// Generate 8 random bytes.
var buf [8]byte
_, err := rand.Read(buf[:])
if err != nil {
return errorutils.CheckError(err)
}

// Convert the random bytes to a 16 chars hexadecimal string.
traceID := hex.EncodeToString(buf[:])

httpclient.TraceIdToken = traceID
clientlog.Debug("Trace ID for JFrog Platform logs: ", httpclient.TraceIdToken)
return 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
16 changes: 16 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"flag"
"fmt"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -355,3 +356,18 @@ func TestIntro(t *testing.T) {
runJfrogCli(t, "intro")
assert.Contains(t, buffer.String(), "Thank you for installing version")
}

func TestGenerateAndLogTraceIdToken(t *testing.T) {
assert.NoError(t, generateAndLogTraceIdToken())
assert.Len(t, httpclient.TraceIdToken, 16)

for _, char := range httpclient.TraceIdToken {
if !isHexChar(char) {
assert.Fail(t, "unexpected: trace ID token contains non-hex characters: '%s'", httpclient.TraceIdToken)
}
}
}

func isHexChar(char rune) bool {
return ('0' <= char && char <= '9') || ('a' <= char && char <= 'f') || ('A' <= char && char <= 'F')
}

0 comments on commit 4b5362d

Please sign in to comment.