Skip to content

Commit

Permalink
Fix: signozlogspipelineprocessor: trace parser: pad with 0s to left (…
Browse files Browse the repository at this point in the history
…not right) when parsing from short hex strings (#419)

fixes SigNoz/signoz#3859
  • Loading branch information
raj-k-singh authored Oct 10, 2024
1 parent 55229e1 commit 321edc7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
48 changes: 45 additions & 3 deletions processor/signozlogspipelineprocessor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"fmt"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -157,15 +158,51 @@ func TestTraceProcessor(t *testing.T) {
- type: trace_parser
trace_id:
parse_from: attributes.traceId
span_id:
parse_from: attributes.spanId
trace_flags:
parse_from: attributes.traceFlags
`

input := []plog.Logs{makePlog(
"test log", map[string]any{"traceId": "e37e734349000e2eda9c07cca0ceb692"},
"test log", map[string]any{
"traceId": "e37e734349000e2eda9c07cca0ceb692",
"spanId": "da9c07cca0ceb692",
"traceFlags": "02",
},
)}
expectedOutput := []plog.Logs{makePlogWithTopLevelFields(
t, "test log", map[string]any{"traceId": "e37e734349000e2eda9c07cca0ceb692"},
t, "test log", map[string]any{
"traceId": "e37e734349000e2eda9c07cca0ceb692",
"spanId": "da9c07cca0ceb692",
"traceFlags": "02",
},
map[string]any{
"trace_id": "e37e734349000e2eda9c07cca0ceb692",
"trace_id": "e37e734349000e2eda9c07cca0ceb692",
"span_id": "da9c07cca0ceb692",
"trace_flags": "02",
},
)}

validateProcessorBehavior(t, confYaml, input, expectedOutput)

// trace id and span id should be padded with 0s to the left
// if provided hex strings are not of the expected length
// See https://github.com/SigNoz/signoz/issues/3859 for an example

input = []plog.Logs{makePlog("test log", map[string]any{
"traceId": "da9c07cca0ceb692",
"spanId": "ceb692",
})}

expectedOutput = []plog.Logs{makePlogWithTopLevelFields(
t, "test log", map[string]any{
"traceId": "da9c07cca0ceb692",
"spanId": "ceb692",
},
map[string]any{
"trace_id": "0000000000000000da9c07cca0ceb692",
"span_id": "0000000000ceb692",
},
)}

Expand Down Expand Up @@ -677,6 +714,11 @@ func makePlogWithTopLevelFields(t *testing.T, body string, attributes map[string
require.NoError(t, err)
lr.SetSpanID(pcommon.SpanID(spanIdBytes))
}
if traceFlags, exists := fields["trace_flags"]; exists {
flags, err := strconv.ParseUint(traceFlags.(string), 16, 64)
require.NoError(t, err)
lr.SetFlags(plog.LogRecordFlags(flags))
}

if sevText, exists := fields["severity_text"]; exists {
lr.SetSeverityText(sevText.(string))
Expand Down
7 changes: 5 additions & 2 deletions processor/signozlogspipelineprocessor/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,15 @@ func convertInto(ent *entry.Entry, dest plog.LogRecord) {

if ent.TraceID != nil {
var buffer [16]byte
copy(buffer[0:16], ent.TraceID)
// ensure buffer gets padded to left if len(ent.TraceID) != 16
copyStartIdx := (max(0, 16-len(ent.TraceID)))
copy(buffer[copyStartIdx:16], ent.TraceID)
dest.SetTraceID(buffer)
}
if ent.SpanID != nil {
var buffer [8]byte
copy(buffer[0:8], ent.SpanID)
copyStartIdx := (max(0, 8-len(ent.SpanID)))
copy(buffer[copyStartIdx:8], ent.SpanID)
dest.SetSpanID(buffer)
}
if ent.TraceFlags != nil && len(ent.TraceFlags) > 0 {
Expand Down

0 comments on commit 321edc7

Please sign in to comment.