-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarify that memory limiter refuses data, doesn't drop it
Contributes to #1084 - Clarify what the memory limiter does. - Set expectations from receivers, how they are supposed to react when the memory limiter refuses the data. - Add a test that demonstrates that memory limiter does not lose data if the receiver and exporter behave according to the contract. All receivers must adhere to this contract. See for example an issue opened against filelog receiver: open-telemetry/opentelemetry-collector-contrib#20511 Note that there are no functional changes to the memory limiter. Future work: one additional thing we can do is implement a backoff logic in the memory limiter. When in memory limited mode the processor can introduce pauses before it returns from the ConsumeLogs/Traces/Metrics call. This will allow to slow down the inflow of data into the Collector and give time for the pipeline to clear and memory usage to return to the normal. This needs to be explored further.
- Loading branch information
1 parent
d4c25d4
commit 5c617b7
Showing
6 changed files
with
306 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
processor/memorylimiterprocessor/internal/mock_exporter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal // import "go.opentelemetry.io/collector/processor/memorylimiterprocessor/internal" | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
|
||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
type MockExporter struct { | ||
destAvailable int64 | ||
acceptedLogCount int64 | ||
deliveredLogCount int64 | ||
Logs []plog.Logs | ||
} | ||
|
||
var _ consumer.Logs = (*MockExporter)(nil) | ||
|
||
func (e *MockExporter) Capabilities() consumer.Capabilities { | ||
return consumer.Capabilities{} | ||
} | ||
|
||
func (e *MockExporter) ConsumeLogs(_ context.Context, ld plog.Logs) error { | ||
atomic.AddInt64(&e.acceptedLogCount, int64(ld.LogRecordCount())) | ||
|
||
if atomic.LoadInt64(&e.destAvailable) == 1 { | ||
// Destination is available, immediately deliver. | ||
atomic.AddInt64(&e.deliveredLogCount, int64(ld.LogRecordCount())) | ||
} else { | ||
// Destination is not available. Queue the logs in the exporter. | ||
e.Logs = append(e.Logs, ld) | ||
} | ||
return nil | ||
} | ||
|
||
func (e *MockExporter) SetDestAvailable(available bool) { | ||
if available { | ||
// Pretend we delivered all queued accepted logs. | ||
atomic.AddInt64(&e.deliveredLogCount, e.acceptedLogCount) | ||
|
||
// Get rid of the delivered logs so that memory can be collected. | ||
e.Logs = nil | ||
|
||
// Now mark destination available so that subsequent ConsumeLogs | ||
// don't queue the logs anymore. | ||
atomic.StoreInt64(&e.destAvailable, 1) | ||
|
||
} else { | ||
atomic.StoreInt64(&e.destAvailable, 0) | ||
} | ||
} | ||
|
||
func (e *MockExporter) AcceptedLogCount() int { | ||
return int(atomic.LoadInt64(&e.acceptedLogCount)) | ||
} | ||
|
||
func (e *MockExporter) DeliveredLogCount() int { | ||
return int(atomic.LoadInt64(&e.deliveredLogCount)) | ||
} | ||
|
||
func NewMockExporter() *MockExporter { | ||
return &MockExporter{} | ||
} |
72 changes: 72 additions & 0 deletions
72
processor/memorylimiterprocessor/internal/mock_receiver.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package internal // import "go.opentelemetry.io/collector/processor/memorylimiterprocessor/internal" | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"sync" | ||
|
||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/consumer/consumererror" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
) | ||
|
||
type MockReceiver struct { | ||
ProduceCount int | ||
NextConsumer consumer.Logs | ||
lastConsumeResult error | ||
mux sync.Mutex | ||
} | ||
|
||
func (m *MockReceiver) Start() { | ||
go m.produce() | ||
} | ||
|
||
// This function demonstrates how the receivers should behave when the ConsumeLogs/Traces/Metrics | ||
// call returns an error. | ||
func (m *MockReceiver) produce() { | ||
for i := 0; i < m.ProduceCount; i++ { | ||
// Create a large log to consume some memory. | ||
ld := plog.NewLogs() | ||
lr := ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() | ||
kiloStr := strings.Repeat("x", 10*1024) | ||
lr.SetSeverityText(kiloStr) | ||
|
||
retry: | ||
// Send to the pipeline. | ||
err := m.NextConsumer.ConsumeLogs(context.Background(), ld) | ||
|
||
// Remember the result to be used in the tests. | ||
m.mux.Lock() | ||
m.lastConsumeResult = err | ||
m.mux.Unlock() | ||
|
||
if err != nil { | ||
// Sending to the pipeline failed. | ||
if !consumererror.IsPermanent(err) { | ||
// Retryable error. Try the same data again. | ||
goto retry | ||
} | ||
// Permanent error. Drop it. | ||
} | ||
} | ||
} | ||
|
||
func (m *MockReceiver) LastConsumeResult() error { | ||
m.mux.Lock() | ||
defer m.mux.Unlock() | ||
return m.lastConsumeResult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.