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

Add the Enabled method to the Logger #5071

Merged
merged 9 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add `WithProxy` option in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#4906)
- Add `WithProxy` option in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlptracehttp`. (#4906)
- The `Enabled` method is added to the `Logger` interface in `go.opentelemetry.io/otel/log`.
This method is used to notify users if a log record will be emitted or not. (#5071)

### Changed

Expand Down
22 changes: 22 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ type Logger interface {
// Implementations of this method need to be safe for a user to call
// concurrently.
Emit(ctx context.Context, record Record)

// Enabled returns whether the Logger emits for the given context and
// record.
//
// The passed record is likely to be a partial record with only the
// bridge-relevant information being provided (e.g a record with only the
// Severity set). If a Logger needs more information than is provided, it
// is said to be in an indeterminate state (see below).
//
// The returned value will be true when the Logger will emit for the
// provided context and record, and will be false if the Logger will not
// emit. The returned value may be true or false in an indeterminate state.
// An implementation should default to returning true for an indeterminate
// state, but may return false if valid reasons in particular circumstances
// exist (e.g. performance, correctness).
//
// The record should not be held by the implementation. A copy should be
// made if the record needs to be held after the call returns.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
Enabled(ctx context.Context, record Record) bool
pellared marked this conversation as resolved.
Show resolved Hide resolved
pellared marked this conversation as resolved.
Show resolved Hide resolved
}

// LoggerOption applies configuration options to a [Logger].
Expand Down
3 changes: 3 additions & 0 deletions log/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ type Logger struct{ embedded.Logger }

// Emit does nothing.
func (Logger) Emit(context.Context, log.Record) {}

// Enabled returns false. No log records are ever emitted.
func (Logger) Enabled(context.Context, log.Record) bool { return false }
5 changes: 5 additions & 0 deletions sdk/log/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
return nil
}

// Enabled returns true.
func (b *BatchingProcessor) Enabled(context.Context, Record) bool {
return true

Check warning on line 38 in sdk/log/batch.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/batch.go#L37-L38

Added lines #L37 - L38 were not covered by tests
}

// Shutdown flushes queued log records and shuts down the decorated expoter.
func (b *BatchingProcessor) Shutdown(ctx context.Context) error {
// TODO (#5063): Implement.
Expand Down
5 changes: 5 additions & 0 deletions sdk/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@
func (l *logger) Emit(ctx context.Context, r log.Record) {
// TODO (#5061): Implement.
}

func (l *logger) Enabled(ctx context.Context, r log.Record) bool {

Check warning on line 24 in sdk/log/logger.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/logger.go#L24

Added line #L24 was not covered by tests
// TODO (#5061): Implement.
return true

Check warning on line 26 in sdk/log/logger.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/logger.go#L26

Added line #L26 was not covered by tests
}
57 changes: 57 additions & 0 deletions sdk/log/min_sev.go
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package log // import "go.opentelemetry.io/otel/sdk/log"

import (
"context"

"go.opentelemetry.io/otel/log"
)

// Compile-time check MinSeverity implements Processor.
var _ Processor = (*MinSeverityProcessor)(nil)

// MinSeverityProcessor is a [Processor] that limits processing of log records
// to only those with a [log.Severity] above a configured threshold.
type MinSeverityProcessor struct {
Processor
minimum log.Severity
}

// NewMinSeverityProcessor returns a [MinSeverityProcessor] that decorates the
// processor such that only [Record]s with a [log.Severity] greater than or
// equal to minimum will be processed.
func NewMinSeverityProcessor(minimum log.Severity, processor Processor) *MinSeverityProcessor {
return &MinSeverityProcessor{
Processor: processor,
minimum: minimum,

Check warning on line 28 in sdk/log/min_sev.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/min_sev.go#L25-L28

Added lines #L25 - L28 were not covered by tests
}
}

// OnEmit passes the context and record to the underlying [Processor] s
// decorates if the [log.Severity] of record is greater than or equal to the
// minimum severity s is configured at.
func (s *MinSeverityProcessor) OnEmit(ctx context.Context, record Record) error {
if !s.enabled(record) {
return nil

Check warning on line 37 in sdk/log/min_sev.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/min_sev.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}
return s.Processor.OnEmit(ctx, record)

Check warning on line 39 in sdk/log/min_sev.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/min_sev.go#L39

Added line #L39 was not covered by tests
}

// Enabled returns true the [log.Severity] of record is greater than or equal
// to the minimum severity s is configured at. It will return false if the
// severity is less than the minimum.
//
// If the record severity is unset, this will return true.
func (s *MinSeverityProcessor) Enabled(_ context.Context, record Record) bool {
return s.enabled(record)

Check warning on line 48 in sdk/log/min_sev.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/min_sev.go#L47-L48

Added lines #L47 - L48 were not covered by tests
}

func (s *MinSeverityProcessor) enabled(r Record) bool {

Check warning on line 51 in sdk/log/min_sev.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/min_sev.go#L51

Added line #L51 was not covered by tests
// TODO (#5067): replace with var definition when added.
const unsetSeverity log.Severity = 0

Check warning on line 53 in sdk/log/min_sev.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/min_sev.go#L53

Added line #L53 was not covered by tests

severity := r.Severity()
return severity == unsetSeverity || s.minimum <= severity

Check warning on line 56 in sdk/log/min_sev.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/min_sev.go#L55-L56

Added lines #L55 - L56 were not covered by tests
}
18 changes: 18 additions & 0 deletions sdk/log/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ type Processor interface {
// Before modifying a Record, the implementation must use Record.Clone
// to create a copy that shares no state with the original.
OnEmit(ctx context.Context, record Record) error
// Enabled returns whether the Processor will process for the given context
// and record.
//
pellared marked this conversation as resolved.
Show resolved Hide resolved
// The passed record is likely to be a partial record with only the
// bridge-relevant information being provided (e.g a record with only the
// Severity set). If a Logger needs more information than is provided, it
// is said to be in an indeterminate state (see below).
//
// The returned value will be true when the Processor will process for the
// provided context and record, and will be false if the Processor will not
// process. The returned value may be true or false in an indeterminate
// state. An implementation should default to returning true for an
// indeterminate state, but may return false if valid reasons in particular
// circumstances exist (e.g. performance, correctness).
//
// Before modifying a Record, the implementation must use Record.Clone
// to create a copy that shares no state with the original.
Enabled(ctx context.Context, record Record) bool
pellared marked this conversation as resolved.
Show resolved Hide resolved
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
// Shutdown is called when the SDK shuts down. Any cleanup or release of
// resources held by the exporter should be done in this call.
//
Expand Down
5 changes: 5 additions & 0 deletions sdk/log/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
return nil
}

// Enabled returns true.
func (s *SimpleProcessor) Enabled(context.Context, Record) bool {
return true

Check warning on line 40 in sdk/log/simple.go

View check run for this annotation

Codecov / codecov/patch

sdk/log/simple.go#L39-L40

Added lines #L39 - L40 were not covered by tests
}

// Shutdown shuts down the expoter.
func (s *SimpleProcessor) Shutdown(ctx context.Context) error {
// TODO (#5062): Implement.
Expand Down