| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package processorhelper |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"sync" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 16 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 17 |
|
| 18 |
"go.opentelemetry.io/collector/component" |
| 19 |
"go.opentelemetry.io/collector/component/componenttest" |
| 20 |
"go.opentelemetry.io/collector/consumer" |
| 21 |
"go.opentelemetry.io/collector/consumer/consumertest" |
| 22 |
"go.opentelemetry.io/collector/pdata/plog" |
| 23 |
"go.opentelemetry.io/collector/processor" |
| 24 |
"go.opentelemetry.io/collector/processor/processorhelper/internal/metadatatest" |
| 25 |
"go.opentelemetry.io/collector/processor/processortest" |
| 26 |
) |
| 27 |
|
| 28 |
var testLogsCfg = struct{}{} |
| 29 |
|
| 30 |
func TestNewLogs(t *testing.T) { |
| 31 |
lp, err := NewLogs(context.Background(), processortest.NewNopSettings(processortest.NopType), &testLogsCfg, consumertest.NewNop(), newTestLProcessor(nil)) |
| 32 |
require.NoError(t, err) |
| 33 |
|
| 34 |
assert.True(t, lp.Capabilities().MutatesData) |
| 35 |
assert.NoError(t, lp.Start(context.Background(), componenttest.NewNopHost())) |
| 36 |
assert.NoError(t, lp.ConsumeLogs(context.Background(), plog.NewLogs())) |
| 37 |
assert.NoError(t, lp.Shutdown(context.Background())) |
| 38 |
} |
| 39 |
|
| 40 |
func TestNewLogs_WithOptions(t *testing.T) { |
| 41 |
want := errors.New("my_error") |
| 42 |
lp, err := NewLogs(context.Background(), processortest.NewNopSettings(processortest.NopType), &testLogsCfg, consumertest.NewNop(), newTestLProcessor(nil), |
| 43 |
WithStart(func(context.Context, component.Host) error { return want }), |
| 44 |
WithShutdown(func(context.Context) error { return want }), |
| 45 |
WithCapabilities(consumer.Capabilities{MutatesData: false})) |
| 46 |
require.NoError(t, err) |
| 47 |
|
| 48 |
assert.Equal(t, want, lp.Start(context.Background(), componenttest.NewNopHost())) |
| 49 |
assert.Equal(t, want, lp.Shutdown(context.Background())) |
| 50 |
assert.False(t, lp.Capabilities().MutatesData) |
| 51 |
} |
| 52 |
|
| 53 |
func TestNewLogs_NilRequiredFields(t *testing.T) { |
| 54 |
_, err := NewLogs(context.Background(), processortest.NewNopSettings(processortest.NopType), &testLogsCfg, consumertest.NewNop(), nil) |
| 55 |
assert.Error(t, err) |
| 56 |
} |
| 57 |
|
| 58 |
func TestNewLogs_ProcessLogError(t *testing.T) { |
| 59 |
want := errors.New("my_error") |
| 60 |
lp, err := NewLogs(context.Background(), processortest.NewNopSettings(processortest.NopType), &testLogsCfg, consumertest.NewNop(), newTestLProcessor(want)) |
| 61 |
require.NoError(t, err) |
| 62 |
assert.Equal(t, want, lp.ConsumeLogs(context.Background(), plog.NewLogs())) |
| 63 |
} |
| 64 |
|
| 65 |
func TestNewLogs_ProcessLogsErrSkipProcessingData(t *testing.T) { |
| 66 |
lp, err := NewLogs(context.Background(), processortest.NewNopSettings(processortest.NopType), &testLogsCfg, consumertest.NewNop(), newTestLProcessor(ErrSkipProcessingData)) |
| 67 |
require.NoError(t, err) |
| 68 |
assert.NoError(t, lp.ConsumeLogs(context.Background(), plog.NewLogs())) |
| 69 |
} |
| 70 |
|
| 71 |
func newTestLProcessor(retError error) ProcessLogsFunc { |
| 72 |
return func(_ context.Context, ld plog.Logs) (plog.Logs, error) { |
| 73 |
return ld, retError |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func TestLogsConcurrency(t *testing.T) { |
| 78 |
logsFunc := func(_ context.Context, ld plog.Logs) (plog.Logs, error) { |
| 79 |
return ld, nil |
| 80 |
} |
| 81 |
|
| 82 |
incomingLogs := plog.NewLogs() |
| 83 |
incomingLogRecords := incomingLogs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords() |
| 84 |
|
| 85 |
// Add 3 records to the incoming |
| 86 |
incomingLogRecords.AppendEmpty() |
| 87 |
incomingLogRecords.AppendEmpty() |
| 88 |
incomingLogRecords.AppendEmpty() |
| 89 |
|
| 90 |
lp, err := NewLogs(context.Background(), processortest.NewNopSettings(processortest.NopType), &testLogsCfg, consumertest.NewNop(), logsFunc) |
| 91 |
require.NoError(t, err) |
| 92 |
assert.NoError(t, lp.Start(context.Background(), componenttest.NewNopHost())) |
| 93 |
|
| 94 |
var wg sync.WaitGroup |
| 95 |
for range 10 { |
| 96 |
wg.Add(1) |
| 97 |
go func() { |
| 98 |
defer wg.Done() |
| 99 |
for range 10000 { |
| 100 |
assert.NoError(t, lp.ConsumeLogs(context.Background(), incomingLogs)) |
| 101 |
} |
| 102 |
}() |
| 103 |
} |
| 104 |
wg.Wait() |
| 105 |
assert.NoError(t, lp.Shutdown(context.Background())) |
| 106 |
} |
| 107 |
|
| 108 |
func TestLogs_RecordInOut(t *testing.T) { |
| 109 |
// Regardless of how many logs are ingested, emit just one |
| 110 |
mockAggregate := func(_ context.Context, _ plog.Logs) (plog.Logs, error) { |
| 111 |
ld := plog.NewLogs() |
| 112 |
ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() |
| 113 |
return ld, nil |
| 114 |
} |
| 115 |
|
| 116 |
incomingLogs := plog.NewLogs() |
| 117 |
incomingLogRecords := incomingLogs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords() |
| 118 |
|
| 119 |
// Add 3 records to the incoming |
| 120 |
incomingLogRecords.AppendEmpty() |
| 121 |
incomingLogRecords.AppendEmpty() |
| 122 |
incomingLogRecords.AppendEmpty() |
| 123 |
|
| 124 |
tel := componenttest.NewTelemetry() |
| 125 |
lp, err := NewLogs(context.Background(), newSettings(tel), &testLogsCfg, consumertest.NewNop(), mockAggregate) |
| 126 |
require.NoError(t, err) |
| 127 |
|
| 128 |
assert.NoError(t, lp.Start(context.Background(), componenttest.NewNopHost())) |
| 129 |
assert.NoError(t, lp.ConsumeLogs(context.Background(), incomingLogs)) |
| 130 |
assert.NoError(t, lp.Shutdown(context.Background())) |
| 131 |
|
| 132 |
metadatatest.AssertEqualProcessorIncomingItems(t, tel, |
| 133 |
[]metricdata.DataPoint[int64]{ |
| 134 |
{ |
| 135 |
Value: 3, |
| 136 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "logs")), |
| 137 |
}, |
| 138 |
}, metricdatatest.IgnoreTimestamp()) |
| 139 |
metadatatest.AssertEqualProcessorOutgoingItems(t, tel, |
| 140 |
[]metricdata.DataPoint[int64]{ |
| 141 |
{ |
| 142 |
Value: 1, |
| 143 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "logs")), |
| 144 |
}, |
| 145 |
}, metricdatatest.IgnoreTimestamp()) |
| 146 |
} |
| 147 |
|
| 148 |
func TestLogs_RecordIn_ErrorOut(t *testing.T) { |
| 149 |
// Regardless of input, return error |
| 150 |
mockErr := func(_ context.Context, _ plog.Logs) (plog.Logs, error) { |
| 151 |
return plog.NewLogs(), errors.New("fake") |
| 152 |
} |
| 153 |
|
| 154 |
incomingLogs := plog.NewLogs() |
| 155 |
incomingLogRecords := incomingLogs.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords() |
| 156 |
|
| 157 |
// Add 3 records to the incoming |
| 158 |
incomingLogRecords.AppendEmpty() |
| 159 |
incomingLogRecords.AppendEmpty() |
| 160 |
incomingLogRecords.AppendEmpty() |
| 161 |
|
| 162 |
tel := componenttest.NewTelemetry() |
| 163 |
lp, err := NewLogs(context.Background(), newSettings(tel), &testLogsCfg, consumertest.NewNop(), mockErr) |
| 164 |
require.NoError(t, err) |
| 165 |
|
| 166 |
require.NoError(t, lp.Start(context.Background(), componenttest.NewNopHost())) |
| 167 |
require.Error(t, lp.ConsumeLogs(context.Background(), incomingLogs)) |
| 168 |
require.NoError(t, lp.Shutdown(context.Background())) |
| 169 |
|
| 170 |
metadatatest.AssertEqualProcessorIncomingItems(t, tel, |
| 171 |
[]metricdata.DataPoint[int64]{ |
| 172 |
{ |
| 173 |
Value: 3, |
| 174 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "logs")), |
| 175 |
}, |
| 176 |
}, metricdatatest.IgnoreTimestamp()) |
| 177 |
metadatatest.AssertEqualProcessorOutgoingItems(t, tel, |
| 178 |
[]metricdata.DataPoint[int64]{ |
| 179 |
{ |
| 180 |
Value: 0, |
| 181 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "logs")), |
| 182 |
}, |
| 183 |
}, metricdatatest.IgnoreTimestamp()) |
| 184 |
} |
| 185 |
|
| 186 |
func TestLogs_ProcessInternalDuration(t *testing.T) { |
| 187 |
mockAggregate := func(_ context.Context, _ plog.Logs) (plog.Logs, error) { |
| 188 |
ld := plog.NewLogs() |
| 189 |
ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() |
| 190 |
return ld, nil |
| 191 |
} |
| 192 |
|
| 193 |
incomingLogs := plog.NewLogs() |
| 194 |
|
| 195 |
tel := componenttest.NewTelemetry() |
| 196 |
lp, err := NewLogs(context.Background(), newSettings(tel), &testLogsCfg, consumertest.NewNop(), mockAggregate) |
| 197 |
require.NoError(t, err) |
| 198 |
|
| 199 |
assert.NoError(t, lp.Start(context.Background(), componenttest.NewNopHost())) |
| 200 |
assert.NoError(t, lp.ConsumeLogs(context.Background(), incomingLogs)) |
| 201 |
assert.NoError(t, lp.Shutdown(context.Background())) |
| 202 |
|
| 203 |
metadatatest.AssertEqualProcessorInternalDuration(t, tel, |
| 204 |
[]metricdata.HistogramDataPoint[float64]{ |
| 205 |
{ |
| 206 |
Count: 1, |
| 207 |
BucketCounts: []uint64{1}, |
| 208 |
Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "logs")), |
| 209 |
}, |
| 210 |
}, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue()) |
| 211 |
} |
| 212 |
|
| 213 |
func newSettings(tel *componenttest.Telemetry) processor.Settings { |
| 214 |
set := processortest.NewNopSettings(component.MustNewType("processorhelper")) |
| 215 |
set.TelemetrySettings = tel.NewTelemetrySettings() |
| 216 |
return set |
| 217 |
} |