From 02ab2b447a7af99503421b00ec77e8ee645bb3a2 Mon Sep 17 00:00:00 2001 From: Alex Van Boxel Date: Thu, 15 Apr 2021 22:33:14 +0200 Subject: [PATCH] Add trace attributes to the log entry (#3018) JSON log parsers are now able to extract the OpenTelemetry compliant trace_id, span_id and trace_flags attributes and add them to the correct attributes. --- internal/stanza/converter.go | 19 +++++++++++++++++++ internal/stanza/converter_test.go | 23 +++++++++++++++++++++++ internal/stanza/register.go | 1 + 3 files changed, 43 insertions(+) diff --git a/internal/stanza/converter.go b/internal/stanza/converter.go index 74ae54963161..1f7498843abe 100644 --- a/internal/stanza/converter.go +++ b/internal/stanza/converter.go @@ -337,6 +337,25 @@ func convertInto(ent *entry.Entry, dest pdata.LogRecord) { } insertToAttributeVal(ent.Body, dest.Body()) + + if ent.TraceId != nil { + var buffer [16]byte + copy(buffer[0:16], ent.TraceId) + dest.SetTraceID(pdata.NewTraceID(buffer)) + } + if ent.SpanId != nil { + var buffer [8]byte + copy(buffer[0:8], ent.SpanId) + dest.SetSpanID(pdata.NewSpanID(buffer)) + } + if ent.TraceFlags != nil { + // The 8 least significant bits are the trace flags as defined in W3C Trace + // Context specification. Don't override the 24 reserved bits. + flags := dest.Flags() + flags = flags & 0xFFFFFF00 + flags = flags | uint32(ent.TraceFlags[0]) + dest.SetFlags(flags) + } } func insertToAttributeVal(value interface{}, dest pdata.AttributeValue) { diff --git a/internal/stanza/converter_test.go b/internal/stanza/converter_test.go index dae6c76fde4c..0e78518b05c9 100644 --- a/internal/stanza/converter_test.go +++ b/internal/stanza/converter_test.go @@ -551,3 +551,26 @@ func TestConvertSeverity(t *testing.T) { }) } } + +func TestConvertTrace(t *testing.T) { + record := convertAndDrill(&entry.Entry{ + TraceId: []byte{ + 0x48, 0x01, 0x40, 0xf3, 0xd7, 0x70, 0xa5, 0xae, 0x32, 0xf0, 0xa2, 0x2b, 0x6a, 0x81, 0x2c, 0xff, + }, + SpanId: []byte{ + 0x32, 0xf0, 0xa2, 0x2b, 0x6a, 0x81, 0x2c, 0xff, + }, + TraceFlags: []byte{ + 0x01, + }}) + + require.Equal(t, pdata.NewTraceID( + [16]byte{ + 0x48, 0x01, 0x40, 0xf3, 0xd7, 0x70, 0xa5, 0xae, 0x32, 0xf0, 0xa2, 0x2b, 0x6a, 0x81, 0x2c, 0xff, + }), record.TraceID()) + require.Equal(t, pdata.NewSpanID( + [8]byte{ + 0x32, 0xf0, 0xa2, 0x2b, 0x6a, 0x81, 0x2c, 0xff, + }), record.SpanID()) + require.Equal(t, uint32(0x01), record.Flags()) +} diff --git a/internal/stanza/register.go b/internal/stanza/register.go index d3c8145930c4..4754f35cf914 100644 --- a/internal/stanza/register.go +++ b/internal/stanza/register.go @@ -20,6 +20,7 @@ import ( _ "github.com/open-telemetry/opentelemetry-log-collection/operator/builtin/parser/regex" _ "github.com/open-telemetry/opentelemetry-log-collection/operator/builtin/parser/severity" _ "github.com/open-telemetry/opentelemetry-log-collection/operator/builtin/parser/time" + _ "github.com/open-telemetry/opentelemetry-log-collection/operator/builtin/parser/trace" _ "github.com/open-telemetry/opentelemetry-log-collection/operator/builtin/transformer/metadata" _ "github.com/open-telemetry/opentelemetry-log-collection/operator/builtin/transformer/restructure" _ "github.com/open-telemetry/opentelemetry-log-collection/operator/builtin/transformer/router"