Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New immutable type for Flags structs #5970

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pdata/pcommon/trace_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pcommon

const isSampledMask = uint8(1)

var DefaultTraceFlags = NewTraceFlagsFromRaw(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 struct {
orig uint8
}

// NewTraceFlagsFromRaw creates a new empty TraceFlags.
//
// 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 NewTraceFlagsFromRaw(val uint8) TraceFlags {
return TraceFlags{orig: val}
}

// IsSampled returns true if the LogRecordFlags contains the IsSampled flag.
func (ms TraceFlags) IsSampled() bool {
return ms.orig&isSampledMask != 0
}

// WithIsSampled returns a new TraceFlags, with the IsSampled flag set to the given value.
func (ms TraceFlags) WithIsSampled(b bool) TraceFlags {
orig := ms.orig
if b {
orig |= isSampledMask
} else {
orig &^= isSampledMask
}
return NewTraceFlagsFromRaw(orig)
}

// AsRaw converts LogRecordFlags to the OTLP uint32 representation.
func (ms TraceFlags) AsRaw() uint8 {
return ms.orig
}
49 changes: 49 additions & 0 deletions pdata/pcommon/trace_flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 TestDefaultTraceFlags(t *testing.T) {
flags := DefaultTraceFlags
assert.False(t, flags.IsSampled())
assert.Equal(t, uint8(0), flags.AsRaw())

flags = flags.WithIsSampled(true)
assert.True(t, flags.IsSampled())
assert.Equal(t, uint8(1), flags.AsRaw())

flags = flags.WithIsSampled(false)
assert.False(t, flags.IsSampled())
assert.Equal(t, uint8(0), flags.AsRaw())
}

func TestNewTraceFlagsFromRaw(t *testing.T) {
flags := NewTraceFlagsFromRaw(1)
assert.True(t, flags.IsSampled())
assert.Equal(t, uint8(1), flags.AsRaw())

flags = flags.WithIsSampled(false)
assert.False(t, flags.IsSampled())
assert.Equal(t, uint8(0), flags.AsRaw())

flags = flags.WithIsSampled(true)
assert.True(t, flags.IsSampled())
assert.Equal(t, uint8(1), flags.AsRaw())
}
9 changes: 7 additions & 2 deletions pdata/plog/generated_logs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pdata/plog/generated_logs_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions pdata/plog/log_record_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 = NewLogRecordFlagsFromRaw(uint32(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.
//
// 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 struct {
orig uint32
}

// NewLogRecordFlagsFromRaw 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 NewLogRecordFlagsFromRaw(val uint32) LogRecordFlags {
return LogRecordFlags{orig: val}
}

// TraceFlags copies all properties from the current struct to the dest.
func (ms LogRecordFlags) TraceFlags() pcommon.TraceFlags {
return pcommon.NewTraceFlagsFromRaw(uint8(ms.AsRaw() & traceFlagsMask))
}

// SetTraceFlags moves all properties from the current struct to dest resetting the current instance to its zero value
func (ms LogRecordFlags) WithTraceFlags(v pcommon.TraceFlags) LogRecordFlags {
orig := ms.orig
orig &= ^traceFlagsMask // cleanup old trace flags
orig |= uint32(v.AsRaw()) // set new trace flags
return LogRecordFlags{orig: orig}
}

// AsRaw converts LogRecordFlags to the OTLP uint32 representation.
func (ms LogRecordFlags) AsRaw() uint32 {
return ms.orig
}
51 changes: 51 additions & 0 deletions pdata/plog/log_record_flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 TestDefaultLogRecordFlags(t *testing.T) {
flags := DefaultLogRecordFlags
assert.Equal(t, pcommon.DefaultTraceFlags, flags.TraceFlags())
assert.Equal(t, uint32(0), flags.AsRaw())

flags = flags.WithTraceFlags(pcommon.DefaultTraceFlags.WithIsSampled(true))
assert.True(t, flags.TraceFlags().IsSampled())
assert.Equal(t, uint32(1), flags.AsRaw())

flags = flags.WithTraceFlags(pcommon.DefaultTraceFlags.WithIsSampled(false))
assert.False(t, flags.TraceFlags().IsSampled())
assert.Equal(t, uint32(0), flags.AsRaw())
}

func TestLogRecordFlags(t *testing.T) {
flags := NewLogRecordFlagsFromRaw(1)
assert.True(t, flags.TraceFlags().IsSampled())
assert.Equal(t, uint32(1), flags.AsRaw())

flags = flags.WithTraceFlags(pcommon.DefaultTraceFlags.WithIsSampled(false))
assert.False(t, flags.TraceFlags().IsSampled())
assert.Equal(t, uint32(0), flags.AsRaw())

flags = flags.WithTraceFlags(pcommon.DefaultTraceFlags.WithIsSampled(true))
assert.True(t, flags.TraceFlags().IsSampled())
assert.Equal(t, uint32(1), flags.AsRaw())
}
61 changes: 0 additions & 61 deletions pdata/plog/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
27 changes: 0 additions & 27 deletions pdata/plog/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down