@@ -36,6 +36,7 @@ import (
36
36
"github.com/signalfx/splunk-otel-collector/internal/version"
37
37
)
38
38
39
+ // The list of environment variables must be the same as what is used in the yaml configs.
39
40
const (
40
41
ballastEnvVarName = "SPLUNK_BALLAST_SIZE_MIB"
41
42
configEnvVarName = "SPLUNK_CONFIG"
@@ -136,35 +137,25 @@ func checkRuntimeParams() {
136
137
checkConfig ()
137
138
138
139
// Set default total memory
139
- memTotalSizeMiB := defaultMemoryTotalMiB
140
+ memTotalSize := defaultMemoryTotalMiB
140
141
// Check if the total memory is specified via the env var
141
- memTotalEnvVarVal := os .Getenv (memTotalEnvVarName )
142
142
// If so, validate and change total memory
143
- if memTotalEnvVarVal != "" {
143
+ if os . Getenv ( memTotalEnvVarName ) != "" {
144
144
// 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 )
149
146
// 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 )
152
149
}
153
- memTotalSizeMiB = val
154
150
}
155
151
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 )
165
158
}
166
- setMemoryBallast (memTotalSizeMiB )
167
- setMemoryLimit (memTotalSizeMiB )
168
159
}
169
160
170
161
// Sets flag '--config' to specified env var SPLUNK_CONFIG, if the flag not specified.
@@ -256,54 +247,43 @@ func checkRequiredEnvVars(path string) {
256
247
}
257
248
258
249
// 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 )
271
257
}
272
- } else {
273
- ballastSize = strconv .Itoa (memTotalSizeMiB * defaultMemoryBallastPercentage / 100 )
274
- os .Setenv (ballastEnvVarName , ballastSize )
258
+ os .Setenv (ballastEnvVarName , ballastSizeFlag )
275
259
}
276
260
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
+ }
281
268
}
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
283
273
}
284
274
285
275
// 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
296
278
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 )
303
282
}
304
283
305
284
os .Setenv (memLimitMiBEnvVarName , strconv .Itoa (memLimit ))
306
285
log .Printf ("Set memory limit to %d MiB" , memLimit )
286
+ return memLimit
307
287
}
308
288
309
289
// Returns a ParserProvider that reads configuration YAML from an environment variable when applicable.
@@ -332,3 +312,13 @@ func runInteractive(params service.CollectorSettings) error {
332
312
333
313
return nil
334
314
}
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