Skip to content

Commit

Permalink
Track bytes tracked by conn stats per protocol.
Browse files Browse the repository at this point in the history
Summary: Track the total number of bytes received or sent per protocol. This will aid in verifying the data loss metrics, and also should be a useful metric on its own.

Test Plan: Deployed to testing saw the conn_stats_bytes metrics.

Reviewers: vihang, nserrino, #stirling

Reviewed By: vihang

Subscribers: yzhao

Signed-off-by: James Bartlett <jamesbartlett@pixielabs.ai>

Differential Revision: https://phab.corp.pixielabs.ai/D11419

GitOrigin-RevId: 1a9bbc1
  • Loading branch information
JamesMBartlett authored and copybaranaut committed May 12, 2022
1 parent 9f1b8aa commit 9156d6e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/stirling/source_connectors/socket_tracer/conn_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "src/stirling/source_connectors/socket_tracer/bcc_bpf_intf/go_grpc_types.hpp"
#include "src/stirling/source_connectors/socket_tracer/conn_stats.h"
#include "src/stirling/source_connectors/socket_tracer/conn_trackers_manager.h"
#include "src/stirling/source_connectors/socket_tracer/metrics.h"
#include "src/stirling/utils/enum_map.h"

DEFINE_bool(treat_loopback_as_in_cluster, true,
Expand Down Expand Up @@ -176,6 +177,19 @@ void ConnTracker::AddDataEvent(std::unique_ptr<SocketDataEvent> event) {
}
}

namespace {
void UpdateProtocolMetrics(traffic_protocol_t protocol, const conn_stats_event_t& event,
const ConnTracker::ConnStatsTracker& conn_stats) {
auto& metrics = SocketTracerMetrics::GetProtocolMetrics(protocol);
if (event.rd_bytes > conn_stats.bytes_recv()) {
metrics.conn_stats_bytes.Increment(event.rd_bytes - conn_stats.bytes_recv());
}
if (event.wr_bytes > conn_stats.bytes_sent()) {
metrics.conn_stats_bytes.Increment(event.wr_bytes - conn_stats.bytes_sent());
}
}
} // namespace

void ConnTracker::AddConnStats(const conn_stats_event_t& event) {
SetRole(event.role, "inferred from conn_stats event");
SetRemoteAddr(event.addr, "conn_stats event");
Expand All @@ -191,6 +205,8 @@ void ConnTracker::AddConnStats(const conn_stats_event_t& event) {
DCHECK_GE(event.rd_bytes, conn_stats_.bytes_recv());
DCHECK_GE(event.wr_bytes, conn_stats_.bytes_sent());

UpdateProtocolMetrics(protocol_, event, conn_stats_);

conn_stats_.set_bytes_recv(event.rd_bytes);
conn_stats_.set_bytes_sent(event.wr_bytes);
conn_stats_.set_closed(event.conn_events & CONN_CLOSE);
Expand Down
6 changes: 3 additions & 3 deletions src/stirling/source_connectors/socket_tracer/conn_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ class ConnTracker : NotCopyMoveable {

void set_bytes_sent(int64_t bytes_sent) { bytes_sent_ = bytes_sent; }

int64_t bytes_recv() { return bytes_recv_; }
int64_t bytes_sent() { return bytes_sent_; }
bool closed() { return closed_; }
int64_t bytes_recv() const { return bytes_recv_; }
int64_t bytes_sent() const { return bytes_sent_; }
bool closed() const { return closed_; }

bool OpenSinceLastRead() {
bool val = true - last_reported_open_;
Expand Down
7 changes: 6 additions & 1 deletion src/stirling/source_connectors/socket_tracer/metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ SocketTracerMetrics::SocketTracerMetrics(prometheus::Registry* registry,
.Help("Total bytes of data loss for this protocol. Measured by bytes that weren't "
"successfully parsed.")
.Register(*registry)
.Add({{"protocol", std::string(magic_enum::enum_name(protocol))}})) {}
.Add({{"protocol", std::string(magic_enum::enum_name(protocol))}})),
conn_stats_bytes(prometheus::BuildCounter()
.Name("conn_stats_bytes")
.Help("Total bytes of data tracked by conn stats for this protocol.")
.Register(*registry)
.Add({{"protocol", std::string(magic_enum::enum_name(protocol))}})) {}

namespace {
std::unordered_map<traffic_protocol_t, std::unique_ptr<SocketTracerMetrics>> g_protocol_metrics;
Expand Down
1 change: 1 addition & 0 deletions src/stirling/source_connectors/socket_tracer/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace stirling {
struct SocketTracerMetrics {
SocketTracerMetrics(prometheus::Registry* registry, traffic_protocol_t protocol);
prometheus::Counter& data_loss_bytes;
prometheus::Counter& conn_stats_bytes;

static SocketTracerMetrics& GetProtocolMetrics(traffic_protocol_t protocol);

Expand Down

0 comments on commit 9156d6e

Please sign in to comment.