Skip to content

Commit

Permalink
Report connections statistics at each reporting interval.
Browse files Browse the repository at this point in the history
  • Loading branch information
ovalenti committed Oct 20, 2023
1 parent 3649bd7 commit 01e26e7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
7 changes: 5 additions & 2 deletions collector/lib/CollectorService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ void CollectorService::RunForever() {
prometheus::Exposer exposer("9090");
exposer.RegisterCollectable(registry);

CollectorStatsExporter exporter(registry, &config_, &sysdig_);

std::unique_ptr<NetworkStatusNotifier> net_status_notifier;

CLOG(INFO) << "Network scrape interval set to " << config_.ScrapeInterval() << " seconds";
Expand Down Expand Up @@ -85,11 +87,12 @@ void CollectorService::RunForever() {

net_status_notifier = MakeUnique<NetworkStatusNotifier>(conn_scraper, config_.ScrapeInterval(), config_.ScrapeListenEndpoints(), config_.TurnOffScrape(),
conn_tracker, config_.AfterglowPeriod(), config_.EnableAfterglow(),
network_connection_info_service_comm);
network_connection_info_service_comm,
exporter.GetConnectionsTotalReporter(),
exporter.GetConnectionsRateReporter());
net_status_notifier->Start();
}

CollectorStatsExporter exporter(registry, &config_, &sysdig_);
if (!exporter.start()) {
CLOG(FATAL) << "Unable to start collector stats exporter";
}
Expand Down
42 changes: 42 additions & 0 deletions collector/lib/NetworkStatusNotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,44 @@ void NetworkStatusNotifier::WaitUntilWriterStarted(IDuplexClientWriter<sensor::N
CLOG(INFO) << "Established network connection info stream.";
}

void NetworkStatusNotifier::ReportConnectionStats() {
if (connections_total_reporter_) {
//
// Total number of connections stored in ConnTracker
//
ConnectionTracker::Stats stats_total = conn_tracker_->GetConnectionStats_StoredConnections();

connections_total_reporter_->Observe(
stats_total.inbound.private_,
stats_total.inbound.public_,
stats_total.outbound.private_,
stats_total.outbound.public_);
}

if (connections_rate_reporter_) {
//
// Rate of connections creation since last iteration (reporting interval)
//
ConnectionTracker::Stats stats_new_counter = conn_tracker_->GetConnectionStats_NewConnectionCounters();

auto now = std::chrono::steady_clock::now();
auto delta_t = std::chrono::duration_cast<std::chrono::seconds>(now - connections_last_report_time_).count();

if (connections_rate_counter_last_ // skip the first call
&& delta_t > 0) { // division by zero assertion

connections_rate_reporter_->Observe(
((float)stats_new_counter.inbound.private_ - connections_rate_counter_last_->inbound.private_) / delta_t,
((float)stats_new_counter.inbound.public_ - connections_rate_counter_last_->inbound.public_) / delta_t,
((float)stats_new_counter.outbound.private_ - connections_rate_counter_last_->outbound.private_) / delta_t,
((float)stats_new_counter.outbound.public_ - connections_rate_counter_last_->outbound.public_) / delta_t);
}

connections_rate_counter_last_ = stats_new_counter;
connections_last_report_time_ = now;
}
}

bool NetworkStatusNotifier::UpdateAllConnsAndEndpoints() {
if (turn_off_scraping_) {
return true;
Expand Down Expand Up @@ -195,6 +233,8 @@ void NetworkStatusNotifier::RunSingle(IDuplexClientWriter<sensor::NetworkConnect
continue;
}

ReportConnectionStats();

const sensor::NetworkConnectionInfoMessage* msg;
ConnMap new_conn_state;
AdvertisedEndpointMap new_cep_state;
Expand Down Expand Up @@ -240,6 +280,8 @@ void NetworkStatusNotifier::RunSingleAfterglow(IDuplexClientWriter<sensor::Netwo
continue;
}

ReportConnectionStats();

int64_t time_micros = NowMicros();
const sensor::NetworkConnectionInfoMessage* msg;
AdvertisedEndpointMap new_cep_state;
Expand Down
21 changes: 19 additions & 2 deletions collector/lib/NetworkStatusNotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ class NetworkStatusNotifier : protected ProtoAllocator<sensor::NetworkConnection
public:
NetworkStatusNotifier(std::shared_ptr<IConnScraper> conn_scraper, int scrape_interval, bool scrape_listen_endpoints, bool turn_off_scrape,
std::shared_ptr<ConnectionTracker> conn_tracker, int64_t afterglow_period_micros, bool use_afterglow,
std::shared_ptr<INetworkConnectionInfoServiceComm> comm)
: conn_scraper_(conn_scraper), scrape_interval_(scrape_interval), turn_off_scraping_(turn_off_scrape), scrape_listen_endpoints_(scrape_listen_endpoints), conn_tracker_(std::move(conn_tracker)), afterglow_period_micros_(afterglow_period_micros), enable_afterglow_(use_afterglow), comm_(comm) {
std::shared_ptr<INetworkConnectionInfoServiceComm> comm,
std::shared_ptr<CollectorConnectionStats<unsigned int>> connections_total_reporter = 0,
std::shared_ptr<CollectorConnectionStats<float>> connections_rate_reporter = 0)
: conn_scraper_(conn_scraper),
scrape_interval_(scrape_interval),
turn_off_scraping_(turn_off_scrape),
scrape_listen_endpoints_(scrape_listen_endpoints),
conn_tracker_(std::move(conn_tracker)),
afterglow_period_micros_(afterglow_period_micros),
enable_afterglow_(use_afterglow),
comm_(comm),
connections_total_reporter_(connections_total_reporter),
connections_rate_reporter_(connections_rate_reporter) {
}

void Start();
Expand Down Expand Up @@ -54,6 +65,12 @@ class NetworkStatusNotifier : protected ProtoAllocator<sensor::NetworkConnection
int64_t afterglow_period_micros_;
bool enable_afterglow_;
std::shared_ptr<INetworkConnectionInfoServiceComm> comm_;

std::shared_ptr<CollectorConnectionStats<unsigned int>> connections_total_reporter_;
std::shared_ptr<CollectorConnectionStats<float>> connections_rate_reporter_;
std::chrono::steady_clock::time_point connections_last_report_time_; // time delta between the current reporting and the previous (rate computation)
std::optional<ConnectionTracker::Stats> connections_rate_counter_last_; // previous counter values (rate computation)
void ReportConnectionStats();
};

} // namespace collector
Expand Down

0 comments on commit 01e26e7

Please sign in to comment.