diff --git a/entry/entry.go b/entry/entry.go index f10d7b56..1a8e3ee7 100644 --- a/entry/entry.go +++ b/entry/entry.go @@ -48,9 +48,9 @@ type Entry struct { Attributes map[string]string `json:"attributes,omitempty" yaml:"attributes,omitempty"` Resource map[string]string `json:"resource,omitempty" yaml:"resource,omitempty"` Record interface{} `json:"record" yaml:"record"` - TraceId []byte `json:"trace_id" yaml:"trace_id"` - SpanId []byte `json:"span_id" yaml:"span_id"` - TraceFlags []byte `json:"trace_flags" yaml:"trace_flags"` + TraceId []byte `json:"trace_id,omitempty" yaml:"trace_id,omitempty"` + SpanId []byte `json:"span_id,omitempty" yaml:"span_id,omitempty"` + TraceFlags []byte `json:"trace_flags,omitempty" yaml:"trace_flags,omitempty"` } // New will create a new log entry with current timestamp and an empty record. @@ -199,8 +199,8 @@ func (entry *Entry) Copy() *Entry { Attributes: copyStringMap(entry.Attributes), Resource: copyStringMap(entry.Resource), Record: copyValue(entry.Record), - TraceId: entry.TraceId, - SpanId: entry.SpanId, - TraceFlags: entry.TraceFlags, + TraceId: copyByteArray(entry.TraceId), + SpanId: copyByteArray(entry.SpanId), + TraceFlags: copyByteArray(entry.TraceFlags), } } diff --git a/entry/entry_test.go b/entry/entry_test.go index 9e1965cf..143ab294 100644 --- a/entry/entry_test.go +++ b/entry/entry_test.go @@ -152,9 +152,9 @@ func TestCopy(t *testing.T) { entry.Record = "new" entry.Attributes = map[string]string{"label": "new value"} entry.Resource = map[string]string{"resource": "new value"} - entry.TraceId = []byte{0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} - entry.SpanId = []byte{0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03} - entry.TraceFlags = []byte{0x00} + entry.TraceId[0] = 0xff + entry.SpanId[0] = 0xff + entry.TraceFlags[0] = 0xff require.Equal(t, time.Time{}, copy.Timestamp) require.Equal(t, Severity(0), copy.Severity) @@ -167,6 +167,32 @@ func TestCopy(t *testing.T) { require.Equal(t, []byte{0x01}, copy.TraceFlags) } +func TestCopyNil(t *testing.T) { + entry := New() + entry.Timestamp = time.Time{} + copy := entry.Copy() + + entry.Severity = Severity(1) + entry.SeverityText = "1" + entry.Timestamp = time.Now() + entry.Record = "new" + entry.Attributes = map[string]string{"label": "new value"} + entry.Resource = map[string]string{"resource": "new value"} + entry.TraceId = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} + entry.SpanId = []byte{0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03} + entry.TraceFlags = []byte{0x01} + + require.Equal(t, time.Time{}, copy.Timestamp) + require.Equal(t, Severity(0), copy.Severity) + require.Equal(t, "", copy.SeverityText) + require.Equal(t, map[string]string{}, copy.Attributes) + require.Equal(t, map[string]string{}, copy.Resource) + require.Equal(t, nil, copy.Record) + require.Equal(t, []byte{}, copy.TraceId) + require.Equal(t, []byte{}, copy.SpanId) + require.Equal(t, []byte{}, copy.TraceFlags) +} + func TestFieldFromString(t *testing.T) { cases := []struct { name string diff --git a/operator/builtin/output/stdout/stdout_test.go b/operator/builtin/output/stdout/stdout_test.go index cf0f1490..5aa83535 100644 --- a/operator/builtin/output/stdout/stdout_test.go +++ b/operator/builtin/output/stdout/stdout_test.go @@ -56,6 +56,6 @@ func TestStdoutOperator(t *testing.T) { marshalledTimestamp, err := json.Marshal(ts) require.NoError(t, err) - expected := `{"timestamp":` + string(marshalledTimestamp) + `,"severity":0,"record":"test record","trace_id":null,"span_id":null,"trace_flags":null}` + "\n" + expected := `{"timestamp":` + string(marshalledTimestamp) + `,"severity":0,"record":"test record"}` + "\n" require.Equal(t, expected, buf.String()) } diff --git a/operator/builtin/parser/trace/trace.go b/operator/builtin/parser/trace/trace.go index 0464054e..d699f469 100644 --- a/operator/builtin/parser/trace/trace.go +++ b/operator/builtin/parser/trace/trace.go @@ -34,13 +34,13 @@ func NewTraceParserConfig(operatorID string) *TraceParserConfig { } } -// SeverityParserConfig is the configuration of a severity parser operator. +// TraceParserConfig is the configuration of a trace parser operator. type TraceParserConfig struct { helper.TransformerConfig `mapstructure:",squash" yaml:",inline"` helper.TraceParser `mapstructure:",omitempty,squash" yaml:",omitempty,inline"` } -// Build will build a time parser operator. +// Build will build a trace parser operator. func (c TraceParserConfig) Build(context operator.BuildContext) ([]operator.Operator, error) { transformerOperator, err := c.TransformerConfig.Build(context) if err != nil { @@ -59,13 +59,13 @@ func (c TraceParserConfig) Build(context operator.BuildContext) ([]operator.Oper return []operator.Operator{traceOperator}, nil } -// SeverityParserOperator is an operator that parses time from a field to an entry. +// TraceParserConfig is an operator that parses traces from fields to an entry. type TraceParserOperator struct { helper.TransformerOperator helper.TraceParser } -// Process will parse time from an entry. +// Process will parse traces from an entry. func (p *TraceParserOperator) Process(ctx context.Context, entry *entry.Entry) error { return p.ProcessWith(ctx, entry, p.Parse) } diff --git a/operator/builtin/parser/trace/trace_test.go b/operator/builtin/parser/trace/trace_test.go index 22df6762..2d2c9191 100644 --- a/operator/builtin/parser/trace/trace_test.go +++ b/operator/builtin/parser/trace/trace_test.go @@ -137,19 +137,22 @@ func TestTraceParserParse(t *testing.T) { require.Equal(t, tc.expectedRecord, e.Record) traceId, _ := hex.DecodeString(tc.traceId) if len(tc.traceId) == 0 { - traceId = nil + require.Nil(t, e.TraceId) + } else { + require.Equal(t, traceId, e.TraceId) } spanId, _ := hex.DecodeString(tc.spanId) if len(tc.spanId) == 0 { - spanId = nil + require.Nil(t, e.SpanId) + } else { + require.Equal(t, spanId, e.SpanId) } traceFlags, _ := hex.DecodeString(tc.traceFlags) if len(tc.traceFlags) == 0 { - traceFlags = nil + require.Nil(t, e.TraceFlags) + } else { + require.Equal(t, traceFlags, e.TraceFlags) } - require.Equal(t, traceId, e.TraceId) - require.Equal(t, spanId, e.SpanId) - require.Equal(t, traceFlags, e.TraceFlags) }) } } diff --git a/operator/helper/parser.go b/operator/helper/parser.go index 5adcc27b..eda29a74 100644 --- a/operator/helper/parser.go +++ b/operator/helper/parser.go @@ -163,8 +163,9 @@ func (p *ParserOperator) ParseWith(ctx context.Context, entry *entry.Entry, pars severityParseErr = p.SeverityParser.Parse(entry) } + var traceParseErr error if p.TraceParser != nil { - _ = p.TraceParser.Parse(entry) + traceParseErr = p.TraceParser.Parse(entry) } // Handle time or severity parsing errors after attempting to parse both @@ -174,6 +175,9 @@ func (p *ParserOperator) ParseWith(ctx context.Context, entry *entry.Entry, pars if severityParseErr != nil { return p.HandleEntryError(ctx, entry, errors.Wrap(severityParseErr, "severity parser")) } + if traceParseErr != nil { + return p.HandleEntryError(ctx, entry, errors.Wrap(traceParseErr, "trace parser")) + } return nil }