Path: blob/main/component/otelcol/processor/memorylimiter/memorylimiter.go
4096 views
// Package memorylimiter provides an otelcol.processor.memory_limiter component.1package memorylimiter23import (4"fmt"5"time"67"github.com/alecthomas/units"8"github.com/grafana/agent/component"9"github.com/grafana/agent/component/otelcol"10"github.com/grafana/agent/component/otelcol/processor"11"github.com/grafana/agent/pkg/river"12otelcomponent "go.opentelemetry.io/collector/component"13otelconfig "go.opentelemetry.io/collector/config"14"go.opentelemetry.io/collector/processor/memorylimiterprocessor"15)1617func init() {18component.Register(component.Registration{19Name: "otelcol.processor.memory_limiter",20Args: Arguments{},21Exports: otelcol.ConsumerExports{},2223Build: func(opts component.Options, args component.Arguments) (component.Component, error) {24fact := memorylimiterprocessor.NewFactory()25return processor.New(opts, fact, args.(Arguments))26},27})28}2930// Arguments configures the otelcol.processor.memory_limiter component.31type Arguments struct {32CheckInterval time.Duration `river:"check_interval,attr"`33MemoryLimit units.Base2Bytes `river:"limit,attr,optional"`34MemorySpikeLimit units.Base2Bytes `river:"spike_limit,attr,optional"`35MemoryLimitPercentage uint32 `river:"limit_percentage,attr,optional"`36MemorySpikePercentage uint32 `river:"spike_limit_percentage,attr,optional"`3738// Output configures where to send processed data. Required.39Output *otelcol.ConsumerArguments `river:"output,block"`40}4142var (43_ processor.Arguments = Arguments{}44_ river.Unmarshaler = (*Arguments)(nil)45)4647// DefaultArguments holds default settings for Arguments.48var DefaultArguments = Arguments{49CheckInterval: 0,50MemoryLimit: 0,51MemorySpikeLimit: 0,52MemoryLimitPercentage: 0,53MemorySpikePercentage: 0,54}5556// UnmarshalRiver implements river.Unmarshaler. It applies defaults to args and57// validates settings provided by the user.58func (args *Arguments) UnmarshalRiver(f func(interface{}) error) error {59*args = DefaultArguments6061type arguments Arguments62if err := f((*arguments)(args)); err != nil {63return err64}6566if args.CheckInterval <= 0 {67return fmt.Errorf("check_interval must be greater than zero")68}6970if args.MemoryLimit > 0 && args.MemoryLimitPercentage > 0 {71return fmt.Errorf("either limit or limit_percentage must be set, but not both")72}7374if args.MemoryLimit > 0 {75if args.MemorySpikeLimit >= args.MemoryLimit {76return fmt.Errorf("spike_limit must be less than limit")77}78if args.MemorySpikeLimit == 0 {79args.MemorySpikeLimit = args.MemoryLimit / 580}81return nil82}83if args.MemoryLimitPercentage > 0 {84if args.MemoryLimitPercentage <= 0 ||85args.MemoryLimitPercentage > 100 ||86args.MemorySpikePercentage <= 0 ||87args.MemorySpikePercentage > 100 {8889return fmt.Errorf("limit_percentage and spike_limit_percentage must be greater than 0 and and less or equal than 100")90}91return nil92}9394return fmt.Errorf("either limit or limit_percentage must be set to greater than zero")95}9697// Convert implements processor.Arguments.98func (args Arguments) Convert() (otelconfig.Processor, error) {99return &memorylimiterprocessor.Config{100ProcessorSettings: otelconfig.NewProcessorSettings(otelconfig.NewComponentID("memory_limiter")),101102CheckInterval: args.CheckInterval,103MemoryLimitMiB: uint32(args.MemoryLimit / units.Mebibyte),104MemorySpikeLimitMiB: uint32(args.MemorySpikeLimit / units.Mebibyte),105MemoryLimitPercentage: args.MemoryLimitPercentage,106MemorySpikePercentage: args.MemorySpikePercentage,107}, nil108}109110// Extensions implements processor.Arguments.111func (args Arguments) Extensions() map[otelconfig.ComponentID]otelcomponent.Extension {112return nil113}114115// Exporters implements processor.Arguments.116func (args Arguments) Exporters() map[otelconfig.DataType]map[otelconfig.ComponentID]otelcomponent.Exporter {117return nil118}119120// NextConsumers implements processor.Arguments.121func (args Arguments) NextConsumers() *otelcol.ConsumerArguments {122return args.Output123}124125126