diff --git a/processor/signozlogspipelineprocessor/processor_test.go b/processor/signozlogspipelineprocessor/processor_test.go index a63e550e..0683d84f 100644 --- a/processor/signozlogspipelineprocessor/processor_test.go +++ b/processor/signozlogspipelineprocessor/processor_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/hex" "fmt" + "strconv" "sync" "testing" "time" @@ -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", }, )} @@ -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)) diff --git a/processor/signozlogspipelineprocessor/utils.go b/processor/signozlogspipelineprocessor/utils.go index fe18881f..3c177303 100644 --- a/processor/signozlogspipelineprocessor/utils.go +++ b/processor/signozlogspipelineprocessor/utils.go @@ -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 {