@@ -27,59 +27,67 @@ namespace o2
2727namespace monitoring
2828{
2929
30- void addInfoLogger (Monitoring* monitoring, http::url uri) {
31- monitoring-> addBackend ( std::make_unique<backends::InfoLoggerBackend>() );
30+ std::unique_ptr<Backend> getInfoLogger ( http::url uri) {
31+ return std::make_unique<backends::InfoLoggerBackend>();
3232}
33- void addInfluxDb (Monitoring* monitoring, http::url uri) {
33+
34+ std::unique_ptr<Backend> getInfluxDb (http::url uri) {
3435 auto const position = uri.protocol .find_last_of (' -' );
3536 if (position != std::string::npos) {
3637 uri.protocol = uri.protocol .substr (position + 1 );
3738 }
3839 if (uri.protocol == " udp" ) {
39- monitoring-> addBackend ( std::make_unique<backends::InfluxDB>(uri.host , uri.port ) );
40+ return std::make_unique<backends::InfluxDB>(uri.host , uri.port );
4041 }
4142 if (uri.protocol == " http" ) {
42- monitoring-> addBackend ( std::make_unique<backends::InfluxDB>(uri.host , uri.port , uri.search ) );
43+ return std::make_unique<backends::InfluxDB>(uri.host , uri.port , uri.search );
4344 }
45+ throw std::runtime_error (" InfluxDB transport protocol not supported" );
4446}
45- void addApMon (Monitoring* monitoring, http::url uri) {
47+
48+ std::unique_ptr<Backend> getApMon (http::url uri) {
4649#ifdef _WITH_APPMON
47- monitoring-> addBackend ( std::make_unique<backends::ApMonBackend>(uri.path ) );
50+ return std::make_unique<backends::ApMonBackend>(uri.path );
4851#else
4952 throw std::runtime_error (" ApMon backend is not enabled" );
5053#endif
5154}
52- void addFlume (Monitoring* monitoring, http::url uri) {
53- monitoring->addBackend (std::make_unique<backends::Flume>(uri.host , uri.port ));
55+
56+ std::unique_ptr<Backend> getFlume (http::url uri) {
57+ return std::make_unique<backends::Flume>(uri.host , uri.port );
58+ }
59+
60+ std::unique_ptr<Backend> MonitoringFactory::GetBackend (std::string& url) {
61+ static const std::map<std::string, std::function<std::unique_ptr<Backend>(const http::url&)>> map = {
62+ {" infologger" , getInfoLogger},
63+ {" influxdb-udp" , getInfluxDb},
64+ {" influxdb-http" , getInfluxDb},
65+ {" apmon" , getApMon},
66+ {" flume" , getFlume},
67+ };
68+
69+ http::url parsedUrl = http::ParseHttpUrl (url);
70+ if (parsedUrl.protocol .empty ()) {
71+ throw std::runtime_error (" Ill-formed URI" );
72+ }
73+
74+ auto iterator = map.find (parsedUrl.protocol );
75+ if (iterator != map.end ()) {
76+ return iterator->second (parsedUrl);
77+ } else {
78+ throw std::runtime_error (" Unrecognized backend " + parsedUrl.protocol );
79+ }
5480}
5581
5682std::unique_ptr<Monitoring> MonitoringFactory::Get (std::string urlsString)
5783{
58- static const std::map<std::string, std::function<void (Monitoring* monitoring, const http::url&)>> map = {
59- {" infologger" , addInfoLogger},
60- {" influxdb-udp" , addInfluxDb},
61- {" influxdb-http" , addInfluxDb},
62- {" apmon" , addApMon},
63- {" flume" , addFlume},
64- };
65-
6684 auto monitoring = std::make_unique<Monitoring>();
6785
6886 std::vector<std::string> urls;
6987 boost::split (urls, urlsString, boost::is_any_of (" ," ));
7088
7189 for (auto url : urls) {
72- http::url parsedUrl = http::ParseHttpUrl (url);
73- if (parsedUrl.protocol .empty ()) {
74- throw std::runtime_error (" Ill-formed URI" );
75- }
76-
77- auto iterator = map.find (parsedUrl.protocol );
78- if (iterator != map.end ()) {
79- iterator->second (monitoring.get (), parsedUrl);
80- } else {
81- throw std::runtime_error (" Unrecognized backend " + parsedUrl.protocol );
82- }
90+ monitoring->addBackend (MonitoringFactory::GetBackend (url));
8391 }
8492 return monitoring;
8593}
0 commit comments