Debian Codegraph @

processor/memorylimiterprocessor/internal/mock_receiver.go

raw
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 "strings"
9 "sync"
10
11 "go.opentelemetry.io/collector/consumer"
12 "go.opentelemetry.io/collector/consumer/consumererror"
13 "go.opentelemetry.io/collector/pdata/plog"
14 )
15
16 type MockReceiver struct {
17 ProduceCount int
18 NextConsumer consumer.Logs
19 lastConsumeResult error
20 mux sync.Mutex
21 }
22
23 func (m *MockReceiver) Start() {
24 go m.produce()
25 }
26
27 // This function demonstrates how the receivers should behave when the ConsumeLogs/Traces/Metrics
28 // call returns an error.
29 func (m *MockReceiver) produce() {
30 for i := 0; i < m.ProduceCount; i++ {
31 // Create a large log to consume some memory.
32 ld := plog.NewLogs()
33 lr := ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
34 kiloStr := strings.Repeat("x", 10*1024)
35 lr.SetSeverityText(kiloStr)
36
37 retry:
38 // Send to the pipeline.
39 err := m.NextConsumer.ConsumeLogs(context.Background(), ld)
40
41 // Remember the result to be used in the tests.
42 m.mux.Lock()
43 m.lastConsumeResult = err
44 m.mux.Unlock()
45
46 if err != nil {
47 // Sending to the pipeline failed.
48 if !consumererror.IsPermanent(err) {
49 // Retryable error. Try the same data again.
50 goto retry
51 }
52 // Permanent error. Drop it.
53 }
54 }
55 }
56
57 func (m *MockReceiver) LastConsumeResult() error {
58 m.mux.Lock()
59 defer m.mux.Unlock()
60 return m.lastConsumeResult
61 }