Skip to content

Commit

Permalink
id component
Browse files Browse the repository at this point in the history
  • Loading branch information
timannguyen committed Nov 27, 2023
1 parent 3b0afe9 commit 1f2204d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .chloggen/extension-memory-limiter.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: [new_component]
change_type: new_component

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: extension_memoryLimiter
component: extension/memoryLimiter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: move memory limiter processor to extension to allow connection limits at the receiver
note: replicate processor/memorylimiter -> extension/memorylimiter to limits at the receiver

# One or more tracking issues or pull requests related to the change
issues: [8632]
Expand Down
18 changes: 18 additions & 0 deletions extension/memorylimiter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package memorylimiter // import "go.opentelemetry.io/collector/extension/memorylimiter"

import (
"fmt"
"time"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -35,6 +36,23 @@ type Config struct {
MemorySpikePercentage uint32 `mapstructure:"spike_limit_percentage"`
}

// MemoryLimitation defines memory limiter path for the receiver
type MemoryLimitation struct {
// MemoryLimiterID specifies the name of the memory limiter extension
MemoryLimiterID component.ID `mapstructure:"memory_limiter"`
}

// GetMemoryLimiter attempts to find a memory limiter extension in the extension list.
// If a memory limiter extension is not found, an error is returned.
func (m *MemoryLimitation) GetMemoryLimiter(extensions map[component.ID]component.Component) (MemoryLimiter, error) {
if ext, found := extensions[m.MemoryLimiterID]; found {
if ml, ok := ext.(MemoryLimiter); ok {
return ml, nil
}
}
return nil, fmt.Errorf("failed to resolve Memory Limiter %q", m.MemoryLimiterID)
}

var _ component.Config = (*Config)(nil)

// Validate checks if the extension configuration is valid
Expand Down
31 changes: 31 additions & 0 deletions extension/memorylimiter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ package memorylimiter

import (
"path/filepath"
"runtime"
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
Expand Down Expand Up @@ -56,3 +59,31 @@ func TestValidateConfig(t *testing.T) {
pCfg.MemoryLimitPercentage = 0
assert.Error(t, pCfg.Validate(), errLimitOutOfRange)
}

func TestMemoryLimitation(t *testing.T) {
var mlc = &MemoryLimitation{
MemoryLimiterID: component.NewID("ml"),
}
exts := make(map[component.ID]component.Component)
t.Run("extension not found", func(t *testing.T) {
e, err := mlc.GetMemoryLimiter(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 := mlc.GetMemoryLimiter(exts)
assert.NoError(t, err)
assert.NoError(t, ml.CheckMemory())
})
}
8 changes: 4 additions & 4 deletions extension/memorylimiter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
go.opentelemetry.io/collector/confmap v0.89.0
go.opentelemetry.io/collector/consumer v0.89.0
go.opentelemetry.io/collector/extension v0.89.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0018
go.uber.org/zap v1.26.0
)

Expand Down Expand Up @@ -37,9 +36,10 @@ require (
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.89.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0018 // indirect
go.opentelemetry.io/otel v1.20.0 // indirect
go.opentelemetry.io/otel/metric v1.20.0 // indirect
go.opentelemetry.io/otel/trace v1.20.0 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0018 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions extension/memorylimiter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions extension/memorylimiter/memorylimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,6 @@ 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 Down
26 changes: 0 additions & 26 deletions extension/memorylimiter/memorylimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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 @@ -220,28 +219,3 @@ 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 1f2204d

Please sign in to comment.