Skip to content

Commit 176dde7

Browse files
committed
Simplify handling of stats and sinsp configuration
1 parent a651faa commit 176dde7

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

@@ -50,9 +49,18 @@ BoolEnvVar collect_connection_status("ROX_COLLECT_CONNECTION_STATUS", true);
5049

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

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

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

@@ -98,7 +106,13 @@ CollectorConfig::CollectorConfig(CollectorArgs* args)
98106
use_docker_ce_(use_docker_ce),
99107
use_podman_ce_(use_podman_ce),
100108
enable_introspection_(enable_introspection),
101-
track_send_recv_(track_send_recv) {
109+
track_send_recv_(track_send_recv),
110+
connection_stats_error_(connection_stats_error),
111+
connection_stats_window_(connection_stats_window),
112+
sinsp_cpu_per_buffer_(sinsp_cpu_per_buffer),
113+
sinsp_buffer_size_(sinsp_buffer_size),
114+
sinsp_total_buffer_size_(sinsp_total_buffer_size),
115+
sinsp_thread_cache_size_(sinsp_thread_cache_size) {
102116
// Get hostname
103117
hostname_ = GetHostname();
104118
if (hostname_.empty()) {
@@ -118,8 +132,7 @@ CollectorConfig::CollectorConfig(CollectorArgs* args)
118132
HandleArgs(args);
119133
HandleNetworkConfig();
120134
HandleAfterglowEnvVars();
121-
HandleConnectionStatsEnvVars();
122-
HandleSinspEnvVars();
135+
HandleConnectionStatsQuantiles();
123136
HandleConfig(config_file.value());
124137

125138
host_config_ = ProcessHostHeuristics(*this);
@@ -290,17 +303,12 @@ void CollectorConfig::HandleAfterglowEnvVars() {
290303
CLOG(INFO) << "Afterglow is " << (enable_afterglow_ ? "enabled" : "disabled");
291304
}
292305

293-
void CollectorConfig::HandleConnectionStatsEnvVars() {
294-
const char* envvar;
295-
306+
void CollectorConfig::HandleConnectionStatsQuantiles() {
296307
connection_stats_quantiles_ = {0.50, 0.90, 0.95};
297308

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

378323
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)