Skip to content

Commit

Permalink
getextension
Browse files Browse the repository at this point in the history
  • Loading branch information
timannguyen committed Dec 8, 2023
1 parent f3c7466 commit 4ada692
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 128 deletions.
67 changes: 0 additions & 67 deletions extension/memorylimiter/internal/mock_exporter.go

This file was deleted.

61 changes: 0 additions & 61 deletions extension/memorylimiter/internal/mock_receiver.go

This file was deleted.

20 changes: 20 additions & 0 deletions extension/memorylimiter/memorylimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ var (
getMemoryFn = iruntime.TotalMemory
)

// MemoryLimiter is an Extension control memory usage of OpenTelemetry Collector.
// Common usage is to check memory usage during connection and can reject or accept the connection based on the
// configured memoryLimiter settings
type MemoryLimiter interface {
// CheckMemory check if memory usage is above soft limits and return error to refuse data if is above.
CheckMemory() error
}

type memoryLimiter struct {
usageChecker memUsageChecker

Expand Down Expand Up @@ -121,6 +129,17 @@ func getMemUsageChecker(cfg *Config, logger *zap.Logger) (*memUsageChecker, erro
return newPercentageMemUsageChecker(totalMemory, uint64(cfg.MemoryLimitPercentage), uint64(cfg.MemorySpikePercentage))
}

// GetMemoryLimiterExtension attempts to find a memory limiter extension in the extension list.
// If a memory limiter extension is not found, an error is returned.
func GetMemoryLimiterExtension(extensions map[component.ID]component.Component) (MemoryLimiter, error) {
for _, extension := range extensions {
if ext, ok := extension.(interface{ CheckMemory() error }); ok {
return ext.(MemoryLimiter), nil
}
}
return nil, fmt.Errorf("failed to resolve Memory Limiter")
}

func (ml *memoryLimiter) Start(_ context.Context, _ component.Host) error {
ml.startMonitoring()
return nil
Expand All @@ -139,6 +158,7 @@ func (ml *memoryLimiter) Shutdown(_ context.Context) error {
return nil
}

// CheckMemory check if memory usage is above soft limits and return error to refuse data if is above.
func (ml *memoryLimiter) CheckMemory() error {
if ml.mustRefuse.Load() {
// TODO: actually to be 100% sure that this is "refused" and not "dropped"
Expand Down
26 changes: 26 additions & 0 deletions extension/memorylimiter/memorylimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/internal/iruntime"
"go.uber.org/zap"

Expand Down Expand Up @@ -219,3 +220,28 @@ func TestRefuseDecision(t *testing.T) {
})
}
}

func testGetGetMemoryLimiterExtension(t *testing.T) {
exts := make(map[component.ID]component.Component)
t.Run("extension not found", func(t *testing.T) {
e, err := GetMemoryLimiterExtension(exts)
require.Error(t, err)
assert.Nil(t, e)
})

exts[component.NewID("ml")] = &memoryLimiter{
usageChecker: memUsageChecker{
memAllocLimit: 1024,
},
mustRefuse: &atomic.Bool{},
readMemStatsFn: func(ms *runtime.MemStats) {
ms.Alloc = 100
},
logger: zap.NewNop(),
}
t.Run("extension found", func(t *testing.T) {
ml, err := GetMemoryLimiterExtension(exts)
assert.NoError(t, err)
assert.NoError(t, ml.CheckMemory())
})
}

0 comments on commit 4ada692

Please sign in to comment.