-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New immutable type for Flags structs
Signed-off-by: Bogdan <[email protected]>
- Loading branch information
1 parent
40cd9ba
commit b1cd7af
Showing
8 changed files
with
176 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 LogRecordFlags 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.EqualValues(t, uint8(0), flags) | ||
|
||
flags = flags.WithIsSampled(true) | ||
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) | ||
} | ||
|
||
func TestNewTraceFlagsFromRaw(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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 copies all properties from the current struct to the dest. | ||
func (ms LogRecordFlags) TraceFlags() pcommon.TraceFlags { | ||
return pcommon.TraceFlags(uint32(ms) & traceFlagsMask) | ||
} | ||
|
||
// WithTraceFlags 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 := uint32(ms) | ||
orig &= ^traceFlagsMask // cleanup old trace flags | ||
orig |= uint32(v) // set new trace flags | ||
return LogRecordFlags(orig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.EqualValues(t, uint32(0), flags) | ||
|
||
flags = flags.WithTraceFlags(pcommon.DefaultTraceFlags.WithIsSampled(true)) | ||
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) | ||
} | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters