Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panics on invalid sized trace & span IDs #340

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/kevinnoel-be_fix-panic-on-invalid-trace-span-ids.yaml
Original file line number Diff line number Diff line change
@@ -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:
29 changes: 21 additions & 8 deletions pkg/otlp/logs/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package logs
import (
"encoding/binary"
"encoding/hex"
"errors"
"strconv"
"strings"

Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions pkg/otlp/logs/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading