From 4b5362dc82587fea29ab7f03287008f5304e933f Mon Sep 17 00:00:00 2001 From: Robi Nino Date: Thu, 11 Apr 2024 18:11:56 +0300 Subject: [PATCH] Support tracing all requests by a command --- go.mod | 2 +- go.sum | 4 ++-- main.go | 25 +++++++++++++++++++++++++ main_test.go | 16 ++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index fa461dfcc..74980fa0d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index fd327bf24..2be85ac45 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/main.go b/main.go index 8482344cc..d00aa846c 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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" diff --git a/main_test.go b/main_test.go index f0f8859a8..1bec829d4 100644 --- a/main_test.go +++ b/main_test.go @@ -4,6 +4,7 @@ import ( "errors" "flag" "fmt" + "github.com/jfrog/jfrog-client-go/http/httpclient" "os" "path/filepath" "strconv" @@ -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') +}