| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package logtest // import "go.opentelemetry.io/otel/log/logtest" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"sync" |
| 9 |
"time" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/log" |
| 13 |
"go.opentelemetry.io/otel/log/embedded" |
| 14 |
) |
| 15 |
|
| 16 |
type enabledFn func(context.Context, log.EnabledParameters) bool |
| 17 |
|
| 18 |
var defaultEnabledFunc = func(context.Context, log.EnabledParameters) bool { |
| 19 |
return true |
| 20 |
} |
| 21 |
|
| 22 |
type config struct { |
| 23 |
enabledFn enabledFn |
| 24 |
} |
| 25 |
|
| 26 |
func newConfig(options []Option) config { |
| 27 |
var c config |
| 28 |
for _, opt := range options { |
| 29 |
c = opt.apply(c) |
| 30 |
} |
| 31 |
|
| 32 |
return c |
| 33 |
} |
| 34 |
|
| 35 |
// Option configures a [Recorder]. |
| 36 |
type Option interface { |
| 37 |
apply(config) config |
| 38 |
} |
| 39 |
|
| 40 |
type optFunc func(config) config |
| 41 |
|
| 42 |
func (f optFunc) apply(c config) config { return f(c) } |
| 43 |
|
| 44 |
// WithEnabledFunc allows configuring whether the [Recorder] is enabled for specific log entries or not. |
| 45 |
// |
| 46 |
// By default, the Recorder is enabled for every log entry. |
| 47 |
func WithEnabledFunc(fn func(context.Context, log.EnabledParameters) bool) Option { |
| 48 |
return optFunc(func(c config) config { |
| 49 |
c.enabledFn = fn |
| 50 |
return c |
| 51 |
}) |
| 52 |
} |
| 53 |
|
| 54 |
// NewRecorder returns a new [Recorder]. |
| 55 |
func NewRecorder(options ...Option) *Recorder { |
| 56 |
cfg := newConfig(options) |
| 57 |
|
| 58 |
return &Recorder{ |
| 59 |
enabledFn: cfg.enabledFn, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// Recording represents the recorded log records snapshot. |
| 64 |
type Recording map[Scope][]Record |
| 65 |
|
| 66 |
// Scope represents the instrumentation scope. |
| 67 |
type Scope struct { |
| 68 |
// Name is the name of the instrumentation scope. This should be the |
| 69 |
// Go package name of that scope. |
| 70 |
Name string |
| 71 |
// Version is the version of the instrumentation scope. |
| 72 |
Version string |
| 73 |
// SchemaURL of the telemetry emitted by the scope. |
| 74 |
SchemaURL string |
| 75 |
// Attributes of the telemetry emitted by the scope. |
| 76 |
Attributes attribute.Set |
| 77 |
} |
| 78 |
|
| 79 |
// Record represents the record alongside its context. |
| 80 |
type Record struct { |
| 81 |
// Ensure forward compatibility by explicitly making this not comparable. |
| 82 |
_ [0]func() |
| 83 |
|
| 84 |
Context context.Context |
| 85 |
EventName string |
| 86 |
Timestamp time.Time |
| 87 |
ObservedTimestamp time.Time |
| 88 |
Severity log.Severity |
| 89 |
SeverityText string |
| 90 |
Body log.Value |
| 91 |
Attributes []log.KeyValue |
| 92 |
} |
| 93 |
|
| 94 |
// Recorder stores all received log records in-memory. |
| 95 |
// Recorder implements [log.LoggerProvider]. |
| 96 |
type Recorder struct { |
| 97 |
// Ensure forward compatibility by explicitly making this not comparable. |
| 98 |
_ [0]func() |
| 99 |
|
| 100 |
embedded.LoggerProvider |
| 101 |
|
| 102 |
mu sync.Mutex |
| 103 |
loggers map[Scope]*logger |
| 104 |
|
| 105 |
// enabledFn decides whether the recorder should enable logging of a record or not |
| 106 |
enabledFn enabledFn |
| 107 |
} |
| 108 |
|
| 109 |
// Compile-time check Recorder implements log.LoggerProvider. |
| 110 |
var _ log.LoggerProvider = (*Recorder)(nil) |
| 111 |
|
| 112 |
// Clone returns a deep copy. |
| 113 |
func (a Record) Clone() Record { |
| 114 |
b := a |
| 115 |
attrs := make([]log.KeyValue, len(a.Attributes)) |
| 116 |
copy(attrs, a.Attributes) |
| 117 |
b.Attributes = attrs |
| 118 |
return b |
| 119 |
} |
| 120 |
|
| 121 |
// Logger returns a copy of Recorder as a [log.Logger] with the provided scope |
| 122 |
// information. |
| 123 |
func (r *Recorder) Logger(name string, opts ...log.LoggerOption) log.Logger { |
| 124 |
cfg := log.NewLoggerConfig(opts...) |
| 125 |
scope := Scope{ |
| 126 |
Name: name, |
| 127 |
Version: cfg.InstrumentationVersion(), |
| 128 |
SchemaURL: cfg.SchemaURL(), |
| 129 |
Attributes: cfg.InstrumentationAttributes(), |
| 130 |
} |
| 131 |
|
| 132 |
r.mu.Lock() |
| 133 |
defer r.mu.Unlock() |
| 134 |
|
| 135 |
if r.loggers == nil { |
| 136 |
r.loggers = make(map[Scope]*logger) |
| 137 |
} |
| 138 |
|
| 139 |
l, ok := r.loggers[scope] |
| 140 |
if ok { |
| 141 |
return l |
| 142 |
} |
| 143 |
l = &logger{ |
| 144 |
enabledFn: r.enabledFn, |
| 145 |
} |
| 146 |
r.loggers[scope] = l |
| 147 |
return l |
| 148 |
} |
| 149 |
|
| 150 |
// Reset clears the in-memory log records for all loggers. |
| 151 |
func (r *Recorder) Reset() { |
| 152 |
r.mu.Lock() |
| 153 |
defer r.mu.Unlock() |
| 154 |
|
| 155 |
for _, l := range r.loggers { |
| 156 |
l.Reset() |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
// Result returns a deep copy of the current in-memory recorded log records. |
| 161 |
func (r *Recorder) Result() Recording { |
| 162 |
r.mu.Lock() |
| 163 |
defer r.mu.Unlock() |
| 164 |
|
| 165 |
res := make(Recording, len(r.loggers)) |
| 166 |
for s, l := range r.loggers { |
| 167 |
func() { |
| 168 |
l.mu.Lock() |
| 169 |
defer l.mu.Unlock() |
| 170 |
if l.records == nil { |
| 171 |
res[s] = nil |
| 172 |
return |
| 173 |
} |
| 174 |
recs := make([]Record, len(l.records)) |
| 175 |
for i, r := range l.records { |
| 176 |
recs[i] = r.Clone() |
| 177 |
} |
| 178 |
res[s] = recs |
| 179 |
}() |
| 180 |
} |
| 181 |
return res |
| 182 |
} |
| 183 |
|
| 184 |
type logger struct { |
| 185 |
embedded.Logger |
| 186 |
|
| 187 |
mu sync.Mutex |
| 188 |
records []*Record |
| 189 |
|
| 190 |
// enabledFn decides whether the recorder should enable logging of a record or not. |
| 191 |
enabledFn enabledFn |
| 192 |
} |
| 193 |
|
| 194 |
// Enabled indicates whether a specific record should be stored. |
| 195 |
func (l *logger) Enabled(ctx context.Context, param log.EnabledParameters) bool { |
| 196 |
if l.enabledFn == nil { |
| 197 |
return defaultEnabledFunc(ctx, param) |
| 198 |
} |
| 199 |
|
| 200 |
return l.enabledFn(ctx, param) |
| 201 |
} |
| 202 |
|
| 203 |
// Emit stores the log record. |
| 204 |
func (l *logger) Emit(ctx context.Context, record log.Record) { |
| 205 |
l.mu.Lock() |
| 206 |
defer l.mu.Unlock() |
| 207 |
|
| 208 |
attrs := make([]log.KeyValue, 0, record.AttributesLen()) |
| 209 |
record.WalkAttributes(func(kv log.KeyValue) bool { |
| 210 |
attrs = append(attrs, kv) |
| 211 |
return true |
| 212 |
}) |
| 213 |
|
| 214 |
r := &Record{ |
| 215 |
Context: ctx, |
| 216 |
EventName: record.EventName(), |
| 217 |
Timestamp: record.Timestamp(), |
| 218 |
ObservedTimestamp: record.ObservedTimestamp(), |
| 219 |
Severity: record.Severity(), |
| 220 |
SeverityText: record.SeverityText(), |
| 221 |
Body: record.Body(), |
| 222 |
Attributes: attrs, |
| 223 |
} |
| 224 |
|
| 225 |
l.records = append(l.records, r) |
| 226 |
} |
| 227 |
|
| 228 |
// Reset clears the in-memory log records. |
| 229 |
func (l *logger) Reset() { |
| 230 |
l.mu.Lock() |
| 231 |
defer l.mu.Unlock() |
| 232 |
|
| 233 |
l.records = nil |
| 234 |
} |