Skip to content

Commit

Permalink
use falcopayload.UUID if template rendering produced no value (instea…
Browse files Browse the repository at this point in the history
…d of re-generating a random 32char string)

Signed-off-by: JuanJo Ciarlante <[email protected]>
  • Loading branch information
jjo committed Sep 14, 2023
1 parent 4d3cd22 commit 6251b34
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
27 changes: 7 additions & 20 deletions outputs/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package outputs
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"hash/fnv"
Expand Down Expand Up @@ -147,26 +146,14 @@ func generateTraceID(falcopayload types.FalcoPayload, config *types.Configuratio
switch traceIDStr {
case "":
case "<no value>":
// 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
}
3 changes: 3 additions & 0 deletions outputs/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit 6251b34

Please sign in to comment.