| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"io" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
|
| 17 |
"go.opentelemetry.io/otel" |
| 18 |
"go.opentelemetry.io/otel/attribute" |
| 19 |
"go.opentelemetry.io/otel/sdk" |
| 20 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 21 |
"go.opentelemetry.io/otel/sdk/log" |
| 22 |
"go.opentelemetry.io/otel/sdk/log/internal/observ" |
| 23 |
"go.opentelemetry.io/otel/sdk/metric" |
| 24 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 25 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 26 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 27 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 28 |
) |
| 29 |
|
| 30 |
type exporter struct { |
| 31 |
records []log.Record |
| 32 |
|
| 33 |
exportCalled bool |
| 34 |
shutdownCalled bool |
| 35 |
forceFlushCalled bool |
| 36 |
} |
| 37 |
|
| 38 |
func (e *exporter) Export(_ context.Context, r []log.Record) error { |
| 39 |
e.records = r |
| 40 |
e.exportCalled = true |
| 41 |
return nil |
| 42 |
} |
| 43 |
|
| 44 |
func (e *exporter) Shutdown(context.Context) error { |
| 45 |
e.shutdownCalled = true |
| 46 |
return nil |
| 47 |
} |
| 48 |
|
| 49 |
func (e *exporter) ForceFlush(context.Context) error { |
| 50 |
e.forceFlushCalled = true |
| 51 |
return nil |
| 52 |
} |
| 53 |
|
| 54 |
var _ log.Exporter = (*failingTestExporter)(nil) |
| 55 |
|
| 56 |
type failingTestExporter struct { |
| 57 |
exporter |
| 58 |
} |
| 59 |
|
| 60 |
func (f *failingTestExporter) Export(ctx context.Context, r []log.Record) error { |
| 61 |
_ = f.exporter.Export(ctx, r) |
| 62 |
return assert.AnError |
| 63 |
} |
| 64 |
|
| 65 |
func TestSimpleProcessorEnabled(t *testing.T) { |
| 66 |
e := log.NewSimpleProcessor(nil) |
| 67 |
enabled := e.Enabled(t.Context(), log.EnabledParameters{}) |
| 68 |
assert.True(t, enabled, "Enabled should return true") |
| 69 |
} |
| 70 |
|
| 71 |
func TestSimpleProcessorOnEmit(t *testing.T) { |
| 72 |
e := new(exporter) |
| 73 |
s := log.NewSimpleProcessor(e) |
| 74 |
|
| 75 |
r := new(log.Record) |
| 76 |
r.SetSeverityText("test") |
| 77 |
_ = s.OnEmit(t.Context(), r) |
| 78 |
|
| 79 |
require.True(t, e.exportCalled, "exporter Export not called") |
| 80 |
assert.Equal(t, []log.Record{*r}, e.records) |
| 81 |
} |
| 82 |
|
| 83 |
func TestSimpleProcessorShutdown(t *testing.T) { |
| 84 |
e := new(exporter) |
| 85 |
s := log.NewSimpleProcessor(e) |
| 86 |
_ = s.Shutdown(t.Context()) |
| 87 |
require.True(t, e.shutdownCalled, "exporter Shutdown not called") |
| 88 |
} |
| 89 |
|
| 90 |
func TestSimpleProcessorForceFlush(t *testing.T) { |
| 91 |
e := new(exporter) |
| 92 |
s := log.NewSimpleProcessor(e) |
| 93 |
_ = s.ForceFlush(t.Context()) |
| 94 |
require.True(t, e.forceFlushCalled, "exporter ForceFlush not called") |
| 95 |
} |
| 96 |
|
| 97 |
type writerExporter struct { |
| 98 |
io.Writer |
| 99 |
} |
| 100 |
|
| 101 |
func (e *writerExporter) Export(_ context.Context, records []log.Record) error { |
| 102 |
for _, r := range records { |
| 103 |
_, _ = io.WriteString(e.Writer, r.Body().String()) |
| 104 |
} |
| 105 |
return nil |
| 106 |
} |
| 107 |
|
| 108 |
func (*writerExporter) Shutdown(context.Context) error { |
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
func (*writerExporter) ForceFlush(context.Context) error { |
| 113 |
return nil |
| 114 |
} |
| 115 |
|
| 116 |
func TestSimpleProcessorEmpty(t *testing.T) { |
| 117 |
assert.NotPanics(t, func() { |
| 118 |
var s log.SimpleProcessor |
| 119 |
ctx := t.Context() |
| 120 |
record := new(log.Record) |
| 121 |
assert.NoError(t, s.OnEmit(ctx, record), "OnEmit") |
| 122 |
assert.NoError(t, s.ForceFlush(ctx), "ForceFlush") |
| 123 |
assert.NoError(t, s.Shutdown(ctx), "Shutdown") |
| 124 |
}) |
| 125 |
} |
| 126 |
|
| 127 |
func TestSimpleProcessorConcurrentSafe(t *testing.T) { |
| 128 |
const goRoutineN = 10 |
| 129 |
|
| 130 |
var wg sync.WaitGroup |
| 131 |
wg.Add(goRoutineN) |
| 132 |
|
| 133 |
r := new(log.Record) |
| 134 |
r.SetSeverityText("test") |
| 135 |
ctx := t.Context() |
| 136 |
e := &writerExporter{new(strings.Builder)} |
| 137 |
s := log.NewSimpleProcessor(e) |
| 138 |
for range goRoutineN { |
| 139 |
go func() { |
| 140 |
defer wg.Done() |
| 141 |
|
| 142 |
_ = s.OnEmit(ctx, r) |
| 143 |
_ = s.Shutdown(ctx) |
| 144 |
_ = s.ForceFlush(ctx) |
| 145 |
}() |
| 146 |
} |
| 147 |
|
| 148 |
wg.Wait() |
| 149 |
} |
| 150 |
|
| 151 |
func BenchmarkSimpleProcessorOnEmit(b *testing.B) { |
| 152 |
r := new(log.Record) |
| 153 |
r.SetSeverityText("test") |
| 154 |
ctx := b.Context() |
| 155 |
s := log.NewSimpleProcessor(nil) |
| 156 |
|
| 157 |
b.ReportAllocs() |
| 158 |
b.ResetTimer() |
| 159 |
b.RunParallel(func(pb *testing.PB) { |
| 160 |
var out error |
| 161 |
|
| 162 |
for pb.Next() { |
| 163 |
out = s.OnEmit(ctx, r) |
| 164 |
} |
| 165 |
|
| 166 |
_ = out |
| 167 |
}) |
| 168 |
} |
| 169 |
|
| 170 |
func BenchmarkSimpleProcessorObservability(b *testing.B) { |
| 171 |
run := func(b *testing.B) { |
| 172 |
slp := log.NewSimpleProcessor(&failingTestExporter{exporter: exporter{}}) |
| 173 |
record := new(log.Record) |
| 174 |
record.SetSeverityText("test") |
| 175 |
|
| 176 |
ctx := b.Context() |
| 177 |
b.ReportAllocs() |
| 178 |
b.ResetTimer() |
| 179 |
|
| 180 |
var err error |
| 181 |
for b.Loop() { |
| 182 |
err = slp.OnEmit(ctx, record) |
| 183 |
} |
| 184 |
_ = err |
| 185 |
} |
| 186 |
|
| 187 |
b.Run("Observability", func(b *testing.B) { |
| 188 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 189 |
run(b) |
| 190 |
}) |
| 191 |
b.Run("NoObservability", run) |
| 192 |
} |
| 193 |
|
| 194 |
func TestSimpleLogProcessorObservability(t *testing.T) { |
| 195 |
testcases := []struct { |
| 196 |
name string |
| 197 |
enabled bool |
| 198 |
exporter log.Exporter |
| 199 |
wantErr error |
| 200 |
assertMetrics func(t *testing.T, rm metricdata.ResourceMetrics) |
| 201 |
}{ |
| 202 |
{ |
| 203 |
name: "disabled", |
| 204 |
enabled: false, |
| 205 |
exporter: new(exporter), |
| 206 |
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 207 |
assert.Empty(t, rm.ScopeMetrics) |
| 208 |
}, |
| 209 |
}, |
| 210 |
{ |
| 211 |
name: "enabled", |
| 212 |
enabled: true, |
| 213 |
exporter: new(exporter), |
| 214 |
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 215 |
assert.Len(t, rm.ScopeMetrics, 1) |
| 216 |
sm := rm.ScopeMetrics[0] |
| 217 |
|
| 218 |
p := otelconv.SDKProcessorLogProcessed{} |
| 219 |
|
| 220 |
want := metricdata.ScopeMetrics{ |
| 221 |
Scope: instrumentation.Scope{ |
| 222 |
Name: observ.ScopeName, |
| 223 |
Version: sdk.Version(), |
| 224 |
SchemaURL: semconv.SchemaURL, |
| 225 |
}, |
| 226 |
Metrics: []metricdata.Metrics{ |
| 227 |
{ |
| 228 |
Name: p.Name(), |
| 229 |
Description: p.Description(), |
| 230 |
Unit: p.Unit(), |
| 231 |
Data: metricdata.Sum[int64]{ |
| 232 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 233 |
{ |
| 234 |
Value: 1, |
| 235 |
Attributes: attribute.NewSet( |
| 236 |
observ.GetSLPComponentName(0), |
| 237 |
semconv.OTelComponentTypeKey.String( |
| 238 |
string(otelconv.ComponentTypeSimpleLogProcessor), |
| 239 |
), |
| 240 |
), |
| 241 |
}, |
| 242 |
}, |
| 243 |
Temporality: metricdata.CumulativeTemporality, |
| 244 |
IsMonotonic: true, |
| 245 |
}, |
| 246 |
}, |
| 247 |
}, |
| 248 |
} |
| 249 |
|
| 250 |
metricdatatest.AssertEqual( |
| 251 |
t, |
| 252 |
want, |
| 253 |
sm, |
| 254 |
metricdatatest.IgnoreExemplars(), |
| 255 |
metricdatatest.IgnoreTimestamp(), |
| 256 |
) |
| 257 |
}, |
| 258 |
}, |
| 259 |
{ |
| 260 |
name: "Enable Exporter error", |
| 261 |
enabled: true, |
| 262 |
wantErr: assert.AnError, |
| 263 |
exporter: &failingTestExporter{ |
| 264 |
exporter: exporter{}, |
| 265 |
}, |
| 266 |
assertMetrics: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 267 |
assert.Len(t, rm.ScopeMetrics, 1) |
| 268 |
sm := rm.ScopeMetrics[0] |
| 269 |
p := otelconv.SDKProcessorLogProcessed{} |
| 270 |
|
| 271 |
want := metricdata.ScopeMetrics{ |
| 272 |
Scope: instrumentation.Scope{ |
| 273 |
Name: "go.opentelemetry.io/otel/sdk/log/internal/observ", |
| 274 |
Version: sdk.Version(), |
| 275 |
SchemaURL: semconv.SchemaURL, |
| 276 |
}, |
| 277 |
Metrics: []metricdata.Metrics{ |
| 278 |
{ |
| 279 |
Name: p.Name(), |
| 280 |
Description: p.Description(), |
| 281 |
Unit: p.Unit(), |
| 282 |
Data: metricdata.Sum[int64]{ |
| 283 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 284 |
{ |
| 285 |
Value: 1, |
| 286 |
Attributes: attribute.NewSet( |
| 287 |
observ.GetSLPComponentName(0), |
| 288 |
semconv.OTelComponentTypeKey.String( |
| 289 |
string(otelconv.ComponentTypeSimpleLogProcessor), |
| 290 |
), |
| 291 |
semconv.ErrorTypeKey.String("*errors.errorString"), |
| 292 |
), |
| 293 |
}, |
| 294 |
}, |
| 295 |
Temporality: metricdata.CumulativeTemporality, |
| 296 |
IsMonotonic: true, |
| 297 |
}, |
| 298 |
}, |
| 299 |
}, |
| 300 |
} |
| 301 |
|
| 302 |
metricdatatest.AssertEqual( |
| 303 |
t, |
| 304 |
want, |
| 305 |
sm, |
| 306 |
metricdatatest.IgnoreTimestamp(), |
| 307 |
metricdatatest.IgnoreExemplars(), |
| 308 |
) |
| 309 |
}, |
| 310 |
}, |
| 311 |
} |
| 312 |
|
| 313 |
for _, tc := range testcases { |
| 314 |
t.Run(tc.name, func(t *testing.T) { |
| 315 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", strconv.FormatBool(tc.enabled)) |
| 316 |
|
| 317 |
original := otel.GetMeterProvider() |
| 318 |
t.Cleanup(func() { |
| 319 |
otel.SetMeterProvider(original) |
| 320 |
}) |
| 321 |
|
| 322 |
r := metric.NewManualReader() |
| 323 |
mp := metric.NewMeterProvider(metric.WithReader(r)) |
| 324 |
otel.SetMeterProvider(mp) |
| 325 |
|
| 326 |
slp := log.NewSimpleProcessor(tc.exporter) |
| 327 |
record := new(log.Record) |
| 328 |
record.SetSeverityText("test") |
| 329 |
err := slp.OnEmit(t.Context(), record) |
| 330 |
require.ErrorIs(t, err, tc.wantErr) |
| 331 |
var rm metricdata.ResourceMetrics |
| 332 |
require.NoError(t, r.Collect(t.Context(), &rm)) |
| 333 |
tc.assertMetrics(t, rm) |
| 334 |
observ.SetSimpleProcessorID(0) |
| 335 |
}) |
| 336 |
} |
| 337 |
} |