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