Debian Codegraph @

internal/memorylimiter/config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package memorylimiter // import "go.opentelemetry.io/collector/internal/memorylimiter"
5
6 import (
7 "errors"
8 "time"
9
10 "go.opentelemetry.io/collector/component"
11 )
12
13 var (
14 errCheckIntervalOutOfRange = errors.New("'check_interval' must be greater than zero")
15 errInconsistentGCMinInterval = errors.New("'min_gc_interval_when_soft_limited' should be larger than 'min_gc_interval_when_hard_limited'")
16 errLimitOutOfRange = errors.New("'limit_mib' or 'limit_percentage' must be greater than zero")
17 errSpikeLimitOutOfRange = errors.New("'spike_limit_mib' must be smaller than 'limit_mib'")
18 errSpikeLimitPercentageOutOfRange = errors.New("'spike_limit_percentage' must be smaller than 'limit_percentage'")
19 errLimitPercentageOutOfRange = errors.New(
20 "'limit_percentage' and 'spike_limit_percentage' must be greater than zero and less than or equal to hundred")
21 )
22
23 // Config defines configuration for memory memoryLimiter processor.
24 type Config struct {
25 // CheckInterval is the time between measurements of memory usage for the
26 // purposes of avoiding going over the limits. Defaults to zero, so no
27 // checks will be performed.
28 CheckInterval time.Duration `mapstructure:"check_interval"`
29
30 // MinGCIntervalWhenSoftLimited minimum interval between forced GC when in soft (=limit_mib - spike_limit_mib) limited mode.
31 // Zero value means no minimum interval.
32 // GCs is a CPU-heavy operation and executing it too frequently may affect the recovery capabilities of the collector.
33 MinGCIntervalWhenSoftLimited time.Duration `mapstructure:"min_gc_interval_when_soft_limited"`
34
35 // MinGCIntervalWhenHardLimited minimum interval between forced GC when in hard (=limit_mib) limited mode.
36 // Zero value means no minimum interval.
37 // GCs is a CPU-heavy operation and executing it too frequently may affect the recovery capabilities of the collector.
38 MinGCIntervalWhenHardLimited time.Duration `mapstructure:"min_gc_interval_when_hard_limited"`
39
40 // MemoryLimitMiB is the maximum amount of memory, in MiB, targeted to be
41 // allocated by the process.
42 MemoryLimitMiB uint32 `mapstructure:"limit_mib"`
43
44 // MemorySpikeLimitMiB is the maximum, in MiB, spike expected between the
45 // measurements of memory usage.
46 MemorySpikeLimitMiB uint32 `mapstructure:"spike_limit_mib"`
47
48 // MemoryLimitPercentage is the maximum amount of memory, in %, targeted to be
49 // allocated by the process. The fixed memory settings MemoryLimitMiB has a higher precedence.
50 MemoryLimitPercentage uint32 `mapstructure:"limit_percentage"`
51
52 // MemorySpikePercentage is the maximum, in percents against the total memory,
53 // spike expected between the measurements of memory usage.
54 MemorySpikePercentage uint32 `mapstructure:"spike_limit_percentage"`
55 }
56
57 var _ component.Config = (*Config)(nil)
58
59 func NewDefaultConfig() *Config {
60 return &Config{
61 MinGCIntervalWhenSoftLimited: 10 * time.Second,
62 }
63 }
64
65 // Validate checks if the processor configuration is valid
66 func (cfg *Config) Validate() error {
67 if cfg.CheckInterval <= 0 {
68 return errCheckIntervalOutOfRange
69 }
70 if cfg.MinGCIntervalWhenSoftLimited < cfg.MinGCIntervalWhenHardLimited {
71 return errInconsistentGCMinInterval
72 }
73 if cfg.MemoryLimitMiB == 0 && cfg.MemoryLimitPercentage == 0 {
74 return errLimitOutOfRange
75 }
76 if cfg.MemoryLimitPercentage > 100 || cfg.MemorySpikePercentage > 100 {
77 return errLimitPercentageOutOfRange
78 }
79 if cfg.MemoryLimitMiB > 0 && cfg.MemoryLimitMiB <= cfg.MemorySpikeLimitMiB {
80 return errSpikeLimitOutOfRange
81 }
82 if cfg.MemoryLimitPercentage > 0 && cfg.MemoryLimitPercentage <= cfg.MemorySpikePercentage {
83 return errSpikeLimitPercentageOutOfRange
84 }
85 return nil
86 }