Skip to content

Commit

Permalink
Add trace attributes to the log entry (#3018)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexvanboxel authored Apr 15, 2021
1 parent 9a981e3 commit 02ab2b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/stanza/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions internal/stanza/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
1 change: 1 addition & 0 deletions internal/stanza/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 02ab2b4

Please sign in to comment.