Skip to content

Commit a28524e

Browse files
committed
Simplify handling of stats and sinsp configuration
1 parent bce847f commit a28524e

File tree

3 files changed

+46
-86
lines changed

3 files changed

+46
-86
lines changed

collector/lib/CollectorConfig.cpp

Lines changed: 21 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <filesystem>
44
#include <optional>
5-
#include <sstream>
65

76
#include <libsinsp/sinsp.h>
87

@@ -49,9 +48,18 @@ BoolEnvVar collect_connection_status("ROX_COLLECT_CONNECTION_STATUS", true);
4948

5049
BoolEnvVar enable_external_ips("ROX_ENABLE_EXTERNAL_IPS", false);
5150

51+
// Stats and metrics configuration
5252
BoolEnvVar enable_connection_stats("ROX_COLLECTOR_ENABLE_CONNECTION_STATS", true);
53-
5453
BoolEnvVar enable_detailed_metrics("ROX_COLLECTOR_ENABLE_DETAILED_METRICS", true);
54+
StringListEnvVar connection_stats_quantiles("ROX_COLLECTOR_CONNECTION_STATS_QUANTILES");
55+
DoubleEnvVar connection_stats_error("ROX_COLLECTOR_CONNECTION_STATS_ERROR", 0.01);
56+
UIntEnvVar connection_stats_window("ROX_COLLECTOR_CONNECTION_STATS_WINDOW", 60);
57+
58+
// Sinsp configuration
59+
UIntEnvVar sinsp_cpu_per_buffer("ROX_COLLECTOR_SINSP_CPU_PER_BUFFER", DEFAULT_CPU_FOR_EACH_BUFFER);
60+
UIntEnvVar sinsp_buffer_size("ROX_COLLECTOR_SINSP_BUFFER_SIZE", DEFAULT_DRIVER_BUFFER_BYTES_DIM);
61+
UIntEnvVar sinsp_total_buffer_size("ROX_COLLECTOR_SINSP_TOTAL_BUFFER_SIZE", 512 * 1024 * 1024);
62+
UIntEnvVar sinsp_thread_cache_size("ROX_COLLECTOR_SINSP_TOTAL_BUFFER_SIZE", 32768);
5563

5664
BoolEnvVar enable_runtime_config("ROX_COLLECTOR_RUNTIME_CONFIG_ENABLED", false);
5765

