Skip to content

Commit 07e7d65

Browse files
authored
Move transport creating to factory (#185)
* Move transport creating to factory * Fix clang-format
1 parent 933bc8c commit 07e7d65

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

src/Backends/InfluxDB.cxx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
///
1515

1616
#include "InfluxDB.h"
17+
#include <sstream>
1718
#include <string>
1819
#include <variant>
19-
#include "../Transports/UDP.h"
20-
#include "../Transports/Unix.h"
20+
#include <boost/algorithm/string/replace.hpp>
2121
#include "../Exceptions/MonitoringException.h"
2222

2323
namespace o2
@@ -36,19 +36,14 @@ struct overloaded : Ts... {
3636
template <class... Ts>
3737
overloaded(Ts...)->overloaded<Ts...>;
3838

39-
InfluxDB::InfluxDB(const std::string& host, unsigned int port) : mTransport(std::make_unique<transports::UDP>(host, port))
39+
InfluxDB::InfluxDB(std::unique_ptr<transports::TransportInterface> transport)
40+
: mTransport(std::move(transport))
4041
{
41-
MonLogger::Get() << "InfluxDB/UDP backend initialized"
42-
<< " (" << host << ":" << port << ")" << MonLogger::End();
42+
MonLogger::Get() << "InfluxDB backend initialized" << MonLogger::End();
4343
}
4444

4545
InfluxDB::InfluxDB() {}
4646

47-
InfluxDB::InfluxDB(const std::string& socketPath) : mTransport(std::make_unique<transports::Unix>(socketPath))
48-
{
49-
MonLogger::Get() << "InfluxDB/Unix backend initialized (" << socketPath << ")" << MonLogger::End();
50-
}
51-
5247
inline unsigned long InfluxDB::convertTimestamp(const std::chrono::time_point<std::chrono::system_clock>& timestamp)
5348
{
5449
return std::chrono::duration_cast<std::chrono::nanoseconds>(

src/Backends/InfluxDB.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,13 @@ namespace backends
3737
class InfluxDB final : public Backend
3838
{
3939
public:
40-
/// Constructor for UDP transport
41-
/// \param host InfluxDB UDP endpoint hostname
42-
/// \param port InfluxDB UDP endpoint port number
43-
InfluxDB(const std::string& host, unsigned int port);
40+
/// Constuctor
41+
/// \param transport Any available transport (udp, unix, kafka)
42+
InfluxDB(std::unique_ptr<transports::TransportInterface> transport);
4443

4544
/// Constructor for other backends
4645
InfluxDB();
4746

48-
/// Constructor for Unix socket transport
49-
InfluxDB(const std::string& socketPath);
50-
5147
/// Default destructor
5248
~InfluxDB() = default;
5349

src/MonitoringFactory.cxx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222

2323
#include "Backends/StdOut.h"
2424
#include "Backends/Noop.h"
25-
2625
#include "Backends/InfluxDB.h"
2726

27+
#include "Transports/UDP.h"
28+
#include "Transports/Unix.h"
29+
2830
#ifdef O2_MONITORING_WITH_APPMON
2931
#include "Backends/ApMonBackend.h"
3032
#endif
@@ -75,18 +77,20 @@ std::unique_ptr<Backend> getInfluxDb(http::url uri)
7577
uri.protocol = uri.protocol.substr(position + 1);
7678
}
7779
if (uri.protocol == "udp") {
78-
return std::make_unique<backends::InfluxDB>(uri.host, uri.port);
80+
auto transport = std::make_unique<transports::UDP>(uri.host, uri.port);
81+
return std::make_unique<backends::InfluxDB>(std::move(transport));
7982
}
8083
if (uri.protocol == "unix") {
8184
std::string path = uri.path;
82-
;
8385
auto found = std::find_if(begin(verbosities), end(verbosities),
8486
[&](const auto& s) { return path.find(s.first) != std::string::npos; });
8587
if (found != end(verbosities)) {
8688
path.erase(path.rfind('/'));
8789
}
88-
return std::make_unique<backends::InfluxDB>(path);
90+
auto transport = std::make_unique<transports::Unix>(path);
91+
return std::make_unique<backends::InfluxDB>(std::move(transport));
8992
}
93+
9094
throw std::runtime_error("InfluxDB transport protocol not supported");
9195
}
9296

src/Transports/UDP.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "UDP.h"
1717
#include <string>
18+
#include "../MonLogger.h"
1819

1920
namespace o2
2021
{
@@ -31,6 +32,7 @@ UDP::UDP(const std::string& hostname, int port) : mSocket(mIoService, boost::asi
3132
boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), hostname, std::to_string(port));
3233
boost::asio::ip::udp::resolver::iterator resolverInerator = resolver.resolve(query);
3334
mEndpoint = *resolverInerator;
35+
MonLogger::Get() << "UDP transport initialized (" << hostname << ":" << port << ")" << MonLogger::End();
3436
}
3537

3638
void UDP::send(std::string&& message)

src/Transports/Unix.cxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "Unix.h"
1717
#include <string>
18+
#include "../MonLogger.h"
1819

1920
namespace o2
2021
{
@@ -28,6 +29,7 @@ namespace transports
2829
Unix::Unix(const std::string& socketPath) : mSocket(mIoService), mEndpoint(socketPath)
2930
{
3031
mSocket.open();
32+
MonLogger::Get() << "Unix socket transport initialized (" << socketPath << ")" << MonLogger::End();
3133
}
3234

3335
void Unix::send(std::string&& message)

test/testInfluxDb.cxx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define BOOST_TEST_MODULE Test Monitoring InfluxDB
1414
#define BOOST_TEST_DYN_LINK
1515
#include <boost/test/unit_test.hpp>
16+
#include "../src/Transports/UDP.h"
1617
#include "../src/Backends/InfluxDB.h"
1718

1819
namespace o2
@@ -26,7 +27,8 @@ BOOST_AUTO_TEST_CASE(simplySendMetric)
2627
{
2728
std::string url = "influxdb-udp://localhost:1000";
2829
auto parsed = http::ParseHttpUrl(url);
29-
o2::monitoring::backends::InfluxDB influxBackend(parsed.host, parsed.port);
30+
auto transport = std::make_unique<transports::UDP>(parsed.host, parsed.port);
31+
o2::monitoring::backends::InfluxDB influxBackend(std::move(transport));
3032
o2::monitoring::Metric metric{10, "myCrazyMetric"};
3133
influxBackend.send(metric);
3234
}

0 commit comments

Comments
 (0)