| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package minsev |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
api "go.opentelemetry.io/otel/log" |
| 12 |
"go.opentelemetry.io/otel/sdk/log" |
| 13 |
) |
| 14 |
|
| 15 |
var severities = []api.Severity{ |
| 16 |
api.SeverityTrace, api.SeverityTrace1, api.SeverityTrace2, api.SeverityTrace3, api.SeverityTrace4, |
| 17 |
api.SeverityDebug, api.SeverityDebug1, api.SeverityDebug2, api.SeverityDebug3, api.SeverityDebug4, |
| 18 |
api.SeverityInfo, api.SeverityInfo1, api.SeverityInfo2, api.SeverityInfo3, api.SeverityInfo4, |
| 19 |
api.SeverityWarn, api.SeverityWarn1, api.SeverityWarn2, api.SeverityWarn3, api.SeverityWarn4, |
| 20 |
api.SeverityError, api.SeverityError1, api.SeverityError2, api.SeverityError3, api.SeverityError4, |
| 21 |
api.SeverityFatal, api.SeverityFatal1, api.SeverityFatal2, api.SeverityFatal3, api.SeverityFatal4, |
| 22 |
} |
| 23 |
|
| 24 |
type apiSev api.Severity |
| 25 |
|
| 26 |
func (s apiSev) Severity() api.Severity { return api.Severity(s) } |
| 27 |
|
| 28 |
type emitArgs struct { |
| 29 |
Ctx context.Context |
| 30 |
Record *log.Record |
| 31 |
} |
| 32 |
|
| 33 |
type enabledArgs struct { |
| 34 |
Ctx context.Context |
| 35 |
Param log.EnabledParameters |
| 36 |
} |
| 37 |
|
| 38 |
type processor struct { |
| 39 |
ReturnErr error |
| 40 |
|
| 41 |
OnEmitCalls []emitArgs |
| 42 |
EnabledCalls []enabledArgs |
| 43 |
ForceFlushCalls []context.Context |
| 44 |
ShutdownCalls []context.Context |
| 45 |
} |
| 46 |
|
| 47 |
// Compile time assertion that processor implements log.Processor and log.FilterProcessor. |
| 48 |
var _ log.Processor = (*processor)(nil) |
| 49 |
|
| 50 |
func (p *processor) OnEmit(ctx context.Context, r *log.Record) error { |
| 51 |
p.OnEmitCalls = append(p.OnEmitCalls, emitArgs{ctx, r}) |
| 52 |
return p.ReturnErr |
| 53 |
} |
| 54 |
|
| 55 |
func (p *processor) Enabled(ctx context.Context, param log.EnabledParameters) bool { |
| 56 |
p.EnabledCalls = append(p.EnabledCalls, enabledArgs{ctx, param}) |
| 57 |
return true |
| 58 |
} |
| 59 |
|
| 60 |
func (p *processor) Shutdown(ctx context.Context) error { |
| 61 |
p.ShutdownCalls = append(p.ShutdownCalls, ctx) |
| 62 |
return p.ReturnErr |
| 63 |
} |
| 64 |
|
| 65 |
func (p *processor) ForceFlush(ctx context.Context) error { |
| 66 |
p.ForceFlushCalls = append(p.ForceFlushCalls, ctx) |
| 67 |
return p.ReturnErr |
| 68 |
} |
| 69 |
|
| 70 |
func (p *processor) Reset() { |
| 71 |
p.OnEmitCalls = p.OnEmitCalls[:0] |
| 72 |
p.EnabledCalls = p.EnabledCalls[:0] |
| 73 |
p.ShutdownCalls = p.ShutdownCalls[:0] |
| 74 |
p.ForceFlushCalls = p.ForceFlushCalls[:0] |
| 75 |
} |
| 76 |
|
| 77 |
func TestLogProcessorDynamicSeverity(t *testing.T) { |
| 78 |
sev := new(SeverityVar) |
| 79 |
wrapped := new(processor) |
| 80 |
p := NewLogProcessor(wrapped, sev) |
| 81 |
|
| 82 |
ctx := t.Context() |
| 83 |
params := log.EnabledParameters{Severity: api.SeverityDebug} |
| 84 |
assert.False(t, p.Enabled(ctx, params), api.SeverityDebug.String()) |
| 85 |
|
| 86 |
params.Severity = api.SeverityInfo |
| 87 |
assert.True(t, p.Enabled(ctx, params), api.SeverityInfo.String()) |
| 88 |
|
| 89 |
sev.Set(SeverityError) |
| 90 |
|
| 91 |
params.Severity = api.SeverityInfo |
| 92 |
assert.False(t, p.Enabled(ctx, params), api.SeverityInfo.String()) |
| 93 |
|
| 94 |
params.Severity = api.SeverityError |
| 95 |
assert.True(t, p.Enabled(ctx, params), api.SeverityError.String()) |
| 96 |
} |
| 97 |
|
| 98 |
func TestLogProcessorOnEmit(t *testing.T) { |
| 99 |
t.Run("Passthrough", func(t *testing.T) { |
| 100 |
wrapped := &processor{ReturnErr: assert.AnError} |
| 101 |
|
| 102 |
p := NewLogProcessor(wrapped, SeverityTrace1) |
| 103 |
ctx := t.Context() |
| 104 |
r := &log.Record{} |
| 105 |
for _, sev := range severities { |
| 106 |
r.SetSeverity(sev) |
| 107 |
assert.ErrorIs(t, p.OnEmit(ctx, r), assert.AnError, sev.String()) |
| 108 |
|
| 109 |
if assert.Lenf(t, wrapped.OnEmitCalls, 1, "Record with severity %s not passed-through", sev) { |
| 110 |
assert.Equal(t, ctx, wrapped.OnEmitCalls[0].Ctx, sev.String()) |
| 111 |
assert.Equal(t, r, wrapped.OnEmitCalls[0].Record, sev.String()) |
| 112 |
} |
| 113 |
wrapped.Reset() |
| 114 |
} |
| 115 |
}) |
| 116 |
|
| 117 |
t.Run("Dropped", func(t *testing.T) { |
| 118 |
wrapped := &processor{ReturnErr: assert.AnError} |
| 119 |
|
| 120 |
p := NewLogProcessor(wrapped, apiSev(api.SeverityFatal4+1)) |
| 121 |
ctx := t.Context() |
| 122 |
r := &log.Record{} |
| 123 |
for _, sev := range severities { |
| 124 |
r.SetSeverity(sev) |
| 125 |
assert.NoError(t, p.OnEmit(ctx, r), sev.String()) |
| 126 |
|
| 127 |
if !assert.Emptyf(t, wrapped.OnEmitCalls, "Record with severity %s passed-through", sev) { |
| 128 |
wrapped.Reset() |
| 129 |
} |
| 130 |
} |
| 131 |
}) |
| 132 |
} |
| 133 |
|
| 134 |
func TestLogProcessorEnabled(t *testing.T) { |
| 135 |
t.Run("Passthrough", func(t *testing.T) { |
| 136 |
wrapped := &processor{} |
| 137 |
|
| 138 |
p := NewLogProcessor(wrapped, SeverityTrace1) |
| 139 |
ctx := t.Context() |
| 140 |
param := log.EnabledParameters{} |
| 141 |
for _, sev := range severities { |
| 142 |
param.Severity = sev |
| 143 |
assert.True(t, p.Enabled(ctx, param), sev.String()) |
| 144 |
|
| 145 |
if assert.Lenf(t, wrapped.EnabledCalls, 1, "Record with severity %s not passed-through", sev) { |
| 146 |
assert.Equal(t, ctx, wrapped.EnabledCalls[0].Ctx, sev.String()) |
| 147 |
assert.Equal(t, param, wrapped.EnabledCalls[0].Param, sev.String()) |
| 148 |
} |
| 149 |
wrapped.Reset() |
| 150 |
} |
| 151 |
}) |
| 152 |
|
| 153 |
t.Run("NotEnabled", func(t *testing.T) { |
| 154 |
wrapped := &processor{} |
| 155 |
|
| 156 |
p := NewLogProcessor(wrapped, apiSev(api.SeverityFatal4+1)) |
| 157 |
ctx := t.Context() |
| 158 |
param := log.EnabledParameters{} |
| 159 |
for _, sev := range severities { |
| 160 |
param.Severity = sev |
| 161 |
assert.False(t, p.Enabled(ctx, param), sev.String()) |
| 162 |
|
| 163 |
if !assert.Emptyf(t, wrapped.EnabledCalls, "Record with severity %s passed-through", sev) { |
| 164 |
wrapped.Reset() |
| 165 |
} |
| 166 |
} |
| 167 |
}) |
| 168 |
} |
| 169 |
|
| 170 |
func TestLogProcessorForceFlushPassthrough(t *testing.T) { |
| 171 |
wrapped := &processor{ReturnErr: assert.AnError} |
| 172 |
|
| 173 |
p := NewLogProcessor(wrapped, SeverityTrace1) |
| 174 |
ctx := t.Context() |
| 175 |
assert.ErrorIs(t, p.ForceFlush(ctx), assert.AnError) |
| 176 |
assert.Len(t, wrapped.ForceFlushCalls, 1, "ForceFlush not passed-through") |
| 177 |
} |
| 178 |
|
| 179 |
func TestLogProcessorShutdownPassthrough(t *testing.T) { |
| 180 |
wrapped := &processor{ReturnErr: assert.AnError} |
| 181 |
|
| 182 |
p := NewLogProcessor(wrapped, SeverityTrace1) |
| 183 |
ctx := t.Context() |
| 184 |
assert.ErrorIs(t, p.Shutdown(ctx), assert.AnError) |
| 185 |
assert.Len(t, wrapped.ShutdownCalls, 1, "Shutdown not passed-through") |
| 186 |
} |
| 187 |
|
| 188 |
func TestLogProcessorNilSeverity(t *testing.T) { |
| 189 |
p := NewLogProcessor(nil, nil) |
| 190 |
assert.Equal(t, SeverityInfo, p.sev.(Severity)) |
| 191 |
} |
| 192 |
|
| 193 |
func TestLogProcessorNilDownstream(t *testing.T) { |
| 194 |
p := NewLogProcessor(nil, SeverityTrace1) |
| 195 |
ctx := t.Context() |
| 196 |
r := new(log.Record) |
| 197 |
r.SetSeverity(api.SeverityTrace1) |
| 198 |
param := log.EnabledParameters{Severity: api.SeverityTrace1} |
| 199 |
assert.NotPanics(t, func() { |
| 200 |
assert.NoError(t, p.OnEmit(ctx, r)) |
| 201 |
assert.False(t, p.Enabled(ctx, param)) |
| 202 |
assert.NoError(t, p.ForceFlush(ctx)) |
| 203 |
assert.NoError(t, p.Shutdown(ctx)) |
| 204 |
}) |
| 205 |
} |
| 206 |
|
| 207 |
func BenchmarkLogProcessor(b *testing.B) { |
| 208 |
r := new(log.Record) |
| 209 |
r.SetSeverity(api.SeverityTrace) |
| 210 |
param := log.EnabledParameters{Severity: api.SeverityTrace} |
| 211 |
ctx := b.Context() |
| 212 |
|
| 213 |
run := func(p log.Processor) func(b *testing.B) { |
| 214 |
return func(b *testing.B) { |
| 215 |
var err error |
| 216 |
var enabled bool |
| 217 |
b.ReportAllocs() |
| 218 |
for range b.N { |
| 219 |
enabled = p.Enabled(ctx, param) |
| 220 |
err = p.OnEmit(ctx, r) |
| 221 |
} |
| 222 |
|
| 223 |
_, _ = err, enabled |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
b.Run("Base", run(defaultProcessor)) |
| 228 |
b.Run("Enabled", run(NewLogProcessor(nil, SeverityTrace))) |
| 229 |
b.Run("Disabled", run(NewLogProcessor(nil, SeverityDebug))) |
| 230 |
} |