| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internal // import "go.opentelemetry.io/collector/processor/memorylimiterprocessor/internal" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"sync/atomic" |
| 9 |
|
| 10 |
"go.opentelemetry.io/collector/consumer" |
| 11 |
"go.opentelemetry.io/collector/consumer/consumertest" |
| 12 |
"go.opentelemetry.io/collector/pdata/plog" |
| 13 |
) |
| 14 |
|
| 15 |
type MockExporter struct { |
| 16 |
destAvailable atomic.Bool |
| 17 |
acceptedLogCount atomic.Int64 |
| 18 |
deliveredLogCount atomic.Int64 |
| 19 |
Logs consumertest.LogsSink |
| 20 |
} |
| 21 |
|
| 22 |
var _ consumer.Logs = (*MockExporter)(nil) |
| 23 |
|
| 24 |
func (e *MockExporter) Capabilities() consumer.Capabilities { |
| 25 |
return consumer.Capabilities{} |
| 26 |
} |
| 27 |
|
| 28 |
func (e *MockExporter) ConsumeLogs(ctx context.Context, ld plog.Logs) error { |
| 29 |
e.acceptedLogCount.Add(int64(ld.LogRecordCount())) |
| 30 |
|
| 31 |
if !e.destAvailable.Load() { |
| 32 |
// Destination is not available. Queue the logs in the exporter. |
| 33 |
return e.Logs.ConsumeLogs(ctx, ld) |
| 34 |
} |
| 35 |
// Destination is available, immediately deliver. |
| 36 |
e.deliveredLogCount.Add(int64(ld.LogRecordCount())) |
| 37 |
return nil |
| 38 |
} |
| 39 |
|
| 40 |
func (e *MockExporter) SetDestAvailable(available bool) { |
| 41 |
if available { |
| 42 |
// Pretend we delivered all queued accepted logs. |
| 43 |
e.deliveredLogCount.Add(int64(e.Logs.LogRecordCount())) |
| 44 |
|
| 45 |
// Get rid of the delivered logs so that memory can be collected. |
| 46 |
e.Logs.Reset() |
| 47 |
|
| 48 |
// Now mark destination available so that subsequent ConsumeLogs |
| 49 |
// don't queue the logs anymore. |
| 50 |
e.destAvailable.Store(true) |
| 51 |
} else { |
| 52 |
e.destAvailable.Store(false) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func (e *MockExporter) AcceptedLogCount() int { |
| 57 |
return int(e.acceptedLogCount.Load()) |
| 58 |
} |
| 59 |
|
| 60 |
func (e *MockExporter) DeliveredLogCount() int { |
| 61 |
return int(e.deliveredLogCount.Load()) |
| 62 |
} |
| 63 |
|
| 64 |
func NewMockExporter() *MockExporter { |
| 65 |
return &MockExporter{ |
| 66 |
destAvailable: atomic.Bool{}, |
| 67 |
acceptedLogCount: atomic.Int64{}, |
| 68 |
deliveredLogCount: atomic.Int64{}, |
| 69 |
Logs: consumertest.LogsSink{}, |
| 70 |
} |
| 71 |
} |