diff --git a/exporter/loggingexporter/internal/otlptext/logs.go b/exporter/loggingexporter/internal/otlptext/logs.go index 757d0099bd3..7bf4e3e5e10 100644 --- a/exporter/loggingexporter/internal/otlptext/logs.go +++ b/exporter/loggingexporter/internal/otlptext/logs.go @@ -52,7 +52,7 @@ func (textLogsMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) { buf.logAttributes("Attributes", lr.Attributes()) buf.logEntry("Trace ID: %s", lr.TraceID().HexString()) buf.logEntry("Span ID: %s", lr.SpanID().HexString()) - buf.logEntry("Flags: %d", lr.FlagsStruct().AsRaw()) + buf.logEntry("Flags: %d", lr.FlagsStruct()) } } } diff --git a/pdata/internal/cmd/pdatagen/internal/log_structs.go b/pdata/internal/cmd/pdatagen/internal/log_structs.go index 5a8d9935d53..df3781cb4cd 100644 --- a/pdata/internal/cmd/pdatagen/internal/log_structs.go +++ b/pdata/internal/cmd/pdatagen/internal/log_structs.go @@ -104,12 +104,14 @@ var logRecord = &messageValueStruct{ }, traceIDField, spanIDField, - &messageValueField{ + &primitiveTypedField{ fieldName: "FlagsStruct", originFieldName: "Flags", - returnMessage: &messageValueStruct{ - structName: "LogRecordFlags", - originFullName: "uint32", + returnType: &primitiveType{ + structName: "LogRecordFlags", + rawType: "uint32", + defaultVal: "0", + testVal: "1", }, }, &primitiveField{ diff --git a/pdata/internal/generated_wrapper_logs.go b/pdata/internal/generated_wrapper_logs.go index 451b22bc34a..b3a26822a4e 100644 --- a/pdata/internal/generated_wrapper_logs.go +++ b/pdata/internal/generated_wrapper_logs.go @@ -177,7 +177,7 @@ func FillTestLogRecord(tv LogRecord) { tv.orig.TimeUnixNano = 1234567890 tv.orig.TraceId = data.NewTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}) tv.orig.SpanId = data.NewSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}) - FillTestLogRecordFlags(NewLogRecordFlags(&tv.orig.Flags)) + tv.orig.Flags = 1 tv.orig.SeverityText = "INFO" tv.orig.SeverityNumber = otlplogs.SeverityNumber(5) FillTestValue(NewValue(&tv.orig.Body)) diff --git a/pdata/pcommon/trace_flags.go b/pdata/pcommon/trace_flags.go new file mode 100644 index 00000000000..a1e457582fc --- /dev/null +++ b/pdata/pcommon/trace_flags.go @@ -0,0 +1,25 @@ +package pcommon + +const isSampledMask = uint8(1) + +var DefaultTraceFlags = TraceFlags(0) + +// TraceFlags defines the trace flags as defined by the w3c trace-context, see +// https://www.w3.org/TR/trace-context/#trace-flags +type TraceFlags uint8 + +// IsSampled returns true if the TraceFlags contains the IsSampled flag. +func (ms TraceFlags) IsSampled() bool { + return uint8(ms)&isSampledMask != 0 +} + +// WithIsSampled returns a new TraceFlags, with the IsSampled flag set to the given value. +func (ms TraceFlags) WithIsSampled(b bool) TraceFlags { + orig := uint8(ms) + if b { + orig |= isSampledMask + } else { + orig &^= isSampledMask + } + return TraceFlags(orig) +} diff --git a/pdata/pcommon/trace_flags_test.go b/pdata/pcommon/trace_flags_test.go new file mode 100644 index 00000000000..f16f6f2eb6d --- /dev/null +++ b/pdata/pcommon/trace_flags_test.go @@ -0,0 +1,41 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pcommon + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTraceFlags(t *testing.T) { + flags := TraceFlags(1) + assert.True(t, flags.IsSampled()) + assert.EqualValues(t, uint8(1), flags) + + flags = flags.WithIsSampled(false) + assert.False(t, flags.IsSampled()) + assert.EqualValues(t, uint8(0), flags) + + flags = flags.WithIsSampled(true) + assert.True(t, flags.IsSampled()) + assert.EqualValues(t, uint8(1), flags) +} + +func TestDefaultTraceFlags(t *testing.T) { + flags := DefaultTraceFlags + assert.False(t, flags.IsSampled()) + assert.EqualValues(t, uint8(0), flags) +} diff --git a/pdata/plog/generated_logs.go b/pdata/plog/generated_logs.go index 1f6d930fedb..8b88c0adf49 100644 --- a/pdata/plog/generated_logs.go +++ b/pdata/plog/generated_logs.go @@ -641,7 +641,12 @@ func (ms LogRecord) SetSpanID(v pcommon.SpanID) { // FlagsStruct returns the flagsstruct associated with this LogRecord. func (ms LogRecord) FlagsStruct() LogRecordFlags { - return LogRecordFlags(internal.NewLogRecordFlags(&ms.getOrig().Flags)) + return LogRecordFlags(ms.getOrig().Flags) +} + +// SetFlagsStruct replaces the flagsstruct associated with this LogRecord. +func (ms LogRecord) SetFlagsStruct(v LogRecordFlags) { + ms.getOrig().Flags = uint32(v) } // SeverityText returns the severitytext associated with this LogRecord. @@ -690,7 +695,7 @@ func (ms LogRecord) CopyTo(dest LogRecord) { dest.SetTimestamp(ms.Timestamp()) dest.SetTraceID(ms.TraceID()) dest.SetSpanID(ms.SpanID()) - ms.FlagsStruct().CopyTo(dest.FlagsStruct()) + dest.SetFlagsStruct(ms.FlagsStruct()) dest.SetSeverityText(ms.SeverityText()) dest.SetSeverityNumber(ms.SeverityNumber()) ms.Body().CopyTo(dest.Body()) diff --git a/pdata/plog/generated_logs_test.go b/pdata/plog/generated_logs_test.go index 9d0e449411c..c5b6ee38639 100644 --- a/pdata/plog/generated_logs_test.go +++ b/pdata/plog/generated_logs_test.go @@ -486,8 +486,10 @@ func TestLogRecord_SpanID(t *testing.T) { func TestLogRecord_FlagsStruct(t *testing.T) { ms := NewLogRecord() - internal.FillTestLogRecordFlags(internal.LogRecordFlags(ms.FlagsStruct())) - assert.Equal(t, LogRecordFlags(internal.GenerateTestLogRecordFlags()), ms.FlagsStruct()) + assert.Equal(t, LogRecordFlags(0), ms.FlagsStruct()) + testValFlagsStruct := LogRecordFlags(1) + ms.SetFlagsStruct(testValFlagsStruct) + assert.Equal(t, testValFlagsStruct, ms.FlagsStruct()) } func TestLogRecord_SeverityText(t *testing.T) { diff --git a/pdata/plog/log_record_flags.go b/pdata/plog/log_record_flags.go new file mode 100644 index 00000000000..01658fc2485 --- /dev/null +++ b/pdata/plog/log_record_flags.go @@ -0,0 +1,40 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package plog + +import ( + "go.opentelemetry.io/collector/pdata/pcommon" +) + +const traceFlagsMask = uint32(0xFF) + +var DefaultLogRecordFlags = LogRecordFlags(0) + +// LogRecordFlags defines flags for the LogRecord. 8 least significant bits are the trace flags as +// defined in W3C Trace Context specification. 24 most significant bits are reserved and must be set to 0. +type LogRecordFlags uint32 + +// TraceFlags returns the TraceFlags part of the LogRecordFlags. +func (ms LogRecordFlags) TraceFlags() pcommon.TraceFlags { + return pcommon.TraceFlags(uint32(ms) & traceFlagsMask) +} + +// WithTraceFlags returns a new LogRecordFlags, with the TraceFlags part set to the given value. +func (ms LogRecordFlags) WithTraceFlags(v pcommon.TraceFlags) LogRecordFlags { + orig := uint32(ms) + orig &= ^traceFlagsMask // cleanup old trace flags + orig |= uint32(v) // set new trace flags + return LogRecordFlags(orig) +} diff --git a/pdata/plog/log_record_flags_test.go b/pdata/plog/log_record_flags_test.go new file mode 100644 index 00000000000..112015941a6 --- /dev/null +++ b/pdata/plog/log_record_flags_test.go @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package plog + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "go.opentelemetry.io/collector/pdata/pcommon" +) + +func TestLogRecordFlags(t *testing.T) { + flags := LogRecordFlags(1) + assert.True(t, flags.TraceFlags().IsSampled()) + assert.EqualValues(t, uint32(1), flags) + + flags = flags.WithTraceFlags(pcommon.DefaultTraceFlags.WithIsSampled(false)) + assert.False(t, flags.TraceFlags().IsSampled()) + assert.EqualValues(t, uint32(0), flags) + + flags = flags.WithTraceFlags(pcommon.DefaultTraceFlags.WithIsSampled(true)) + assert.True(t, flags.TraceFlags().IsSampled()) + assert.EqualValues(t, uint32(1), flags) +} + +func TestDefaultLogRecordFlags(t *testing.T) { + flags := DefaultLogRecordFlags + assert.Equal(t, pcommon.DefaultTraceFlags, flags.TraceFlags()) + assert.EqualValues(t, uint32(0), flags) +} diff --git a/pdata/plog/logs.go b/pdata/plog/logs.go index 223ffb1899f..2474771496e 100644 --- a/pdata/plog/logs.go +++ b/pdata/plog/logs.go @@ -191,64 +191,3 @@ func (ms LogRecord) Flags() uint32 { func (ms LogRecord) SetFlags(v uint32) { ms.getOrig().Flags = v } - -const ( - traceFlagsNone = uint32(0) - isSampledMask = uint32(1) -) - -// LogRecordFlags defines flags for the LogRecord. 8 least significant bits are the trace flags as -// defined in W3C Trace Context specification. 24 most significant bits are reserved and must be set to 0. -// -// This is a reference type, if passed by value and callee modifies it the caller will see the modification. -// -// Must use NewLogRecordFlags function to create new instances. -// Important: zero-initialized instance is not valid for use. -type LogRecordFlags internal.LogRecordFlags - -func newLogRecordFlags(orig *uint32) LogRecordFlags { - return LogRecordFlags(internal.NewLogRecordFlags(orig)) -} - -func (ms LogRecordFlags) getOrig() *uint32 { - return internal.GetOrigLogRecordFlags(internal.LogRecordFlags(ms)) -} - -// NewLogRecordFlags creates a new empty LogRecordFlags. -// -// This must be used only in testing code. Users should use "AppendEmpty" when part of a Slice, -// OR directly access the member if this is embedded in another struct. -func NewLogRecordFlags() LogRecordFlags { - return newLogRecordFlags(new(uint32)) -} - -// MoveTo moves all properties from the current struct to dest resetting the current instance to its zero value -func (ms LogRecordFlags) MoveTo(dest LogRecordFlags) { - *dest.getOrig() = *ms.getOrig() - *ms.getOrig() = traceFlagsNone -} - -// CopyTo copies all properties from the current struct to the dest. -func (ms LogRecordFlags) CopyTo(dest LogRecordFlags) { - *dest.getOrig() = *ms.getOrig() -} - -// IsSampled returns true if the LogRecordFlags contains the IsSampled flag. -func (ms LogRecordFlags) IsSampled() bool { - return *ms.getOrig()&isSampledMask != 0 -} - -// SetIsSampled sets the IsSampled flag if true and removes it if false. -// Setting this Flag when it is already set is a no-op. -func (ms LogRecordFlags) SetIsSampled(b bool) { - if b { - *ms.getOrig() |= isSampledMask - } else { - *ms.getOrig() &^= isSampledMask - } -} - -// AsRaw converts LogRecordFlags to the OTLP uint32 representation. -func (ms LogRecordFlags) AsRaw() uint32 { - return *ms.getOrig() -} diff --git a/pdata/plog/logs_test.go b/pdata/plog/logs_test.go index e3ef8f7f6c5..66a317b7e59 100644 --- a/pdata/plog/logs_test.go +++ b/pdata/plog/logs_test.go @@ -132,33 +132,6 @@ func TestLogsClone(t *testing.T) { assert.EqualValues(t, logs, logs.Clone()) } -func TestLogRecordFlags(t *testing.T) { - flags := LogRecordFlags(internal.GenerateTestLogRecordFlags()) - assert.True(t, flags.IsSampled()) - assert.Equal(t, uint32(1), flags.AsRaw()) - - flags.SetIsSampled(false) - flags.SetIsSampled(false) - assert.False(t, flags.IsSampled()) - assert.Equal(t, uint32(0), flags.AsRaw()) - - flags.SetIsSampled(true) - flags.SetIsSampled(true) - assert.True(t, flags.IsSampled()) - assert.Equal(t, uint32(1), flags.AsRaw()) - - moveFlags := NewLogRecordFlags() - assert.False(t, moveFlags.IsSampled()) - - flags.MoveTo(moveFlags) - assert.False(t, flags.IsSampled()) - assert.True(t, moveFlags.IsSampled()) - - moveFlags.CopyTo(flags) - assert.True(t, flags.IsSampled()) - assert.True(t, moveFlags.IsSampled()) -} - func BenchmarkLogsClone(b *testing.B) { logs := NewLogs() internal.FillTestResourceLogsSlice(internal.ResourceLogsSlice(logs.ResourceLogs()))