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

log: Change EnabledParameters to have a field instead of getter and setter #6009

Merged
merged 9 commits into from
Dec 3, 2024
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Propagate non-retryable error messages to client in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#5929)
- Propagate non-retryable error messages to client in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#5929)
- Performance improvements for attribute value `AsStringSlice`, `AsFloat64Slice`, `AsInt64Slice`, `AsBoolSlice`. (#6011)
- Change `EnabledParameters` to have a `Severity` field instead of a getter and setter in `go.opentelemetry.io/otel/log`. (#6009)

### Fixed

Expand Down
6 changes: 3 additions & 3 deletions log/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ allocations and make it possible to handle new arguments, `Enabled` accepts
a `EnabledParameters` struct, defined in [logger.go](logger.go), as the second
method argument.

The `EnabledParameters` getters are returning values using the `(value, ok)`
idiom in order to indicate if the values were actually set by the caller or if
there are unspecified.
The `EnabledParameters` uses fields, instead of getters and setters, to allow
simpler usage which allows configuring the `EnabledParameters` in the same line
where `Enabled` is called.

### noop package

Expand Down
15 changes: 1 addition & 14 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,5 @@ func WithSchemaURL(schemaURL string) LoggerOption {

// EnabledParameters represents payload for [Logger]'s Enabled method.
type EnabledParameters struct {
severity Severity
severitySet bool
}

// Severity returns the [Severity] level value, or [SeverityUndefined] if no value was set.
// The ok result indicates whether the value was set.
func (r *EnabledParameters) Severity() (value Severity, ok bool) {
return r.severity, r.severitySet
}

// SetSeverity sets the [Severity] level.
func (r *EnabledParameters) SetSeverity(level Severity) {
r.severity = level
r.severitySet = true
Severity Severity
}
29 changes: 10 additions & 19 deletions log/logtest/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,16 @@ func TestRecorderLoggerCreatesNewStruct(t *testing.T) {

func TestLoggerEnabled(t *testing.T) {
for _, tt := range []struct {
name string
options []Option
ctx context.Context
buildEnabledParameters func() log.EnabledParameters

isEnabled bool
name string
options []Option
ctx context.Context
enabledParams log.EnabledParameters
want bool
}{
{
name: "the default option enables every log entry",
ctx: context.Background(),
buildEnabledParameters: func() log.EnabledParameters {
return log.EnabledParameters{}
},

isEnabled: true,
want: true,
},
{
name: "with everything disabled",
Expand All @@ -91,17 +86,13 @@ func TestLoggerEnabled(t *testing.T) {
return false
}),
},
ctx: context.Background(),
buildEnabledParameters: func() log.EnabledParameters {
return log.EnabledParameters{}
},

isEnabled: false,
ctx: context.Background(),
want: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
e := NewRecorder(tt.options...).Logger("test").Enabled(tt.ctx, tt.buildEnabledParameters())
assert.Equal(t, tt.isEnabled, e)
e := NewRecorder(tt.options...).Logger("test").Enabled(tt.ctx, tt.enabledParams)
assert.Equal(t, tt.want, e)
})
}
}
Expand Down
7 changes: 3 additions & 4 deletions sdk/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,9 @@ func BenchmarkLoggerEnabled(b *testing.B) {
WithProcessor(newFltrProcessor("0", false)),
WithProcessor(newFltrProcessor("1", true)),
)
logger := provider.Logger("BenchmarkLoggerEnabled")
ctx, param := context.Background(), log.EnabledParameters{}
param.SetSeverity(log.SeverityDebug)

logger := provider.Logger(b.Name())
ctx := context.Background()
param := log.EnabledParameters{Severity: log.SeverityDebug}
var enabled bool

b.ReportAllocs()
Expand Down
Loading