From 3e95c9f85b926c07255adad05455e218bd81039d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20NO=C3=8BL?= Date: Fri, 14 Jun 2024 14:15:12 +0200 Subject: [PATCH] Fix panics on invalid sized trace & span IDs Fixes https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/33566 --- ...e_fix-panic-on-invalid-trace-span-ids.yaml | 16 ++++++++++ pkg/otlp/logs/transform.go | 9 ++++++ pkg/otlp/logs/transform_test.go | 29 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 .chloggen/kevinnoel-be_fix-panic-on-invalid-trace-span-ids.yaml 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..4516dd66 100644 --- a/pkg/otlp/logs/transform.go +++ b/pkg/otlp/logs/transform.go @@ -17,6 +17,7 @@ package logs import ( "encoding/binary" "encoding/hex" + "fmt" "strconv" "strings" @@ -224,12 +225,20 @@ func extractHostNameAndServiceName(resourceAttrs pcommon.Map, logAttrs pcommon.M func decodeTraceID(traceID string) ([16]byte, error) { var ret [16]byte + traceIDLength := len(traceID) + if traceIDLength > 16 { + return ret, fmt.Errorf("invalid length on trace ID; expected: 16, got: %v", traceIDLength) + } _, err := hex.Decode(ret[:], []byte(traceID)) return ret, err } func decodeSpanID(spanID string) ([8]byte, error) { var ret [8]byte + spanIDLength := len(spanID) + if spanIDLength > 8 { + return ret, fmt.Errorf("invalid length on span ID; expected: 8, got: %v", spanIDLength) + } _, err := hex.Decode(ret[:], []byte(spanID)) return ret, err } diff --git a/pkg/otlp/logs/transform_test.go b/pkg/otlp/logs/transform_test.go index f28b5cf2..01aa05e3 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", "d8f47f62378c2301657f395a30d6533c") + 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",