| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package minsev provides an [log.Processor] that will not log any record with |
| 5 |
// a severity below a configured threshold. |
| 6 |
package minsev // import "go.opentelemetry.io/contrib/processors/minsev" |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/sdk/log" |
| 12 |
) |
| 13 |
|
| 14 |
// NewLogProcessor returns a new [LogProcessor] that wraps the downstream |
| 15 |
// [log.Processor]. |
| 16 |
// |
| 17 |
// severity reports the minimum record severity that will be logged. The |
| 18 |
// LogProcessor discards records with lower severities. If severity is nil, |
| 19 |
// SeverityInfo is used as a default. The LogProcessor calls severity.Severity |
| 20 |
// for each record processed or queried; to adjust the minimum level |
| 21 |
// dynamically, use a [SeverityVar]. |
| 22 |
// |
| 23 |
// If downstream is nil a default No-Op [log.Processor] is used. The returned |
| 24 |
// processor will not be enabled for nor emit any records. |
| 25 |
func NewLogProcessor(downstream log.Processor, severity Severitier) *LogProcessor { |
| 26 |
if downstream == nil { |
| 27 |
downstream = defaultProcessor |
| 28 |
} |
| 29 |
if severity == nil { |
| 30 |
severity = SeverityInfo |
| 31 |
} |
| 32 |
return &LogProcessor{ |
| 33 |
Processor: downstream, |
| 34 |
sev: severity, |
| 35 |
wrapped: downstream, |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// LogProcessor is an [log.Processor] implementation that wraps another |
| 40 |
// [log.Processor]. It will pass-through calls to OnEmit and Enabled for |
| 41 |
// records with severity greater than or equal to a minimum. All other method |
| 42 |
// calls are passed to the wrapped [log.Processor]. |
| 43 |
// |
| 44 |
// If the wrapped [log.Processor] is nil, calls to the LogProcessor methods |
| 45 |
// will panic. Use [NewLogProcessor] to create a new LogProcessor that ensures |
| 46 |
// no panics. |
| 47 |
type LogProcessor struct { |
| 48 |
log.Processor |
| 49 |
|
| 50 |
wrapped log.Processor |
| 51 |
sev Severitier |
| 52 |
} |
| 53 |
|
| 54 |
// Compile time assertion that LogProcessor implements log.Processor and log.FilterProcessor. |
| 55 |
var _ log.Processor = (*LogProcessor)(nil) |
| 56 |
|
| 57 |
// OnEmit passes ctx and r to the [log.Processor] that p wraps if the severity |
| 58 |
// of record is greater than or equal to p.Minimum. Otherwise, record is |
| 59 |
// dropped. |
| 60 |
func (p *LogProcessor) OnEmit(ctx context.Context, record *log.Record) error { |
| 61 |
if record.Severity() >= p.sev.Severity() { |
| 62 |
return p.Processor.OnEmit(ctx, record) |
| 63 |
} |
| 64 |
return nil |
| 65 |
} |
| 66 |
|
| 67 |
// Enabled returns if the [log.Processor] that p wraps is enabled if the |
| 68 |
// severity of param is greater than or equal to p.Minimum. Otherwise false is |
| 69 |
// returned. |
| 70 |
func (p *LogProcessor) Enabled(ctx context.Context, param log.EnabledParameters) bool { |
| 71 |
sev := param.Severity |
| 72 |
if p.wrapped != nil { |
| 73 |
return sev >= p.sev.Severity() && |
| 74 |
p.wrapped.Enabled(ctx, param) |
| 75 |
} |
| 76 |
return sev >= p.sev.Severity() |
| 77 |
} |
| 78 |
|
| 79 |
var defaultProcessor = noopProcessor{} |
| 80 |
|
| 81 |
type noopProcessor struct{} |
| 82 |
|
| 83 |
func (noopProcessor) OnEmit(context.Context, *log.Record) error { return nil } |
| 84 |
func (noopProcessor) Enabled(context.Context, log.EnabledParameters) bool { return false } |
| 85 |
func (noopProcessor) Shutdown(context.Context) error { return nil } |
| 86 |
func (noopProcessor) ForceFlush(context.Context) error { return nil } |