Skip to content

Commit 5f8dec7

Browse files
author
Bogdan Drutu
authored
Update to use the ballast memory instead of the flag: (#567)
* The memory_ballast extension is the only way to configure ballast starting with v0.31.0 of the collector; * The memory_limiter processor accesses the ballast directly from the config, no need to set ballast_memory_size anymore; Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 491b2f0 commit 5f8dec7

File tree

9 files changed

+144
-140
lines changed

9 files changed

+144
-140
lines changed

cmd/otelcol/config/collector/agent_config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ extensions:
2727
configDir: "${SPLUNK_COLLECTD_DIR}"
2828
zpages:
2929
#endpoint: 0.0.0.0:55679
30+
memory_ballast:
31+
# In general, the ballast should be set to 1/3 of the collector's memory, the limit
32+
# should be 90% of the collector's memory.
33+
# The simplest way to specify the ballast size is set the value of SPLUNK_BALLAST_SIZE_MIB env variable.
34+
size_mib: ${SPLUNK_BALLAST_SIZE_MIB}
3035

3136
receivers:
3237
fluentforward:
@@ -88,14 +93,9 @@ processors:
8893
batch:
8994
# Enabling the memory_limiter is strongly recommended for every pipeline.
9095
# Configuration is based on the amount of memory allocated to the collector.
91-
# In general, the ballast should be set to 1/3 of the collector's memory, the limit
92-
# should be 90% of the collector's memory. The simplest way to specify the
93-
# ballast size is set the value of SPLUNK_BALLAST_SIZE_MIB env variable. Alternatively, the
94-
# --mem-ballast-size-mib command line flag can be passed and take priority.
9596
# For more information about memory limiter, see
9697
# https://github.com/open-telemetry/opentelemetry-collector/blob/main/processor/memorylimiter/README.md
9798
memory_limiter:
98-
ballast_size_mib: ${SPLUNK_BALLAST_SIZE_MIB}
9999
check_interval: 2s
100100
limit_mib: ${SPLUNK_MEMORY_LIMIT_MIB}
101101
# detect if the collector is running on a cloud system
@@ -148,7 +148,7 @@ exporters:
148148
loglevel: debug
149149

150150
service:
151-
extensions: [health_check, http_forwarder, zpages]
151+
extensions: [health_check, http_forwarder, zpages, memory_ballast]
152152
pipelines:
153153
traces:
154154
receivers: [jaeger, otlp, smartagent/signalfx-forwarder, zipkin]

cmd/otelcol/config/collector/full_config_linux.yaml

+11-7
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,8 @@ processors:
217217

218218
# Enables the memory limiter processor with default settings
219219
# Full configuration here: https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor/memorylimiter
220-
# Enabling the memory_limiter is strongly recommended for every pipeline.
221-
# Configuration is based on the amount of memory allocated to the collector.
222-
# The configuration below assumes 2GB of memory. In general, the ballast
223-
# should be set to 1/3 of the collector's memory, the limit should be 90% of
224-
# the collector's memory.
225220
# NOTE: These settings need to be change when using this processor
226221
memory_limiter:
227-
ballast_size_mib: 650
228222
check_interval: 2s
229223
limit_mib: 1800
230224

@@ -535,6 +529,16 @@ extensions:
535529
zpages:
536530
#endpoint: 0.0.0.0:55679
537531

532+
# Enables the memory_ballast extension
533+
# Full configuration here: https://github.com/open-telemetry/opentelemetry-collector/tree/main/extension/ballastextension
534+
memory_ballast:
535+
# Enabling the memory_limiter is strongly recommended for every pipeline.
536+
# Configuration is based on the amount of memory allocated to the collector.
537+
# The configuration below assumes 2GB of memory for the collector.
538+
# In general, the ballast should be set to 1/3 of the collector's memory,
539+
# the limit should be 90% of the collector's memory.
540+
size_mib: 650
541+
538542
###############################################################################
539543
# Service
540544
# In order to enable a configuration it must be defined in this section
@@ -544,7 +548,7 @@ extensions:
544548
service:
545549

546550
# Which extensions you want to enable
547-
extensions: [health_check, http_forwarder, zpages]
551+
extensions: [health_check, http_forwarder, zpages, memory_ballast]
548552

549553
# Pipelines are data source specific today
550554
# Every data source is made up of at least one receiver and one exporter

cmd/otelcol/config/collector/gateway_config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ extensions:
1111
endpoint: "https://api.${SPLUNK_REALM}.signalfx.com"
1212
zpages:
1313
endpoint: 0.0.0.0:55679
14+
memory_ballast:
15+
# In general, the ballast should be set to 1/3 of the collector's memory, the limit
16+
# should be 90% of the collector's memory.
17+
# The simplest way to specify the ballast size is set the value of SPLUNK_BALLAST_SIZE_MIB env variable.
18+
size_mib: ${SPLUNK_BALLAST_SIZE_MIB}
1419

1520
receivers:
1621
jaeger:
@@ -53,14 +58,9 @@ processors:
5358
batch:
5459
# Enabling the memory_limiter is strongly recommended for every pipeline.
5560
# Configuration is based on the amount of memory allocated to the collector.
56-
# In general, the ballast should be set to 1/3 of the collector's memory, the limit
57-
# should be 90% of the collector's memory. The simplest way to specify the
58-
# ballast size is set the value of SPLUNK_BALLAST_SIZE_MIB env variable. Alternatively, the
59-
# --mem-ballast-size-mib command line flag can be passed and take priority.
6061
# For more information about memory limiter, see
6162
# https://github.com/open-telemetry/opentelemetry-collector/blob/main/processor/memorylimiter/README.md
6263
memory_limiter:
63-
ballast_size_mib: ${SPLUNK_BALLAST_SIZE_MIB}
6464
check_interval: 2s
6565
limit_mib: ${SPLUNK_MEMORY_LIMIT_MIB}
6666

@@ -97,7 +97,7 @@ exporters:
9797
#loglevel: debug
9898

9999
service:
100-
extensions: [health_check, http_forwarder, zpages]
100+
extensions: [health_check, http_forwarder, zpages, memory_ballast]
101101
pipelines:
102102
traces:
103103
receivers: [jaeger, otlp, sapm, zipkin]

cmd/otelcol/config/collector/otlp_config_linux.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@ processors:
4545
batch:
4646
# Enabling the memory_limiter is strongly recommended for every pipeline.
4747
# Configuration is based on the amount of memory allocated to the collector.
48-
# In general, the ballast should be set to 1/3 of the collector's memory, the limit
49-
# should be 90% of the collector's memory. The simplest way to specify the
50-
# ballast size is set the value of SPLUNK_BALLAST_SIZE_MIB env variable. Alternatively, the
51-
# --mem-ballast-size-mib command line flag can be passed and take priority.
5248
# For more information about memory limiter, see
5349
# https://github.com/open-telemetry/opentelemetry-collector/blob/main/processor/memorylimiter/README.md
5450
memory_limiter:
55-
ballast_size_mib: ${SPLUNK_BALLAST_SIZE_MIB}
5651
check_interval: 2s
5752
limit_mib: ${SPLUNK_MEMORY_LIMIT_MIB}
5853

@@ -96,9 +91,14 @@ extensions:
9691
endpoint: "https://api.${SPLUNK_REALM}.signalfx.com"
9792
zpages:
9893
endpoint: 0.0.0.0:55679
94+
memory_ballast:
95+
# In general, the ballast should be set to 1/3 of the collector's memory, the limit
96+
# should be 90% of the collector's memory.
97+
# The simplest way to specify the ballast size is set the value of SPLUNK_BALLAST_SIZE_MIB env variable.
98+
size_mib: ${SPLUNK_BALLAST_SIZE_MIB}
9999

100100
service:
101-
extensions: [health_check, http_forwarder, zpages]
101+
extensions: [health_check, http_forwarder, zpages, memory_ballast]
102102
pipelines:
103103
traces:
104104
receivers: [jaeger, otlp, smartagent/signalfx-forwarder, zipkin]

cmd/otelcol/config/collector/upstream_agent_config.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ extensions:
2424
#endpoint: "${SPLUNK_GATEWAY_URL}"
2525
zpages:
2626
#endpoint: 0.0.0.0:55679
27+
memory_ballast:
28+
# In general, the ballast should be set to 1/3 of the collector's memory, the limit
29+
# should be 90% of the collector's memory.
30+
# The simplest way to specify the ballast size is set the value of SPLUNK_BALLAST_SIZE_MIB env variable.
31+
size_mib: ${SPLUNK_BALLAST_SIZE_MIB}
2732

2833
receivers:
2934
fluentforward:
@@ -89,7 +94,6 @@ processors:
8994
# For more information about memory limiter, see
9095
# https://github.com/open-telemetry/opentelemetry-collector/blob/main/processor/memorylimiter/README.md
9196
memory_limiter:
92-
ballast_size_mib: ${SPLUNK_BALLAST_SIZE_MIB}
9397
check_interval: 2s
9498
limit_mib: ${SPLUNK_MEMORY_LIMIT_MIB}
9599
# detect if the collector is running on a cloud system
@@ -141,7 +145,7 @@ exporters:
141145
loglevel: debug
142146

143147
service:
144-
extensions: [health_check, http_forwarder, zpages]
148+
extensions: [health_check, http_forwarder, zpages, memory_ballast]
145149
pipelines:
146150
# Required for Splunk APM
147151
traces:

cmd/otelcol/main.go

+47-57
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/signalfx/splunk-otel-collector/internal/version"
3737
)
3838

