Skip to content

Commit

Permalink
[pkg/ottl] Use hex.EncodeToString (#33578)
Browse files Browse the repository at this point in the history
**Description:** 
Noticed a bug in the new debug logs where nil IDs (All 0s) were not
being printed correctly.

The feature hasn't been released yet, so as long as this gets merged
before the next release we dont need a changelog


**Testing:** <Describe what testing was performed and which tests were
added.>
Tested locally
  • Loading branch information
TylerHelmuth committed Jun 14, 2024
1 parent debbf30 commit 5fbef98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
22 changes: 15 additions & 7 deletions pkg/ottl/contexts/internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package logging // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/logging"

import (
"encoding/hex"
"errors"

"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -90,6 +91,9 @@ type Span ptrace.Span

func (s Span) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
ss := ptrace.Span(s)
parentSpanID := ss.ParentSpanID()
spanID := ss.SpanID()
traceID := ss.TraceID()
err := encoder.AddObject("attributes", Map(ss.Attributes()))
encoder.AddUint32("dropped_attribute_count", ss.DroppedAttributesCount())
encoder.AddUint32("dropped_events_count", ss.DroppedEventsCount())
Expand All @@ -99,12 +103,12 @@ func (s Span) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("kind", ss.Kind().String())
err = errors.Join(err, encoder.AddArray("links", SpanLinkSlice(ss.Links())))
encoder.AddString("name", ss.Name())
encoder.AddString("parent_span_id", ss.ParentSpanID().String())
encoder.AddString("span_id", ss.SpanID().String())
encoder.AddString("parent_span_id", hex.EncodeToString(parentSpanID[:]))
encoder.AddString("span_id", hex.EncodeToString(spanID[:]))
encoder.AddUint64("start_time_unix_nano", uint64(ss.StartTimestamp()))
encoder.AddString("status.code", ss.Status().Code().String())
encoder.AddString("status.message", ss.Status().Message())
encoder.AddString("trace_id", ss.TraceID().String())
encoder.AddString("trace_id", hex.EncodeToString(traceID[:]))
encoder.AddString("trace_state", ss.TraceState().AsRaw())
return err
}
Expand Down Expand Up @@ -146,11 +150,13 @@ type SpanLink ptrace.SpanLink

func (s SpanLink) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
sl := ptrace.SpanLink(s)
spanID := sl.SpanID()
traceID := sl.TraceID()
err := encoder.AddObject("attributes", Map(sl.Attributes()))
encoder.AddUint32("dropped_attribute_count", sl.DroppedAttributesCount())
encoder.AddUint32("flags", sl.Flags())
encoder.AddString("span_id", sl.SpanID().String())
encoder.AddString("trace_id", sl.TraceID().String())
encoder.AddString("span_id", hex.EncodeToString(spanID[:]))
encoder.AddString("trace_id", hex.EncodeToString(traceID[:]))
encoder.AddString("trace_state", sl.TraceState().AsRaw())
return err
}
Expand Down Expand Up @@ -368,10 +374,12 @@ type Exemplar pmetric.Exemplar

func (e Exemplar) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
ee := pmetric.Exemplar(e)
spanID := ee.SpanID()
traceID := ee.TraceID()
err := encoder.AddObject("filtered_attributes", Map(ee.FilteredAttributes()))
encoder.AddString("span_id", ee.SpanID().String())
encoder.AddString("span_id", hex.EncodeToString(spanID[:]))
encoder.AddUint64("time_unix_nano", uint64(ee.Timestamp()))
encoder.AddString("trace_id", ee.TraceID().String())
encoder.AddString("trace_id", hex.EncodeToString(traceID[:]))
if ee.ValueType() == pmetric.ExemplarValueTypeInt {
encoder.AddInt64("value_int", ee.IntValue())
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/ottl/contexts/ottllog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ type logRecord plog.LogRecord

func (l logRecord) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
lr := plog.LogRecord(l)
spanID := lr.SpanID()
traceID := lr.TraceID()
err := encoder.AddObject("attributes", logging.Map(lr.Attributes()))
encoder.AddString("body", lr.Body().AsString())
encoder.AddUint32("dropped_attribute_count", lr.DroppedAttributesCount())
encoder.AddUint32("flags", uint32(lr.Flags()))
encoder.AddUint64("observed_time_unix_nano", uint64(lr.ObservedTimestamp()))
encoder.AddInt32("severity_number", int32(lr.SeverityNumber()))
encoder.AddString("severity_text", lr.SeverityText())
encoder.AddString("span_id", lr.SpanID().String())
encoder.AddString("span_id", hex.EncodeToString(spanID[:]))
encoder.AddUint64("time_unix_nano", uint64(lr.Timestamp()))
encoder.AddString("trace_id", lr.TraceID().String())
encoder.AddString("trace_id", hex.EncodeToString(traceID[:]))
return err
}

Expand Down

0 comments on commit 5fbef98

Please sign in to comment.