Skip to content

Commit 7662fa9

Browse files
committed
Create a dedicated output namespace
1 parent 85cb9c6 commit 7662fa9

14 files changed

+59
-53
lines changed

collector/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
file(GLOB COLLECTOR_LIB_SRC_FILES
22
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
33
${CMAKE_CURRENT_SOURCE_DIR}/system-inspector/*.cpp
4+
${CMAKE_CURRENT_SOURCE_DIR}/output/*.cpp
45
)
56

67
add_library(collector_lib ${DRIVER_HEADERS} ${COLLECTOR_LIB_SRC_FILES})

collector/lib/CollectorConfig.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ class CollectorConfig {
199199
}
200200

201201
protected:
202-
FRIEND_TEST(SensorClientFormatterTest, NoProcessArguments);
203-
204202
int scrape_interval_;
205203
CollectionMethod collection_method_;
206204
bool turn_off_scrape_;

collector/lib/CollectorService.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
#include "CivetWrapper.h"
88
#include "CollectorConfig.h"
9-
#include "CollectorOutput.h"
109
#include "CollectorStatsExporter.h"
1110
#include "ConfigLoader.h"
1211
#include "Control.h"
1312
#include "NetworkStatusInspector.h"
1413
#include "NetworkStatusNotifier.h"
14+
#include "output/Output.h"
1515
#include "system-inspector/Service.h"
1616

1717
namespace collector {
@@ -34,7 +34,7 @@ class CollectorService {
3434
bool WaitForGRPCServer();
3535

3636
CollectorConfig& config_;
37-
CollectorOutput output_;
37+
output::Output output_;
3838
system_inspector::Service system_inspector_;
3939

4040
std::atomic<ControlValue>* control_;

collector/lib/ProcessSignalHandler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include "CollectorConfig.h"
77
#include "ProcessSignalFormatter.h"
88
#include "RateLimit.h"
9-
#include "SensorClientFormatter.h"
109
#include "SignalHandler.h"
10+
#include "output/SensorClientFormatter.h"
1111
#include "system-inspector/Service.h"
1212

1313
// forward declarations
@@ -21,7 +21,7 @@ class ProcessSignalHandler : public SignalHandler {
2121
public:
2222
ProcessSignalHandler(
2323
sinsp* inspector,
24-
CollectorOutput* client,
24+
output::Output* client,
2525
system_inspector::Stats* stats,
2626
const CollectorConfig& config)
2727
: client_(client),
@@ -49,9 +49,9 @@ class ProcessSignalHandler : public SignalHandler {
4949
Result HandleSensorSignal(sinsp_evt* evt);
5050
Result HandleExistingProcessSensor(sinsp_threadinfo* tinfo);
5151

52-
CollectorOutput* client_;
52+
output::Output* client_;
5353
ProcessSignalFormatter signal_formatter_;
54-
SensorClientFormatter sensor_formatter_;
54+
output::SensorClientFormatter sensor_formatter_;
5555
system_inspector::Stats* stats_;
5656
RateLimitCache rate_limiter_;
5757
};

collector/lib/CollectorOutput.cpp renamed to collector/lib/output/Output.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include "CollectorOutput.h"
1+
#include "Output.h"
22

33
#include "GRPCUtil.h"
44

5-
namespace collector {
5+
namespace collector::output {
66

7-
CollectorOutput::CollectorOutput(const CollectorConfig& config)
7+
Output::Output(const CollectorConfig& config)
88
: use_sensor_client_(!config.UseLegacyServices()) {
99
if (config.grpc_channel != nullptr) {
1010
channel_ = config.grpc_channel;
@@ -31,13 +31,13 @@ CollectorOutput::CollectorOutput(const CollectorConfig& config)
3131
thread_.Start([this] { EstablishGrpcStream(); });
3232
}
3333

34-
void CollectorOutput::HandleOutputError() {
34+
void Output::HandleOutputError() {
3535
CLOG(ERROR) << "GRPC stream interrupted";
3636
stream_active_.store(false, std::memory_order_release);
3737
stream_interrupted_.notify_one();
3838
}
3939

40-
SignalHandler::Result CollectorOutput::SensorOutput(const sensor::ProcessSignal& msg) {
40+
SignalHandler::Result Output::SensorOutput(const sensor::ProcessSignal& msg) {
4141
for (auto& client : sensor_clients_) {
4242
auto res = client->SendMsg(msg);
4343
switch (res) {
@@ -57,7 +57,7 @@ SignalHandler::Result CollectorOutput::SensorOutput(const sensor::ProcessSignal&
5757
return SignalHandler::PROCESSED;
5858
}
5959

60-
SignalHandler::Result CollectorOutput::SignalOutput(const sensor::SignalStreamMessage& msg) {
60+
SignalHandler::Result Output::SignalOutput(const sensor::SignalStreamMessage& msg) {
6161
for (auto& client : signal_clients_) {
6262
auto res = client->PushSignals(msg);
6363
switch (res) {
@@ -77,7 +77,7 @@ SignalHandler::Result CollectorOutput::SignalOutput(const sensor::SignalStreamMe
7777
return SignalHandler::PROCESSED;
7878
}
7979

80-
SignalHandler::Result CollectorOutput::SendMsg(const MessageType& msg) {
80+
SignalHandler::Result Output::SendMsg(const MessageType& msg) {
8181
auto visitor = [this](auto&& m) {
8282
using T = std::decay_t<decltype(m)>;
8383
if constexpr (std::is_same_v<T, sensor::ProcessSignal>) {
@@ -93,13 +93,13 @@ SignalHandler::Result CollectorOutput::SendMsg(const MessageType& msg) {
9393
return std::visit(visitor, msg);
9494
}
9595

96-
void CollectorOutput::EstablishGrpcStream() {
96+
void Output::EstablishGrpcStream() {
9797
while (EstablishGrpcStreamSingle()) {
9898
}
9999
CLOG(INFO) << "Service client terminating.";
100100
}
101101

102-
bool CollectorOutput::EstablishGrpcStreamSingle() {
102+
bool Output::EstablishGrpcStreamSingle() {
103103
std::mutex mtx;
104104
std::unique_lock<std::mutex> lock(mtx);
105105
stream_interrupted_.wait(lock, [this]() { return !stream_active_.load(std::memory_order_acquire) || thread_.should_stop(); });
@@ -134,4 +134,4 @@ bool CollectorOutput::EstablishGrpcStreamSingle() {
134134
}
135135
return true;
136136
}
137-
} // namespace collector
137+
} // namespace collector::output

collector/lib/CollectorOutput.h renamed to collector/lib/output/Output.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,29 @@
1111
#include "SignalServiceClient.h"
1212
#include "StoppableThread.h"
1313

14-
namespace collector {
14+
namespace collector::output {
1515

1616
using MessageType = std::variant<sensor::ProcessSignal, sensor::SignalStreamMessage>;
1717

18-
class CollectorOutput {
18+
class Output {
1919
public:
20-
CollectorOutput(const CollectorOutput&) = delete;
21-
CollectorOutput(CollectorOutput&&) = delete;
22-
CollectorOutput& operator=(const CollectorOutput&) = delete;
23-
CollectorOutput& operator=(CollectorOutput&&) = delete;
20+
Output(const Output&) = delete;
21+
Output(Output&&) = delete;
22+
Output& operator=(const Output&) = delete;
23+
Output& operator=(Output&&) = delete;
2424

25-
CollectorOutput(const CollectorConfig& config);
25+
Output(const CollectorConfig& config);
2626

27-
~CollectorOutput() {
27+
~Output() {
2828
stream_interrupted_.notify_one();
2929
if (thread_.running()) {
3030
thread_.Stop();
3131
}
3232
}
3333

3434
// Constructor for tests
35-
CollectorOutput(std::unique_ptr<ISensorClient>&& sensor_client,
36-
std::unique_ptr<ISignalServiceClient>&& signal_client) {
35+
Output(std::unique_ptr<ISensorClient>&& sensor_client,
36+
std::unique_ptr<ISignalServiceClient>&& signal_client) {
3737
sensor_clients_.emplace_back(std::move(sensor_client));
3838
signal_clients_.emplace_back(std::move(signal_client));
3939
}
@@ -76,6 +76,6 @@ class CollectorOutput {
7676
std::shared_ptr<grpc::Channel> channel_;
7777
};
7878

79-
} // namespace collector
79+
} // namespace collector::output
8080

8181
#endif // COLLECTOR_OUTPUT_H

collector/lib/SensorClient.cpp renamed to collector/lib/output/SensorClient.cpp

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

33
#include "Logging.h"
44

5-
namespace collector {
5+
namespace collector::output {
66
bool SensorClient::Recreate() {
77
context_ = std::make_unique<grpc::ClientContext>();
88
writer_ = DuplexClient::CreateWithReadsIgnored(&sensor::CollectorService::Stub::AsyncPushProcesses, channel_, context_.get());
@@ -44,4 +44,4 @@ SignalHandler::Result SensorClient::SendMsg(const sensor::ProcessSignal& msg) {
4444

4545
return SignalHandler::PROCESSED;
4646
}
47-
} // namespace collector
47+
} // namespace collector::output

collector/lib/SensorClient.h renamed to collector/lib/output/SensorClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "DuplexGRPC.h"
1111
#include "SignalHandler.h"
1212

13-
namespace collector {
13+
namespace collector::output {
1414

1515
class ISensorClient {
1616
public:
@@ -83,6 +83,6 @@ class SensorClientStdout : public ISensorClient {
8383
}
8484
};
8585

86-
} // namespace collector
86+
} // namespace collector::output
8787

8888
#endif //_SENSOR_CLIENT_H_

collector/lib/SensorClientFormatter.cpp renamed to collector/lib/output/SensorClientFormatter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "Utility.h"
1414
#include "system-inspector/EventExtractor.h"
1515

16-
namespace collector {
16+
namespace collector::output {
1717

1818
using SignalStreamMessage = sensor::SignalStreamMessage;
1919
using Signal = SensorClientFormatter::Signal;
@@ -331,4 +331,4 @@ std::vector<LineageInfo> SensorClientFormatter::GetProcessLineage(sinsp_threadin
331331
return lineage;
332332
}
333333

334-
} // namespace collector
334+
} // namespace collector::output

collector/lib/SensorClientFormatter.h renamed to collector/lib/output/SensorClientFormatter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace collector::system_inspector {
2020
class EventExtractor;
2121
}
2222

23-
namespace collector {
23+
namespace collector::output {
2424

2525
class SensorClientFormatter : public ProtoSignalFormatter<sensor::ProcessSignal> {
2626
public:
@@ -109,6 +109,6 @@ class SensorClientFormatter : public ProtoSignalFormatter<sensor::ProcessSignal>
109109
const CollectorConfig& config_;
110110
};
111111

112-
} // namespace collector
112+
} // namespace collector::output
113113

114114
#endif // SENSOR_CLIENT_FORMATTER_H

collector/lib/system-inspector/Service.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
#include <google/protobuf/util/time_util.h>
1414

15-
#include "CollectionMethod.h"
1615
#include "CollectorException.h"
17-
#include "CollectorOutput.h"
1816
#include "CollectorStats.h"
1917
#include "ContainerEngine.h"
2018
#include "ContainerMetadata.h"
@@ -35,7 +33,7 @@ namespace collector::system_inspector {
3533

3634
Service::~Service() = default;
3735

38-
Service::Service(const CollectorConfig& config, CollectorOutput* client)
36+
Service::Service(const CollectorConfig& config, output::Output* client)
3937
: inspector_(std::make_unique<sinsp>(true)),
4038
container_metadata_inspector_(std::make_unique<ContainerMetadata>(inspector_.get())),
4139
default_formatter_(std::make_unique<sinsp_evt_formatter>(

collector/lib/system-inspector/Service.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
#include <gtest/gtest_prod.h>
1010

11-
#include "CollectorOutput.h"
1211
#include "ContainerMetadata.h"
1312
#include "Control.h"
1413
#include "SignalHandler.h"
1514
#include "SystemInspector.h"
15+
#include "output/Output.h"
1616

1717
// forward declarations
1818
class sinsp;
@@ -30,7 +30,7 @@ class Service : public SystemInspector {
3030
Service& operator=(Service&&) = delete;
3131
~Service() override;
3232

33-
Service(const CollectorConfig& config, CollectorOutput* client);
33+
Service(const CollectorConfig& config, output::Output* client);
3434
void Start() override;
3535
void Run(const std::atomic<ControlValue>& control) override;
3636
void CleanUp() override;

collector/test/CollectorOutputTest.cpp renamed to collector/test/OutputTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include <gmock/gmock.h>
22
#include <gtest/gtest.h>
33

4-
#include "CollectorOutput.h"
5-
#include "SensorClient.h"
64
#include "SignalServiceClient.h"
5+
#include "output/Output.h"
6+
#include "output/SensorClient.h"
77

8-
namespace collector {
8+
namespace collector::output {
99
class MockSensorClient : public ISensorClient {
1010
public:
1111
MOCK_METHOD(bool, Recreate, ());
@@ -34,7 +34,7 @@ TEST_F(CollectorOutputTest, SensorClient) {
3434

3535
EXPECT_CALL(*sensor_client, SendMsg).Times(1).WillOnce(testing::Return(SignalHandler::PROCESSED));
3636

37-
CollectorOutput output{std::move(sensor_client), std::move(signal_client)};
37+
Output output{std::move(sensor_client), std::move(signal_client)};
3838
auto result = output.SendMsg(msg);
3939

4040
EXPECT_EQ(result, SignalHandler::PROCESSED);
@@ -45,10 +45,10 @@ TEST_F(CollectorOutputTest, SignalClient) {
4545

4646
EXPECT_CALL(*signal_client, PushSignals).Times(1).WillOnce(testing::Return(SignalHandler::PROCESSED));
4747

48-
CollectorOutput output{std::move(sensor_client), std::move(signal_client)};
48+
Output output{std::move(sensor_client), std::move(signal_client)};
4949

5050
auto result = output.SendMsg(msg);
5151

5252
EXPECT_EQ(result, SignalHandler::PROCESSED);
5353
}
54-
} // namespace collector
54+
} // namespace collector::output

collector/test/SensorClientFormatterTest.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#include "libsinsp/sinsp.h"
77

88
#include "CollectorStats.h"
9-
#include "SensorClientFormatter.h"
109
#include "Utility.h"
10+
#include "output/SensorClientFormatter.h"
1111

12-
namespace collector {
12+
namespace collector::output {
1313

1414
using LineageInfo = SensorClientFormatter::LineageInfo;
1515

@@ -23,6 +23,15 @@ struct ThreadInfoParams {
2323
std::string exepath;
2424
};
2525

26+
class MockCollectorConfig : public CollectorConfig {
27+
public:
28+
MockCollectorConfig() = default;
29+
30+
void SetDisableProcessArguments(bool value) {
31+
disable_process_arguments_ = value;
32+
}
33+
};
34+
2635
#define EXPECT_STATS_COUNTER(index, expected) \
2736
EXPECT_EQ(CollectorStats::GetOrCreate().GetCounter(index), expected)
2837

@@ -47,7 +56,7 @@ class SensorClientFormatterTest : public testing::Test {
4756
}
4857

4958
std::unique_ptr<sinsp> inspector;
50-
CollectorConfig config;
59+
MockCollectorConfig config;
5160
SensorClientFormatter formatter;
5261
};
5362

@@ -317,7 +326,7 @@ TEST_F(SensorClientFormatterTest, ProcessArguments) {
317326
}
318327

319328
TEST_F(SensorClientFormatterTest, NoProcessArguments) {
320-
config.disable_process_arguments_ = true;
329+
config.SetDisableProcessArguments(true);
321330

322331
// {pid, tid, ptid, vpid, uid, container_id, exepath},
323332
auto tinfo = build_threadinfo({3, 3, -1, 0, 42, "", "qwerty"});
@@ -336,4 +345,4 @@ TEST_F(SensorClientFormatterTest, NoProcessArguments) {
336345
EXPECT_TRUE(signal->args().empty());
337346
}
338347

339-
} // namespace collector
348+
} // namespace collector::output

0 commit comments

Comments
 (0)