From 6251b34c1c5c10237cdeda7bbdfa14b2e68caaf8 Mon Sep 17 00:00:00 2001 From: JuanJo Ciarlante Date: Wed, 6 Sep 2023 18:28:34 -0300 Subject: [PATCH] use falcopayload.UUID if template rendering produced no value (instead of re-generating a random 32char string) Signed-off-by: JuanJo Ciarlante --- outputs/otlp.go | 27 +++++++-------------------- outputs/otlp_test.go | 3 +++ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/outputs/otlp.go b/outputs/otlp.go index 2ef946626f..764dd09637 100644 --- a/outputs/otlp.go +++ b/outputs/otlp.go @@ -3,7 +3,6 @@ package outputs import ( "bytes" "context" - "crypto/rand" "encoding/hex" "fmt" "hash/fnv" @@ -147,26 +146,14 @@ func generateTraceID(falcopayload types.FalcoPayload, config *types.Configuratio switch traceIDStr { case "": case "": - // Template produced no string :(, generate a random 32 character string - traceIDStr, err = randomHex(16) - if err != nil { - return traceID, "", err - } - default: - // Hash the returned template- rendered string to generate a 32 character traceID - hash := fnv.New128a() - hash.Write([]byte(traceIDStr)) - digest := hash.Sum(nil) - traceIDStr = hex.EncodeToString(digest[:]) + // Template produced no string, derive the traceID from the payload UUID + traceIDStr = falcopayload.UUID } + // Hash the returned template- rendered string to generate a 32 character traceID + hash := fnv.New128a() + hash.Write([]byte(traceIDStr)) + digest := hash.Sum(nil) + traceIDStr = hex.EncodeToString(digest[:]) traceID, err = trace.TraceIDFromHex(traceIDStr) return traceID, tplStr, err } - -func randomHex(n int) (string, error) { - bytes := make([]byte, n) - if _, err := rand.Read(bytes); err != nil { - return "", err - } - return hex.EncodeToString(bytes), nil -} diff --git a/outputs/otlp_test.go b/outputs/otlp_test.go index 565b2cab7b..390a1e80a7 100644 --- a/outputs/otlp_test.go +++ b/outputs/otlp_test.go @@ -195,6 +195,7 @@ func TestOtlpNewTrace(t *testing.T) { } // Test newTrace() + c.fp.UUID = uuid.New().String() span := client.newTrace(c.fp) require.NotNil(t, span) @@ -211,6 +212,8 @@ func TestOtlpNewTrace(t *testing.T) { } // Verify traceID + // ~hack: to pass c.expectedRandom==true case, recreate fp.UUID as generateTraceID() derives from it + c.fp.UUID = uuid.New().String() traceID, templateStr, err := generateTraceID(c.fp, &c.config) require.Nil(t, err, c.msg) // Always generate a traceID (unless errored)