diff --git a/.chloggen/kevinnoel-be_fix-panic-on-invalid-trace-span-ids.yaml b/.chloggen/kevinnoel-be_fix-panic-on-invalid-trace-span-ids.yaml new file mode 100644 index 00000000..8112df0b --- /dev/null +++ b/.chloggen/kevinnoel-be_fix-panic-on-invalid-trace-span-ids.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component (e.g. pkg/quantile) +component: pkg/otlp/logs + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix panics on invalid sized trace & span IDs + +# The PR related to this change +issues: [340] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/pkg/otlp/logs/transform.go b/pkg/otlp/logs/transform.go index d82405f8..ae0544e9 100644 --- a/pkg/otlp/logs/transform.go +++ b/pkg/otlp/logs/transform.go @@ -17,6 +17,7 @@ package logs import ( "encoding/binary" "encoding/hex" + "errors" "strconv" "strings" @@ -222,16 +223,28 @@ func extractHostNameAndServiceName(resourceAttrs pcommon.Map, logAttrs pcommon.M return host, service } -func decodeTraceID(traceID string) ([16]byte, error) { - var ret [16]byte - _, err := hex.Decode(ret[:], []byte(traceID)) - return ret, err +func decodeTraceID(traceIDStr string) (pcommon.TraceID, error) { + var id pcommon.TraceID + if hex.DecodedLen(len(traceIDStr)) != len(id) { + return pcommon.TraceID{}, errors.New("trace ids must be 32 hex characters") + } + _, err := hex.Decode(id[:], []byte(traceIDStr)) + if err != nil { + return pcommon.TraceID{}, err + } + return id, nil } -func decodeSpanID(spanID string) ([8]byte, error) { - var ret [8]byte - _, err := hex.Decode(ret[:], []byte(spanID)) - return ret, err +func decodeSpanID(spanIDStr string) (pcommon.SpanID, error) { + var id pcommon.SpanID + if hex.DecodedLen(len(spanIDStr)) != len(id) { + return pcommon.SpanID{}, errors.New("span ids must be 16 hex characters") + } + _, err := hex.Decode(id[:], []byte(spanIDStr)) + if err != nil { + return pcommon.SpanID{}, err + } + return id, nil } // traceIDToUint64 converts 128bit traceId to 64 bit uint64 diff --git a/pkg/otlp/logs/transform_test.go b/pkg/otlp/logs/transform_test.go index f28b5cf2..a609c898 100644 --- a/pkg/otlp/logs/transform_test.go +++ b/pkg/otlp/logs/transform_test.go @@ -284,6 +284,35 @@ func TestTranslator(t *testing.T) { }, }, }, + { + name: "trace from attributes size error", + args: args{ + lr: func() plog.LogRecord { + l := plog.NewLogRecord() + l.Attributes().PutStr("app", "test") + l.Attributes().PutStr("spanid", "2023675201651514964") + l.Attributes().PutStr("traceid", "eb068afe5e53704f3b0dc3d3e1e397cb760549a7b58547db4f1dee845d9101f8db1ccf8fdd0976a9112f") + l.Attributes().PutStr(conventions.AttributeServiceName, "otlp_col") + l.SetSeverityNumber(5) + return l + }(), + res: func() pcommon.Resource { + r := pcommon.NewResource() + return r + }(), + }, + want: datadogV2.HTTPLogItem{ + Ddtags: datadog.PtrString("otel_source:test"), + Message: *datadog.PtrString(""), + Service: datadog.PtrString("otlp_col"), + AdditionalProperties: map[string]string{ + "app": "test", + "status": "debug", + otelSeverityNumber: "5", + "service.name": "otlp_col", + }, + }, + }, { // here SeverityText should take precedence for log status name: "SeverityText",