@@ -97,7 +105,13 @@ CollectorConfig::CollectorConfig(CollectorArgs* args)
97105
use_docker_ce_(use_docker_ce),
98106
use_podman_ce_(use_podman_ce),
99107
enable_introspection_(enable_introspection),
100-
track_send_recv_(track_send_recv) {
108+
track_send_recv_(track_send_recv),
109+
connection_stats_error_(connection_stats_error),
110+
connection_stats_window_(connection_stats_window),
111+
sinsp_cpu_per_buffer_(sinsp_cpu_per_buffer),
112+
sinsp_buffer_size_(sinsp_buffer_size),
113+
sinsp_total_buffer_size_(sinsp_total_buffer_size),
114+
sinsp_thread_cache_size_(sinsp_thread_cache_size) {
101115
// Get hostname
102116
hostname_ = GetHostname();
103117
if (hostname_.empty()) {
@@ -117,8 +131,7 @@ CollectorConfig::CollectorConfig(CollectorArgs* args)
117131
HandleArgs(args);
118132
HandleNetworkConfig();
119133
HandleAfterglowEnvVars();
120-
HandleConnectionStatsEnvVars();
121-
HandleSinspEnvVars();
134+
HandleConnectionStatsQuantiles();
122135
HandleConfig(config_file.value());
123136

124137
host_config_ = ProcessHostHeuristics(*this);
@@ -296,17 +309,12 @@ void CollectorConfig::HandleAfterglowEnvVars() {
296309
CLOG(INFO) << "Afterglow is " << (enable_afterglow_ ? "enabled" : "disabled");
297310
}
298311

299-
void CollectorConfig::HandleConnectionStatsEnvVars() {
300-
const char* envvar;
301-
312+
void CollectorConfig::HandleConnectionStatsQuantiles() {
302313
connection_stats_quantiles_ = {0.50, 0.90, 0.95};
303314

304-
if ((envvar = std::getenv("ROX_COLLECTOR_CONNECTION_STATS_QUANTILES")) != NULL) {
315+
if (connection_stats_quantiles.hasValue()) {
305316
connection_stats_quantiles_.clear();
306-
std::stringstream quantiles(envvar);
307-
while (quantiles.good()) {
308-
std::string quantile;
309-
std::getline(quantiles, quantile, ',');
317+
for (const auto& quantile : connection_stats_quantiles.value()) {
310318
try {
311319
double v = std::stod(quantile);
312320
connection_stats_quantiles_.push_back(v);
@@ -316,69 +324,6 @@ void CollectorConfig::HandleConnectionStatsEnvVars() {
316324
}
317325
}
318326
}
319-
320-
if ((envvar = std::getenv("ROX_COLLECTOR_CONNECTION_STATS_ERROR")) != NULL) {
321-
try {
322-
connection_stats_error_ = std::stod(envvar);
323-
CLOG(INFO) << "Connection statistics error value: " << connection_stats_error_;
324-
} catch (...) {
325-
CLOG(ERROR) << "Invalid quantile error value: '" << envvar << "'";
326-
}
327-
}
328-
329-
if ((envvar = std::getenv("ROX_COLLECTOR_CONNECTION_STATS_WINDOW")) != NULL) {
330-
try {
331-
connection_stats_window_ = std::stoi(envvar);
332-
CLOG(INFO) << "Connection statistics window: " << connection_stats_window_;
333-
} catch (...) {
334-
CLOG(ERROR) << "Invalid window length value: '" << envvar << "'";
335-
}
336-
}
337-
}
338-
339-
void CollectorConfig::HandleSinspEnvVars() {
340-
const char* envvar;
341-
342-
sinsp_cpu_per_buffer_ = DEFAULT_CPU_FOR_EACH_BUFFER;
343-
sinsp_buffer_size_ = DEFAULT_DRIVER_BUFFER_BYTES_DIM;
344-
// the default values for sinsp_thread_cache_size_, sinsp_total_buffer_size_
345-
// are not picked up from Falco, but set in the CollectorConfig class.
346-
347-
if ((envvar = std::getenv("ROX_COLLECTOR_SINSP_CPU_PER_BUFFER")) != NULL) {
348-
try {
349-
sinsp_cpu_per_buffer_ = std::stoi(envvar);
350-
CLOG(INFO) << "Sinsp cpu per buffer: " << sinsp_cpu_per_buffer_;
351-
} catch (...) {
352-
CLOG(ERROR) << "Invalid cpu per buffer value: '" << envvar << "'";
353-
}
354-
}
355-
356-
if ((envvar = std::getenv("ROX_COLLECTOR_SINSP_BUFFER_SIZE")) != NULL) {
357-
try {
358-
sinsp_buffer_size_ = std::stoll(envvar);
359-
CLOG(INFO) << "Sinsp buffer size: " << sinsp_buffer_size_;
360-
} catch (...) {
361-
CLOG(ERROR) << "Invalid buffer size value: '" << envvar << "'";
362-
}
363-
}
364-
365-
if ((envvar = std::getenv("ROX_COLLECTOR_SINSP_TOTAL_BUFFER_SIZE")) != NULL) {
366-
try {
367-
sinsp_total_buffer_size_ = std::stoll(envvar);
368-
CLOG(INFO) << "Sinsp total buffer size: " << sinsp_total_buffer_size_;
369-
} catch (...) {
370-
CLOG(ERROR) << "Invalid total buffer size value: '" << envvar << "'";
371-
}
372-
}
373-
374-
if ((envvar = std::getenv("ROX_COLLECTOR_SINSP_THREAD_CACHE_SIZE")) != NULL) {
375-
try {
376-
sinsp_thread_cache_size_ = std::stoll(envvar);
377-
CLOG(INFO) << "Sinsp thread cache size: " << sinsp_thread_cache_size_;
378-
} catch (...) {
379-
CLOG(ERROR) << "Invalid thread cache size value: '" << envvar << "'";
380-
}
381-
}
382327
}
383328

384329
bool CollectorConfig::YamlConfigToConfig(YAML::Node& yamlConfig) {

collector/lib/CollectorConfig.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ class CollectorConfig {
141141
std::vector<std::string> syscalls_;
142142
std::string hostname_;
143143
std::string host_proc_;
144-
bool disable_network_flows_ = false;
144+
bool disable_network_flows_;
145145
bool scrape_listen_endpoints_;
146146
UnorderedSet<L4ProtoPortPair> ignored_l4proto_port_pairs_;
147147
std::vector<IPNet> ignored_networks_;
148148
std::vector<IPNet> non_aggregated_networks_;
149149

150150
HostConfig host_config_;
151-
int64_t afterglow_period_micros_ = 300'000'000; // 5 minutes in microseconds
151+
int64_t afterglow_period_micros_;
152152
bool enable_afterglow_ = false;
153153
bool enable_core_dump_;
154154
bool enable_processes_listening_on_ports_;
@@ -163,26 +163,26 @@ class CollectorConfig {
163163
bool enable_introspection_;
164164
bool track_send_recv_;
165165
std::vector<double> connection_stats_quantiles_;
166-
double connection_stats_error_ = 0.01;
167-
unsigned int connection_stats_window_ = 60;
166+
double connection_stats_error_;
167+
unsigned int connection_stats_window_;
168168

169169
// URL to the GRPC server
170170
std::optional<std::string> grpc_server_;
171171

172172
// One ring buffer will be initialized for this many CPUs
173-
unsigned int sinsp_cpu_per_buffer_ = 0;
173+
unsigned int sinsp_cpu_per_buffer_;
174174
// Size of one ring buffer, in bytes.
175-
unsigned int sinsp_buffer_size_ = 0;
175+
unsigned int sinsp_buffer_size_;
176176
// Allowed size of all ring buffers, in bytes. The default value 512Mi is
177177
// based on the default memory limit set of the Collector DaemonSet, which is
178178
// 1Gi.
179-
unsigned int sinsp_total_buffer_size_ = 512 * 1024 * 1024;
179+
unsigned int sinsp_total_buffer_size_;
180180

181181
// Max size of the thread cache. This parameter essentially translated into
182182
// the upper boundary for memory consumption. Note that Falco puts it's own
183183
// upper limit on top of this value, m_thread_table_absolute_max_size, which
184184
// is 2^17 (131072) and twice as large.
185-
unsigned int sinsp_thread_cache_size_ = 32768;
185+
unsigned int sinsp_thread_cache_size_;
186186

187187
std::optional<TlsConfig> tls_config_;
188188

@@ -198,8 +198,7 @@ class CollectorConfig {
198198
// Methods for complex configurations
199199
void HandleNetworkConfig();
200200
void HandleAfterglowEnvVars();
201-
void HandleConnectionStatsEnvVars();
202-
void HandleSinspEnvVars();
201+
void HandleConnectionStatsQuantiles();
203202
bool YamlConfigToConfig(YAML::Node& yamlConfig);
204203
void HandleConfig(const std::filesystem::path& filePath);
205204

collector/lib/EnvVar.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ struct ParseInt {
104104
}
105105
};
106106

107+
struct ParseUInt {
108+
bool operator()(unsigned int* out, const std::string& str_val) {
109+
*out = std::stoul(str_val);
110+
return true;
111+
}
112+
};
113+
107114
struct ParsePath {
108115
bool operator()(std::filesystem::path* out, const std::string& str_val) {
109116
*out = str_val;
@@ -117,14 +124,23 @@ struct ParseFloat {
117124
return true;
118125
}
119126
};
127+
128+
struct ParseDouble {
129+
bool operator()(double* out, const std::string& str_val) {
130+
*out = std::stod(str_val);
131+
return true;
132+
}
133+
};
120134
} // namespace internal
121135

122136
using BoolEnvVar = EnvVar<bool, internal::ParseBool>;
123137
using StringListEnvVar = EnvVar<std::vector<std::string>, internal::ParseStringList>;
124138
using StringEnvVar = EnvVar<std::string, internal::ParseString>;
125139
using IntEnvVar = EnvVar<int, internal::ParseInt>;
140+
using UIntEnvVar = EnvVar<unsigned int, internal::ParseUInt>;
126141
using PathEnvVar = EnvVar<std::filesystem::path, internal::ParsePath>;
127142
using FloatEnvVar = EnvVar<float, internal::ParseFloat>;
143+
using DoubleEnvVar = EnvVar<double, internal::ParseDouble>;
128144

129145
} // namespace collector
130146

0 commit comments

Comments
 (0)