39+
// The list of environment variables must be the same as what is used in the yaml configs.
3940
const (
4041
ballastEnvVarName = "SPLUNK_BALLAST_SIZE_MIB"
4142
configEnvVarName = "SPLUNK_CONFIG"
@@ -136,35 +137,25 @@ func checkRuntimeParams() {
136137
checkConfig()
137138

138139
// Set default total memory
139-
memTotalSizeMiB := defaultMemoryTotalMiB
140+
memTotalSize := defaultMemoryTotalMiB
140141
// Check if the total memory is specified via the env var
141-
memTotalEnvVarVal := os.Getenv(memTotalEnvVarName)
142142
// If so, validate and change total memory
143-
if memTotalEnvVarVal != "" {
143+
if os.Getenv(memTotalEnvVarName) != "" {
144144
// Check if it is a numeric value.
145-
val, err := strconv.Atoi(memTotalEnvVarVal)
146-
if err != nil {
147-
log.Fatalf("Expected a number in %s env variable but got %s", memTotalEnvVarName, memTotalEnvVarVal)
148-
}
145+
memTotalSize = envVarAsInt(memTotalEnvVarName)
149146
// Ensure number is above some threshold
150-
if 99 > val {
151-
log.Fatalf("Expected a number greater than 99 for %s env variable but got %s", memTotalEnvVarName, memTotalEnvVarVal)
147+
if 99 > memTotalSize {
148+
log.Fatalf("Expected a number greater than 99 for %s env variable but got %d", memTotalEnvVarName, memTotalSize)
152149
}
153-
memTotalSizeMiB = val
154150
}
155151

156-
// Check if memory ballast flag was passed
157-
// If so, ensure memory ballast env var is not set
158-
// Then set memory ballast and limit properly
159-
_, ballastSize := getKeyValue(os.Args[1:], "--mem-ballast-size-mib")
160-
if ballastSize != "" {
161-
if os.Getenv(ballastEnvVarName) != "" {
162-
log.Fatalf("Both %v and '--mem-ballast-size-mib' were specified, but only one is allowed", ballastEnvVarName)
163-
}
164-
os.Setenv(ballastEnvVarName, ballastSize)
152+
ballastSize := setMemoryBallast(memTotalSize)
153+
memLimit := setMemoryLimit(memTotalSize)
154+
155+
// Validate memoryLimit and memoryBallast are sane
156+
if 2*ballastSize > memLimit {
157+
log.Fatalf("Memory limit (%d) is less than 2x ballast (%d). Increase memory limit or decrease ballast size.", memLimit, ballastSize)
165158
}
166-
setMemoryBallast(memTotalSizeMiB)
167-
setMemoryLimit(memTotalSizeMiB)
168159
}
169160

170161
// Sets flag '--config' to specified env var SPLUNK_CONFIG, if the flag not specified.
@@ -256,54 +247,43 @@ func checkRequiredEnvVars(path string) {
256247
}
257248

258249
// Validate and set the memory ballast
259-
func setMemoryBallast(memTotalSizeMiB int) {
260-
// Check if the memory ballast is specified via the env var
261-
ballastSize := os.Getenv(ballastEnvVarName)
262-
// If so, validate and set properly
263-
if ballastSize != "" {
264-
// Check if it is a numeric value.
265-
val, err := strconv.Atoi(ballastSize)
266-
if err != nil {
267-
log.Fatalf("Expected a number in %s env variable but got %s", ballastEnvVarName, ballastSize)
268-
}
269-
if 33 > val {
270-
log.Fatalf("Expected a number greater than 33 for %s env variable but got %s", ballastEnvVarName, ballastSize)
250+
func setMemoryBallast(memTotalSizeMiB int) int {
251+
// Check if deprecated memory ballast flag was passed, if so, ensure the env variable for memory ballast is set.
252+
// Then set memory ballast and limit properly
253+
_, ballastSizeFlag := getKeyValue(os.Args[1:], "--mem-ballast-size-mib")
254+
if ballastSizeFlag != "" {
255+
if os.Getenv(ballastEnvVarName) != "" {
256+
log.Fatalf("Both %v and '--mem-ballast-size-mib' were specified, but only one is allowed", ballastEnvVarName)
271257
}
272-
} else {
273-
ballastSize = strconv.Itoa(memTotalSizeMiB * defaultMemoryBallastPercentage / 100)
274-
os.Setenv(ballastEnvVarName, ballastSize)
258+
os.Setenv(ballastEnvVarName, ballastSizeFlag)
275259
}
276260

277-
args := os.Args[1:]
278-
if !contains(args, "--mem-ballast-size-mib") {
279-
// Inject the command line flag that controls the ballast size.
280-
os.Args = append(os.Args, "--mem-ballast-size-mib="+ballastSize)
261+
ballastSize := memTotalSizeMiB * defaultMemoryBallastPercentage / 100
262+
// Check if the memory ballast is specified via the env var, if so, validate and set properly.
263+
if os.Getenv(ballastEnvVarName) != "" {
264+
ballastSize = envVarAsInt(ballastEnvVarName)
265+
if 33 > ballastSize {
266+
log.Fatalf("Expected a number greater than 33 for %s env variable but got %d", ballastEnvVarName, ballastSize)
267+
}
281268
}
282-
log.Printf("Set ballast to %s MiB", ballastSize)
269+
270+
os.Setenv(ballastEnvVarName, strconv.Itoa(ballastSize))
271+
log.Printf("Set ballast to %d MiB", ballastSize)
272+
return ballastSize
283273
}
284274

285275
// Validate and set the memory limit
286-
func setMemoryLimit(memTotalSizeMiB int) {
287-
memLimit := 0
288-
// Check if the memory limit is specified via the env var
289-
memoryLimit := os.Getenv(memLimitMiBEnvVarName)
290-
// If not, calculate it from memTotalSizeMiB
291-
if memoryLimit == "" {
292-
memLimit = memTotalSizeMiB * defaultMemoryLimitPercentage / 100
293-
} else {
294-
memLimit, _ = strconv.Atoi(memoryLimit)
295-
}
276+
func setMemoryLimit(memTotalSizeMiB int) int {
277+
memLimit := memTotalSizeMiB * defaultMemoryLimitPercentage / 100
296278

297-
// Validate memoryLimit is sane
298-
args := os.Args[1:]
299-
_, b := getKeyValue(args, "--mem-ballast-size-mib")
300-
ballastSize, _ := strconv.Atoi(b)
301-
if (ballastSize * 2) > memLimit {
302-
log.Fatalf("Memory limit (%v) is less than 2x ballast (%v). Increase memory limit or decrease ballast size.", memLimit, ballastSize)
279+
// Check if the memory limit is specified via the env var, if so, validate and set properly.
280+
if os.Getenv(memLimitMiBEnvVarName) != "" {
281+
memLimit = envVarAsInt(memLimitMiBEnvVarName)
303282
}
304283

305284
os.Setenv(memLimitMiBEnvVarName, strconv.Itoa(memLimit))
306285
log.Printf("Set memory limit to %d MiB", memLimit)
286+
return memLimit
307287
}
308288

309289
// Returns a ParserProvider that reads configuration YAML from an environment variable when applicable.
@@ -332,3 +312,13 @@ func runInteractive(params service.CollectorSettings) error {
332312

333313
return nil
334314
}
315+
316+
func envVarAsInt(env string) int {
317+
envVal := os.Getenv(env)
318+
// Check if it is a numeric value.
319+
val, err := strconv.Atoi(envVal)
320+
if err != nil {
321+
log.Fatalf("Expected a number in %s env variable but got %s", env, envVal)
322+
}
323+
return val
324+
}

0 commit comments

Comments
 (0)