diff --git a/collector/lib/CMakeLists.txt b/collector/lib/CMakeLists.txt index 1d5386e7d1..ba1acfd491 100644 --- a/collector/lib/CMakeLists.txt +++ b/collector/lib/CMakeLists.txt @@ -1,6 +1,8 @@ file(GLOB COLLECTOR_LIB_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/system-inspector/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/output/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/output/*/*.cpp ) add_library(collector_lib ${DRIVER_HEADERS} ${COLLECTOR_LIB_SRC_FILES}) diff --git a/collector/lib/CollectorConfig.h b/collector/lib/CollectorConfig.h index a7d357ca95..29294d2ada 100644 --- a/collector/lib/CollectorConfig.h +++ b/collector/lib/CollectorConfig.h @@ -193,8 +193,6 @@ class CollectorConfig { } protected: - FRIEND_TEST(SensorClientFormatterTest, NoProcessArguments); - int scrape_interval_; CollectionMethod collection_method_; bool turn_off_scrape_; diff --git a/collector/lib/CollectorService.h b/collector/lib/CollectorService.h index 551d211fd8..2612a77e7b 100644 --- a/collector/lib/CollectorService.h +++ b/collector/lib/CollectorService.h @@ -5,12 +5,12 @@ #include "CivetWrapper.h" #include "CollectorConfig.h" -#include "CollectorOutput.h" #include "CollectorStatsExporter.h" #include "ConfigLoader.h" #include "Control.h" #include "NetworkStatusInspector.h" #include "NetworkStatusNotifier.h" +#include "output/Output.h" #include "system-inspector/Service.h" namespace collector { @@ -33,7 +33,7 @@ class CollectorService { bool WaitForGRPCServer(); CollectorConfig& config_; - CollectorOutput output_; + output::Output output_; system_inspector::Service system_inspector_; std::atomic* control_; diff --git a/collector/lib/ProcessSignalHandler.h b/collector/lib/ProcessSignalHandler.h index de6833d494..10b385ca9a 100644 --- a/collector/lib/ProcessSignalHandler.h +++ b/collector/lib/ProcessSignalHandler.h @@ -5,8 +5,8 @@ #include "CollectorConfig.h" #include "ProcessSignalFormatter.h" #include "RateLimit.h" -#include "SensorClientFormatter.h" #include "SignalHandler.h" +#include "output/Formatter.h" #include "system-inspector/Service.h" // forward declarations @@ -20,7 +20,7 @@ class ProcessSignalHandler : public SignalHandler { public: ProcessSignalHandler( sinsp* inspector, - CollectorOutput* client, + output::Output* client, system_inspector::Stats* stats, const CollectorConfig& config) : client_(client), @@ -48,9 +48,9 @@ class ProcessSignalHandler : public SignalHandler { Result HandleSensorSignal(sinsp_evt* evt); Result HandleExistingProcessSensor(sinsp_threadinfo* tinfo); - CollectorOutput* client_; + output::Output* client_; ProcessSignalFormatter signal_formatter_; - SensorClientFormatter sensor_formatter_; + output::Formatter sensor_formatter_; system_inspector::Stats* stats_; RateLimitCache rate_limiter_; }; diff --git a/collector/lib/SensorClientFormatter.cpp b/collector/lib/output/Formatter.cpp similarity index 88% rename from collector/lib/SensorClientFormatter.cpp rename to collector/lib/output/Formatter.cpp index c7b6f27e1d..04b101e77d 100644 --- a/collector/lib/SensorClientFormatter.cpp +++ b/collector/lib/output/Formatter.cpp @@ -1,4 +1,4 @@ -#include "SensorClientFormatter.h" +#include "Formatter.h" #include @@ -13,12 +13,12 @@ #include "Utility.h" #include "system-inspector/EventExtractor.h" -namespace collector { +namespace collector::output { using SignalStreamMessage = sensor::SignalStreamMessage; -using Signal = SensorClientFormatter::Signal; -using ProcessSignal = SensorClientFormatter::ProcessSignal; -using LineageInfo = SensorClientFormatter::LineageInfo; +using Signal = Formatter::Signal; +using ProcessSignal = Formatter::ProcessSignal; +using LineageInfo = Formatter::LineageInfo; using Timestamp = google::protobuf::Timestamp; using TimeUtil = google::protobuf::util::TimeUtil; @@ -57,7 +57,7 @@ std::string extract_proc_args(sinsp_threadinfo* tinfo) { } // namespace -SensorClientFormatter::SensorClientFormatter(sinsp* inspector, const CollectorConfig& config) +Formatter::Formatter(sinsp* inspector, const CollectorConfig& config) : event_names_(EventNames::GetInstance()), event_extractor_(std::make_unique()), container_metadata_(inspector), @@ -65,9 +65,9 @@ SensorClientFormatter::SensorClientFormatter(sinsp* inspector, const CollectorCo event_extractor_->Init(inspector); } -SensorClientFormatter::~SensorClientFormatter() = default; +Formatter::~Formatter() = default; -const sensor::ProcessSignal* SensorClientFormatter::ToProtoMessage(sinsp_evt* event) { +const sensor::ProcessSignal* Formatter::ToProtoMessage(sinsp_evt* event) { if (process_signals[event->get_type()] == ProcessSignalType::UNKNOWN_PROCESS_TYPE) { return nullptr; } @@ -82,7 +82,7 @@ const sensor::ProcessSignal* SensorClientFormatter::ToProtoMessage(sinsp_evt* ev return CreateProcessSignal(event); } -const sensor::ProcessSignal* SensorClientFormatter::ToProtoMessage(sinsp_threadinfo* tinfo) { +const sensor::ProcessSignal* Formatter::ToProtoMessage(sinsp_threadinfo* tinfo) { Reset(); if (!ValidateProcessDetails(tinfo)) { CLOG(INFO) << "Dropping process event: " << tinfo; @@ -92,7 +92,7 @@ const sensor::ProcessSignal* SensorClientFormatter::ToProtoMessage(sinsp_threadi return CreateProcessSignal(tinfo); } -ProcessSignal* SensorClientFormatter::CreateProcessSignal(sinsp_evt* event) { +ProcessSignal* Formatter::CreateProcessSignal(sinsp_evt* event) { auto signal = AllocateRoot(); // set id @@ -174,7 +174,7 @@ ProcessSignal* SensorClientFormatter::CreateProcessSignal(sinsp_evt* event) { return signal; } -ProcessSignal* SensorClientFormatter::CreateProcessSignal(sinsp_threadinfo* tinfo) { +ProcessSignal* Formatter::CreateProcessSignal(sinsp_threadinfo* tinfo) { auto signal = AllocateRoot(); // set id @@ -237,7 +237,7 @@ ProcessSignal* SensorClientFormatter::CreateProcessSignal(sinsp_threadinfo* tinf return signal; } -std::string SensorClientFormatter::ToString(sinsp_evt* event) { +std::string Formatter::ToString(sinsp_evt* event) { std::stringstream ss; const std::string* path = event_extractor_->get_exepath(event); const std::string* name = event_extractor_->get_comm(event); @@ -254,7 +254,7 @@ std::string SensorClientFormatter::ToString(sinsp_evt* event) { return ss.str(); } -bool SensorClientFormatter::ValidateProcessDetails(const sinsp_threadinfo* tinfo) { +bool Formatter::ValidateProcessDetails(const sinsp_threadinfo* tinfo) { if (tinfo == nullptr) { return false; } @@ -266,11 +266,11 @@ bool SensorClientFormatter::ValidateProcessDetails(const sinsp_threadinfo* tinfo return true; } -bool SensorClientFormatter::ValidateProcessDetails(sinsp_evt* event) { +bool Formatter::ValidateProcessDetails(sinsp_evt* event) { return ValidateProcessDetails(event->get_thread_info()); } -void SensorClientFormatter::UpdateLineageStats(const std::vector& lineage) { +void Formatter::UpdateLineageStats(const std::vector& lineage) { int string_total = std::accumulate(lineage.cbegin(), lineage.cend(), 0, [](int acc, const auto& l) { return acc + l.parent_exec_file_path().size(); }); @@ -281,7 +281,7 @@ void SensorClientFormatter::UpdateLineageStats(const std::vector& l COUNTER_ADD(CollectorStats::process_lineage_string_total, string_total); } -std::vector SensorClientFormatter::GetProcessLineage(sinsp_threadinfo* tinfo) { +std::vector Formatter::GetProcessLineage(sinsp_threadinfo* tinfo) { std::vector lineage; if (tinfo == nullptr) { return lineage; @@ -331,4 +331,4 @@ std::vector SensorClientFormatter::GetProcessLineage(sinsp_threadin return lineage; } -} // namespace collector +} // namespace collector::output diff --git a/collector/lib/SensorClientFormatter.h b/collector/lib/output/Formatter.h similarity index 84% rename from collector/lib/SensorClientFormatter.h rename to collector/lib/output/Formatter.h index d518242e94..347fa7dcbb 100644 --- a/collector/lib/SensorClientFormatter.h +++ b/collector/lib/output/Formatter.h @@ -19,17 +19,17 @@ namespace collector::system_inspector { class EventExtractor; } -namespace collector { +namespace collector::output { -class SensorClientFormatter : public ProtoSignalFormatter { +class Formatter : public ProtoSignalFormatter { public: - SensorClientFormatter(const SensorClientFormatter&) = delete; - SensorClientFormatter(SensorClientFormatter&&) = delete; - SensorClientFormatter& operator=(const SensorClientFormatter&) = delete; - SensorClientFormatter& operator=(SensorClientFormatter&&) = delete; - virtual ~SensorClientFormatter(); + Formatter(const Formatter&) = delete; + Formatter(Formatter&&) = delete; + Formatter& operator=(const Formatter&) = delete; + Formatter& operator=(Formatter&&) = delete; + virtual ~Formatter(); - SensorClientFormatter(sinsp* inspector, const CollectorConfig& config); + Formatter(sinsp* inspector, const CollectorConfig& config); using Signal = v1::Signal; using ProcessSignal = sensor::ProcessSignal; @@ -108,4 +108,4 @@ class SensorClientFormatter : public ProtoSignalFormatter const CollectorConfig& config_; }; -} // namespace collector +} // namespace collector::output diff --git a/collector/lib/output/IClient.h b/collector/lib/output/IClient.h new file mode 100644 index 0000000000..a4299d7da8 --- /dev/null +++ b/collector/lib/output/IClient.h @@ -0,0 +1,42 @@ +#ifndef OUTPUT_ICLIENT_H +#define OUTPUT_ICLIENT_H + +#include "internalapi/sensor/collector_iservice.grpc.pb.h" + +#include "SignalHandler.h" + +namespace collector::output { + +class IClient { + public: + using Service = sensor::CollectorService; + + IClient() = default; + IClient(const IClient&) = default; + IClient(IClient&&) = delete; + IClient& operator=(const IClient&) = default; + IClient& operator=(IClient&&) = delete; + virtual ~IClient() = default; + + /** + * Recreate the internal state of the object to allow communication. + * + * Mostly useful for handling gRPC reconnections. + * + * @returns true if the refresh was succesful, false otherwise. + */ + virtual bool Recreate() = 0; + + /** + * Send a message to sensor through the iservice. + * + * @param msg The message to be sent to sensor. + * @returns A SignalHandler::Result with the outcome of the send + * operation. + */ + virtual SignalHandler::Result SendMsg(const sensor::ProcessSignal& msg) = 0; +}; + +} // namespace collector::output + +#endif // OUTPUT_ICLIENT_H diff --git a/collector/lib/CollectorOutput.cpp b/collector/lib/output/Output.cpp similarity index 81% rename from collector/lib/CollectorOutput.cpp rename to collector/lib/output/Output.cpp index a315cda020..2c6d1db294 100644 --- a/collector/lib/CollectorOutput.cpp +++ b/collector/lib/output/Output.cpp @@ -1,17 +1,18 @@ -#include "CollectorOutput.h" +#include "Output.h" #include "GRPCUtil.h" -#include "HostInfo.h" +#include "output/grpc/Client.h" +#include "output/log/Client.h" -namespace collector { +namespace collector::output { -CollectorOutput::CollectorOutput(const CollectorConfig& config) +Output::Output(const CollectorConfig& config) : use_sensor_client_(!config.UseLegacyServices()) { if (config.grpc_channel != nullptr) { channel_ = config.grpc_channel; if (use_sensor_client_) { - auto sensor_client = std::make_unique(channel_); + auto sensor_client = std::make_unique(channel_); sensor_clients_.emplace_back(std::move(sensor_client)); } else { auto signal_client = std::make_unique(channel_); @@ -21,7 +22,7 @@ CollectorOutput::CollectorOutput(const CollectorConfig& config) if (config.grpc_channel == nullptr || config.UseStdout()) { if (use_sensor_client_) { - auto sensor_client = std::make_unique(); + auto sensor_client = std::make_unique(); sensor_clients_.emplace_back(std::move(sensor_client)); } else { auto signal_client = std::make_unique(); @@ -32,13 +33,13 @@ CollectorOutput::CollectorOutput(const CollectorConfig& config) thread_.Start([this] { EstablishGrpcStream(); }); } -void CollectorOutput::HandleOutputError() { +void Output::HandleOutputError() { CLOG(ERROR) << "GRPC stream interrupted"; stream_active_.store(false, std::memory_order_release); stream_interrupted_.notify_one(); } -SignalHandler::Result CollectorOutput::SensorOutput(const sensor::ProcessSignal& msg) { +SignalHandler::Result Output::SensorOutput(const sensor::ProcessSignal& msg) { for (auto& client : sensor_clients_) { auto res = client->SendMsg(msg); switch (res) { @@ -58,7 +59,7 @@ SignalHandler::Result CollectorOutput::SensorOutput(const sensor::ProcessSignal& return SignalHandler::PROCESSED; } -SignalHandler::Result CollectorOutput::SignalOutput(const sensor::SignalStreamMessage& msg) { +SignalHandler::Result Output::SignalOutput(const sensor::SignalStreamMessage& msg) { for (auto& client : signal_clients_) { auto res = client->PushSignals(msg); switch (res) { @@ -78,7 +79,7 @@ SignalHandler::Result CollectorOutput::SignalOutput(const sensor::SignalStreamMe return SignalHandler::PROCESSED; } -SignalHandler::Result CollectorOutput::SendMsg(const MessageType& msg) { +SignalHandler::Result Output::SendMsg(const MessageType& msg) { auto visitor = [this](auto&& m) { using T = std::decay_t; if constexpr (std::is_same_v) { @@ -94,13 +95,13 @@ SignalHandler::Result CollectorOutput::SendMsg(const MessageType& msg) { return std::visit(visitor, msg); } -void CollectorOutput::EstablishGrpcStream() { +void Output::EstablishGrpcStream() { while (EstablishGrpcStreamSingle()) { } CLOG(INFO) << "Service client terminating."; } -bool CollectorOutput::EstablishGrpcStreamSingle() { +bool Output::EstablishGrpcStreamSingle() { std::mutex mtx; std::unique_lock lock(mtx); stream_interrupted_.wait(lock, [this]() { return !stream_active_.load(std::memory_order_acquire) || thread_.should_stop(); }); @@ -135,4 +136,4 @@ bool CollectorOutput::EstablishGrpcStreamSingle() { } return true; } -} // namespace collector +} // namespace collector::output diff --git a/collector/lib/CollectorOutput.h b/collector/lib/output/Output.h similarity index 74% rename from collector/lib/CollectorOutput.h rename to collector/lib/output/Output.h index ebb3309e30..e30b88e79b 100644 --- a/collector/lib/CollectorOutput.h +++ b/collector/lib/output/Output.h @@ -5,25 +5,25 @@ #include "internalapi/sensor/signal_iservice.pb.h" #include "CollectorConfig.h" -#include "SensorClient.h" +#include "IClient.h" #include "SignalHandler.h" #include "SignalServiceClient.h" #include "StoppableThread.h" -namespace collector { +namespace collector::output { using MessageType = std::variant; -class CollectorOutput { +class Output { public: - CollectorOutput(const CollectorOutput&) = delete; - CollectorOutput(CollectorOutput&&) = delete; - CollectorOutput& operator=(const CollectorOutput&) = delete; - CollectorOutput& operator=(CollectorOutput&&) = delete; + Output(const Output&) = delete; + Output(Output&&) = delete; + Output& operator=(const Output&) = delete; + Output& operator=(Output&&) = delete; - CollectorOutput(const CollectorConfig& config); + Output(const CollectorConfig& config); - ~CollectorOutput() { + ~Output() { stream_interrupted_.notify_one(); if (thread_.running()) { thread_.Stop(); @@ -31,8 +31,8 @@ class CollectorOutput { } // Constructor for tests - CollectorOutput(std::unique_ptr&& sensor_client, - std::unique_ptr&& signal_client) { + Output(std::unique_ptr&& sensor_client, + std::unique_ptr&& signal_client) { sensor_clients_.emplace_back(std::move(sensor_client)); signal_clients_.emplace_back(std::move(signal_client)); } @@ -64,7 +64,7 @@ class CollectorOutput { SignalHandler::Result SensorOutput(const sensor::ProcessSignal& msg); SignalHandler::Result SignalOutput(const sensor::SignalStreamMessage& msg); - std::vector> sensor_clients_; + std::vector> sensor_clients_; std::vector> signal_clients_; bool use_sensor_client_ = true; @@ -75,4 +75,4 @@ class CollectorOutput { std::shared_ptr channel_; }; -} // namespace collector +} // namespace collector::output diff --git a/collector/lib/SensorClient.h b/collector/lib/output/SensorClient.h similarity index 97% rename from collector/lib/SensorClient.h rename to collector/lib/output/SensorClient.h index f5bebdac85..eac98b886e 100644 --- a/collector/lib/SensorClient.h +++ b/collector/lib/output/SensorClient.h @@ -9,7 +9,7 @@ #include "DuplexGRPC.h" #include "SignalHandler.h" -namespace collector { +namespace collector::output { class ISensorClient { public: @@ -82,4 +82,4 @@ class SensorClientStdout : public ISensorClient { } }; -} // namespace collector +} // namespace collector::output diff --git a/collector/lib/SensorClient.cpp b/collector/lib/output/grpc/Client.cpp similarity index 82% rename from collector/lib/SensorClient.cpp rename to collector/lib/output/grpc/Client.cpp index 2d3a7f21be..5e584174e4 100644 --- a/collector/lib/SensorClient.cpp +++ b/collector/lib/output/grpc/Client.cpp @@ -1,10 +1,10 @@ -#include "SensorClient.h" +#include "Client.h" #include "Logging.h" -namespace collector { -bool SensorClient::Recreate() { - context_ = std::make_unique(); +namespace collector::output::grpc { +bool Client::Recreate() { + context_ = std::make_unique<::grpc::ClientContext>(); writer_ = DuplexClient::CreateWithReadsIgnored(&sensor::CollectorService::Stub::AsyncPushProcesses, channel_, context_.get()); if (!writer_->WaitUntilStarted(std::chrono::seconds(30))) { CLOG(ERROR) << "Signal stream not ready after 30 seconds. Retrying ..."; @@ -18,7 +18,7 @@ bool SensorClient::Recreate() { return true; } -SignalHandler::Result SensorClient::SendMsg(const sensor::ProcessSignal& msg) { +SignalHandler::Result Client::SendMsg(const sensor::ProcessSignal& msg) { if (!stream_active_.load(std::memory_order_acquire)) { CLOG_THROTTLED(ERROR, std::chrono::seconds(10)) << "GRPC stream is not established"; @@ -44,4 +44,4 @@ SignalHandler::Result SensorClient::SendMsg(const sensor::ProcessSignal& msg) { return SignalHandler::PROCESSED; } -} // namespace collector +} // namespace collector::output::grpc diff --git a/collector/lib/output/grpc/Client.h b/collector/lib/output/grpc/Client.h new file mode 100644 index 0000000000..b7a8b851cd --- /dev/null +++ b/collector/lib/output/grpc/Client.h @@ -0,0 +1,46 @@ +#ifndef OUTPUT_GRPC_CLIENT +#define OUTPUT_GRPC_CLIENT + +#include + +#include "DuplexGRPC.h" +#include "output/IClient.h" + +namespace collector::output::grpc { + +using Channel = ::grpc::Channel; + +class Client : public IClient { + public: + using Service = sensor::CollectorService; + + Client(const Client&) = delete; + Client(Client&&) = delete; + Client& operator=(const Client&) = delete; + Client& operator=(Client&&) = delete; + ~Client() override { + context_->TryCancel(); + } + + explicit Client(std::shared_ptr channel) + : channel_(std::move(channel)) { + } + + bool Recreate() override; + + SignalHandler::Result SendMsg(const sensor::ProcessSignal& msg) override; + + private: + std::shared_ptr channel_; + + std::atomic stream_active_ = false; + + // This needs to have the same lifetime as the class. + std::unique_ptr<::grpc::ClientContext> context_; + std::unique_ptr> writer_; + + bool first_write_ = false; +}; +} // namespace collector::output::grpc + +#endif diff --git a/collector/lib/output/log/Client.h b/collector/lib/output/log/Client.h new file mode 100644 index 0000000000..dac07bce4a --- /dev/null +++ b/collector/lib/output/log/Client.h @@ -0,0 +1,19 @@ +#ifndef OUTPUT_LOG_CLIENT_H +#define OUTPUT_LOG_CLIENT_H + +#include + +#include "Utility.h" +#include "output/IClient.h" + +namespace collector::output::log { +class Client : public IClient { + bool Recreate() override { return true; } + + SignalHandler::Result SendMsg(const sensor::ProcessSignal& msg) override { + LogProtobufMessage(msg); + return SignalHandler::PROCESSED; + } +}; +} // namespace collector::output::log +#endif diff --git a/collector/lib/system-inspector/Service.cpp b/collector/lib/system-inspector/Service.cpp index 61082115b6..3700262cb1 100644 --- a/collector/lib/system-inspector/Service.cpp +++ b/collector/lib/system-inspector/Service.cpp @@ -12,9 +12,7 @@ #include -#include "CollectionMethod.h" #include "CollectorException.h" -#include "CollectorOutput.h" #include "CollectorStats.h" #include "ContainerEngine.h" #include "ContainerMetadata.h" @@ -35,7 +33,7 @@ namespace collector::system_inspector { Service::~Service() = default; -Service::Service(const CollectorConfig& config, CollectorOutput* client) +Service::Service(const CollectorConfig& config, output::Output* client) : inspector_(std::make_unique(true)), container_metadata_inspector_(std::make_unique(inspector_.get())), default_formatter_(std::make_unique( diff --git a/collector/lib/system-inspector/Service.h b/collector/lib/system-inspector/Service.h index 36731525b4..7668262137 100644 --- a/collector/lib/system-inspector/Service.h +++ b/collector/lib/system-inspector/Service.h @@ -7,11 +7,11 @@ #include -#include "CollectorOutput.h" #include "ContainerMetadata.h" #include "Control.h" #include "SignalHandler.h" #include "SystemInspector.h" +#include "output/Output.h" // forward declarations class sinsp; @@ -29,7 +29,7 @@ class Service : public SystemInspector { Service& operator=(Service&&) = delete; ~Service() override; - Service(const CollectorConfig& config, CollectorOutput* client); + Service(const CollectorConfig& config, output::Output* client); void Start() override; void Run(const std::atomic& control) override; void CleanUp() override; diff --git a/collector/test/SensorClientFormatterTest.cpp b/collector/test/OutputFormatterTest.cpp similarity index 88% rename from collector/test/SensorClientFormatterTest.cpp rename to collector/test/OutputFormatterTest.cpp index b49ab965d8..3f7e980672 100644 --- a/collector/test/SensorClientFormatterTest.cpp +++ b/collector/test/OutputFormatterTest.cpp @@ -6,12 +6,12 @@ #include "libsinsp/sinsp.h" #include "CollectorStats.h" -#include "SensorClientFormatter.h" #include "Utility.h" +#include "output/Formatter.h" -namespace collector { +namespace collector::output { -using LineageInfo = SensorClientFormatter::LineageInfo; +using LineageInfo = Formatter::LineageInfo; struct ThreadInfoParams { int64_t pid; @@ -23,6 +23,15 @@ struct ThreadInfoParams { std::string exepath; }; +class MockCollectorConfig : public CollectorConfig { + public: + MockCollectorConfig() = default; + + void SetDisableProcessArguments(bool value) { + disable_process_arguments_ = value; + } +}; + #define EXPECT_STATS_COUNTER(index, expected) \ EXPECT_EQ(CollectorStats::GetOrCreate().GetCounter(index), expected) @@ -47,13 +56,13 @@ class SensorClientFormatterTest : public testing::Test { } std::unique_ptr inspector; - CollectorConfig config; - SensorClientFormatter formatter; + MockCollectorConfig config; + Formatter formatter; }; TEST_F(SensorClientFormatterTest, NoProcessTest) { sinsp_threadinfo* tinfo = nullptr; - auto lineage = SensorClientFormatter::GetProcessLineage(tinfo); + auto lineage = Formatter::GetProcessLineage(tinfo); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 0); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 0); @@ -66,7 +75,7 @@ TEST_F(SensorClientFormatterTest, NoProcessTest) { TEST_F(SensorClientFormatterTest, ProcessWithoutParentTest) { // {pid, tid, ptid, vpid, uid, container_id, exepath}, inspector->add_thread(build_threadinfo({0, 0, -1, 2, 7, "", "qwerty"})); - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(0).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(0).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 0); @@ -87,7 +96,7 @@ TEST_F(SensorClientFormatterTest, ProcessWithParentTest) { inspector->add_thread(build_threadinfo(params)); } - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(1).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(1).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 1); @@ -111,7 +120,7 @@ TEST_F(SensorClientFormatterTest, ProcessWithParentWithPid0Test) { inspector->add_thread(build_threadinfo(params)); } - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(1).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(1).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 0); @@ -132,7 +141,7 @@ TEST_F(SensorClientFormatterTest, ProcessWithParentWithSameNameTest) { inspector->add_thread(build_threadinfo(params)); } - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(1).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(1).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 1); @@ -157,7 +166,7 @@ TEST_F(SensorClientFormatterTest, ProcessWithTwoParentsTest) { inspector->add_thread(build_threadinfo(params)); } - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(4).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(4).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 2); @@ -185,7 +194,7 @@ TEST_F(SensorClientFormatterTest, ProcessWithTwoParentsWithTheSameNameTest) { inspector->add_thread(build_threadinfo(params)); } - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(4).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(4).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 1); @@ -211,7 +220,7 @@ TEST_F(SensorClientFormatterTest, ProcessCollapseParentChildWithSameNameTest) { inspector->add_thread(build_threadinfo(params)); } - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(5).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(5).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 1); @@ -236,7 +245,7 @@ TEST_F(SensorClientFormatterTest, ProcessCollapseParentChildWithSameName2Test) { for (const auto& params : tinfo_params) { inspector->add_thread(build_threadinfo(params)); } - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(5).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(5).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 2); @@ -265,7 +274,7 @@ TEST_F(SensorClientFormatterTest, ProcessWithUnrelatedProcessTest) { inspector->add_thread(build_threadinfo(params)); } - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(4).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(4).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 1); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 2); @@ -284,11 +293,11 @@ TEST_F(SensorClientFormatterTest, ProcessWithUnrelatedProcessTest) { TEST_F(SensorClientFormatterTest, CountTwoCounterCallsTest) { // {pid, tid, ptid, vpid, uid, container_id, exepath}, inspector->add_thread(build_threadinfo({1, 1, 555, 10, 9, "", "jkl;"})); - auto lineage = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(1).get()); + auto lineage = Formatter::GetProcessLineage(inspector->get_thread_ref(1).get()); // {pid, tid, ptid, vpid, uid, container_id, exepath}, inspector->add_thread(build_threadinfo({2, 2, 555, 10, 9, "", "jkl;"})); - auto lineage2 = SensorClientFormatter::GetProcessLineage(inspector->get_thread_ref(2).get()); + auto lineage2 = Formatter::GetProcessLineage(inspector->get_thread_ref(2).get()); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_counts, 2); EXPECT_STATS_COUNTER(CollectorStats::process_lineage_total, 0); @@ -317,7 +326,7 @@ TEST_F(SensorClientFormatterTest, ProcessArguments) { } TEST_F(SensorClientFormatterTest, NoProcessArguments) { - config.disable_process_arguments_ = true; + config.SetDisableProcessArguments(true); // {pid, tid, ptid, vpid, uid, container_id, exepath}, auto tinfo = build_threadinfo({3, 3, -1, 0, 42, "", "qwerty"}); @@ -336,4 +345,4 @@ TEST_F(SensorClientFormatterTest, NoProcessArguments) { EXPECT_TRUE(signal->args().empty()); } -} // namespace collector +} // namespace collector::output diff --git a/collector/test/CollectorOutputTest.cpp b/collector/test/OutputTest.cpp similarity index 80% rename from collector/test/CollectorOutputTest.cpp rename to collector/test/OutputTest.cpp index 966b9ffd9f..4f7a184557 100644 --- a/collector/test/CollectorOutputTest.cpp +++ b/collector/test/OutputTest.cpp @@ -1,12 +1,12 @@ #include #include -#include "CollectorOutput.h" -#include "SensorClient.h" #include "SignalServiceClient.h" +#include "output/IClient.h" +#include "output/Output.h" -namespace collector { -class MockSensorClient : public ISensorClient { +namespace collector::output { +class MockSensorClient : public IClient { public: MOCK_METHOD(bool, Recreate, ()); MOCK_METHOD(SignalHandler::Result, SendMsg, (const sensor::ProcessSignal&)); @@ -34,7 +34,7 @@ TEST_F(CollectorOutputTest, SensorClient) { EXPECT_CALL(*sensor_client, SendMsg).Times(1).WillOnce(testing::Return(SignalHandler::PROCESSED)); - CollectorOutput output{std::move(sensor_client), std::move(signal_client)}; + Output output{std::move(sensor_client), std::move(signal_client)}; auto result = output.SendMsg(msg); EXPECT_EQ(result, SignalHandler::PROCESSED); @@ -45,10 +45,10 @@ TEST_F(CollectorOutputTest, SignalClient) { EXPECT_CALL(*signal_client, PushSignals).Times(1).WillOnce(testing::Return(SignalHandler::PROCESSED)); - CollectorOutput output{std::move(sensor_client), std::move(signal_client)}; + Output output{std::move(sensor_client), std::move(signal_client)}; auto result = output.SendMsg(msg); EXPECT_EQ(result, SignalHandler::PROCESSED); } -} // namespace collector +} // namespace collector::output