| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/log" |
| 14 |
) |
| 15 |
|
| 16 |
type mockDelayExporter struct{} |
| 17 |
|
| 18 |
func (mockDelayExporter) Export(context.Context, []Record) error { |
| 19 |
time.Sleep(time.Millisecond * 5) |
| 20 |
return nil |
| 21 |
} |
| 22 |
|
| 23 |
func (mockDelayExporter) Shutdown(context.Context) error { return nil } |
| 24 |
|
| 25 |
func (mockDelayExporter) ForceFlush(context.Context) error { return nil } |
| 26 |
|
| 27 |
func BenchmarkProcessor(b *testing.B) { |
| 28 |
for _, tc := range []struct { |
| 29 |
name string |
| 30 |
f func() []LoggerProviderOption |
| 31 |
}{ |
| 32 |
{ |
| 33 |
name: "Simple", |
| 34 |
f: func() []LoggerProviderOption { |
| 35 |
return []LoggerProviderOption{WithProcessor(NewSimpleProcessor(noopExporter{}))} |
| 36 |
}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "Batch", |
| 40 |
f: func() []LoggerProviderOption { |
| 41 |
return []LoggerProviderOption{WithProcessor(NewBatchProcessor(noopExporter{}))} |
| 42 |
}, |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "BatchSimulateExport", |
| 46 |
f: func() []LoggerProviderOption { |
| 47 |
return []LoggerProviderOption{WithProcessor(NewBatchProcessor(mockDelayExporter{}))} |
| 48 |
}, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "SetTimestampSimple", |
| 52 |
f: func() []LoggerProviderOption { |
| 53 |
return []LoggerProviderOption{ |
| 54 |
WithProcessor(timestampProcessor{}), |
| 55 |
WithProcessor(NewSimpleProcessor(noopExporter{})), |
| 56 |
} |
| 57 |
}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "SetTimestampBatch", |
| 61 |
f: func() []LoggerProviderOption { |
| 62 |
return []LoggerProviderOption{ |
| 63 |
WithProcessor(timestampProcessor{}), |
| 64 |
WithProcessor(NewBatchProcessor(noopExporter{})), |
| 65 |
} |
| 66 |
}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "AddAttributesSimple", |
| 70 |
f: func() []LoggerProviderOption { |
| 71 |
return []LoggerProviderOption{ |
| 72 |
WithProcessor(attrAddProcessor{}), |
| 73 |
WithProcessor(NewSimpleProcessor(noopExporter{})), |
| 74 |
} |
| 75 |
}, |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "AddAttributesBatch", |
| 79 |
f: func() []LoggerProviderOption { |
| 80 |
return []LoggerProviderOption{ |
| 81 |
WithProcessor(attrAddProcessor{}), |
| 82 |
WithProcessor(NewBatchProcessor(noopExporter{})), |
| 83 |
} |
| 84 |
}, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "SetAttributesSimple", |
| 88 |
f: func() []LoggerProviderOption { |
| 89 |
return []LoggerProviderOption{ |
| 90 |
WithProcessor(attrSetDecorator{}), |
| 91 |
WithProcessor(NewSimpleProcessor(noopExporter{})), |
| 92 |
} |
| 93 |
}, |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "SetAttributesBatch", |
| 97 |
f: func() []LoggerProviderOption { |
| 98 |
return []LoggerProviderOption{ |
| 99 |
WithProcessor(attrSetDecorator{}), |
| 100 |
WithProcessor(NewBatchProcessor(noopExporter{})), |
| 101 |
} |
| 102 |
}, |
| 103 |
}, |
| 104 |
} { |
| 105 |
b.Run(tc.name, func(b *testing.B) { |
| 106 |
provider := NewLoggerProvider(tc.f()...) |
| 107 |
b.Cleanup(func() { |
| 108 |
//nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 109 |
assert.NoError(b, provider.Shutdown(context.Background())) |
| 110 |
}) |
| 111 |
logger := provider.Logger(b.Name()) |
| 112 |
|
| 113 |
b.ReportAllocs() |
| 114 |
b.RunParallel(func(pb *testing.PB) { |
| 115 |
for pb.Next() { |
| 116 |
r := log.Record{} |
| 117 |
r.SetBody(log.StringValue("message")) |
| 118 |
r.SetSeverity(log.SeverityInfo) |
| 119 |
r.AddAttributes( |
| 120 |
log.String("foo", "bar"), |
| 121 |
log.Float64("float", 3.14), |
| 122 |
log.Int("int", 123), |
| 123 |
log.Bool("bool", true), |
| 124 |
) |
| 125 |
logger.Emit(b.Context(), r) |
| 126 |
} |
| 127 |
}) |
| 128 |
}) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
type timestampProcessor struct{} |
| 133 |
|
| 134 |
func (timestampProcessor) Enabled(context.Context, EnabledParameters) bool { |
| 135 |
return true |
| 136 |
} |
| 137 |
|
| 138 |
func (timestampProcessor) OnEmit(_ context.Context, r *Record) error { |
| 139 |
r.SetObservedTimestamp(time.Date(1988, time.November, 17, 0, 0, 0, 0, time.UTC)) |
| 140 |
return nil |
| 141 |
} |
| 142 |
|
| 143 |
func (timestampProcessor) Shutdown(context.Context) error { |
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
func (timestampProcessor) ForceFlush(context.Context) error { |
| 148 |
return nil |
| 149 |
} |
| 150 |
|
| 151 |
type attrAddProcessor struct{} |
| 152 |
|
| 153 |
func (attrAddProcessor) Enabled(context.Context, EnabledParameters) bool { |
| 154 |
return true |
| 155 |
} |
| 156 |
|
| 157 |
func (attrAddProcessor) OnEmit(_ context.Context, r *Record) error { |
| 158 |
r.AddAttributes(log.String("add", "me")) |
| 159 |
return nil |
| 160 |
} |
| 161 |
|
| 162 |
func (attrAddProcessor) Shutdown(context.Context) error { |
| 163 |
return nil |
| 164 |
} |
| 165 |
|
| 166 |
func (attrAddProcessor) ForceFlush(context.Context) error { |
| 167 |
return nil |
| 168 |
} |
| 169 |
|
| 170 |
type attrSetDecorator struct{} |
| 171 |
|
| 172 |
func (attrSetDecorator) Enabled(context.Context, EnabledParameters) bool { |
| 173 |
return true |
| 174 |
} |
| 175 |
|
| 176 |
func (attrSetDecorator) OnEmit(_ context.Context, r *Record) error { |
| 177 |
r.SetAttributes(log.String("replace", "me")) |
| 178 |
return nil |
| 179 |
} |
| 180 |
|
| 181 |
func (attrSetDecorator) Shutdown(context.Context) error { |
| 182 |
return nil |
| 183 |
} |
| 184 |
|
| 185 |
func (attrSetDecorator) ForceFlush(context.Context) error { |
| 186 |
return nil |
| 187 |
} |