Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/otelcol/processor/memorylimiter/memorylimiter.go
4096 views
1
// Package memorylimiter provides an otelcol.processor.memory_limiter component.
2
package memorylimiter
3
4
import (
5
"fmt"
6
"time"
7
8
"github.com/alecthomas/units"
9
"github.com/grafana/agent/component"
10
"github.com/grafana/agent/component/otelcol"
11
"github.com/grafana/agent/component/otelcol/processor"
12
"github.com/grafana/agent/pkg/river"
13
otelcomponent "go.opentelemetry.io/collector/component"
14
otelconfig "go.opentelemetry.io/collector/config"
15
"go.opentelemetry.io/collector/processor/memorylimiterprocessor"
16
)
17
18
func init() {
19
component.Register(component.Registration{
20
Name: "otelcol.processor.memory_limiter",
21
Args: Arguments{},
22
Exports: otelcol.ConsumerExports{},
23
24
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
25
fact := memorylimiterprocessor.NewFactory()
26
return processor.New(opts, fact, args.(Arguments))
27
},
28
})
29
}
30
31
// Arguments configures the otelcol.processor.memory_limiter component.
32
type Arguments struct {
33
CheckInterval time.Duration `river:"check_interval,attr"`
34
MemoryLimit units.Base2Bytes `river:"limit,attr,optional"`
35
MemorySpikeLimit units.Base2Bytes `river:"spike_limit,attr,optional"`
36
MemoryLimitPercentage uint32 `river:"limit_percentage,attr,optional"`
37
MemorySpikePercentage uint32 `river:"spike_limit_percentage,attr,optional"`
38
39
// Output configures where to send processed data. Required.
40
Output *otelcol.ConsumerArguments `river:"output,block"`
41
}
42
43
var (
44
_ processor.Arguments = Arguments{}
45
_ river.Unmarshaler = (*Arguments)(nil)
46
)
47
48
// DefaultArguments holds default settings for Arguments.
49
var DefaultArguments = Arguments{
50
CheckInterval: 0,
51
MemoryLimit: 0,
52
MemorySpikeLimit: 0,
53
MemoryLimitPercentage: 0,
54
MemorySpikePercentage: 0,
55
}
56
57
// UnmarshalRiver implements river.Unmarshaler. It applies defaults to args and
58
// validates settings provided by the user.
59
func (args *Arguments) UnmarshalRiver(f func(interface{}) error) error {
60
*args = DefaultArguments
61
62
type arguments Arguments
63
if err := f((*arguments)(args)); err != nil {
64
return err
65
}
66
67
if args.CheckInterval <= 0 {
68
return fmt.Errorf("check_interval must be greater than zero")
69
}
70
71
if args.MemoryLimit > 0 && args.MemoryLimitPercentage > 0 {
72
return fmt.Errorf("either limit or limit_percentage must be set, but not both")
73
}
74
75
if args.MemoryLimit > 0 {
76
if args.MemorySpikeLimit >= args.MemoryLimit {
77
return fmt.Errorf("spike_limit must be less than limit")
78
}
79
if args.MemorySpikeLimit == 0 {
80
args.MemorySpikeLimit = args.MemoryLimit / 5
81
}
82
return nil
83
}
84
if args.MemoryLimitPercentage > 0 {
85
if args.MemoryLimitPercentage <= 0 ||
86
args.MemoryLimitPercentage > 100 ||
87
args.MemorySpikePercentage <= 0 ||
88
args.MemorySpikePercentage > 100 {
89
90
return fmt.Errorf("limit_percentage and spike_limit_percentage must be greater than 0 and and less or equal than 100")
91
}
92
return nil
93
}
94
95
return fmt.Errorf("either limit or limit_percentage must be set to greater than zero")
96
}
97
98
// Convert implements processor.Arguments.
99
func (args Arguments) Convert() (otelconfig.Processor, error) {
100
return &memorylimiterprocessor.Config{
101
ProcessorSettings: otelconfig.NewProcessorSettings(otelconfig.NewComponentID("memory_limiter")),
102
103
CheckInterval: args.CheckInterval,
104
MemoryLimitMiB: uint32(args.MemoryLimit / units.Mebibyte),
105
MemorySpikeLimitMiB: uint32(args.MemorySpikeLimit / units.Mebibyte),
106
MemoryLimitPercentage: args.MemoryLimitPercentage,
107
MemorySpikePercentage: args.MemorySpikePercentage,
108
}, nil
109
}
110
111
// Extensions implements processor.Arguments.
112
func (args Arguments) Extensions() map[otelconfig.ComponentID]otelcomponent.Extension {
113
return nil
114
}
115
116
// Exporters implements processor.Arguments.
117
func (args Arguments) Exporters() map[otelconfig.DataType]map[otelconfig.ComponentID]otelcomponent.Exporter {
118
return nil
119
}
120
121
// NextConsumers implements processor.Arguments.
122
func (args Arguments) NextConsumers() *otelcol.ConsumerArguments {
123
return args.Output
124
}
125
126