Skip to content

Commit 0647d84

Browse files
authored
[OMON-417] Make sure to crash on backend init but not on send (#228)
1 parent 2c0b6aa commit 0647d84

File tree

9 files changed

+31
-34
lines changed

9 files changed

+31
-34
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ endif()
1717

1818
# Define project
1919
project(Monitoring
20-
VERSION 3.5.2
20+
VERSION 3.6.0
2121
DESCRIPTION "O2 Monitoring library"
2222
LANGUAGES CXX
2323
)

src/Backends/InfluxDB.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,12 @@ void InfluxDB::send(std::vector<Metric>&& metrics)
6666
//remove last \n
6767
if (influxMetrics.size() > 0) influxMetrics.pop_back();
6868

69-
70-
try {
71-
mTransport->send(std::move(influxMetrics));
72-
} catch (MonitoringException&) {
73-
}
69+
mTransport->send(std::move(influxMetrics));
7470
}
7571

7672
void InfluxDB::send(const Metric& metric)
7773
{
78-
try {
79-
mTransport->send(toInfluxLineProtocol(metric));
80-
} catch (MonitoringException&) {
81-
}
74+
mTransport->send(toInfluxLineProtocol(metric));
8275
}
8376

8477
std::string InfluxDB::toInfluxLineProtocol(const Metric& metric)

src/Exceptions/MonitoringException.cxx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ namespace o2
2222
namespace monitoring
2323
{
2424

25-
MonitoringException::MonitoringException(const std::string& source, const std::string& message) : MonitoringException(-1, source, message) {}
26-
27-
MonitoringException::MonitoringException(int code, const std::string& source, const std::string& message)
25+
MonitoringException::MonitoringException(const std::string& source, const std::string& message)
2826
: message(message)
2927
{
30-
MonLogger::Get(Severity::Warn) << "MonitoringException["
31-
<< source << "] (" << code << "): " << message << MonLogger::End();
28+
MonLogger::Get(Severity::Error) << "MonitoringException["
29+
<< source << "] : " << message << MonLogger::End();
3230
}
3331

3432
const char* MonitoringException::what() const throw()

src/Exceptions/MonitoringException.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class MonitoringException : public std::exception
3030
{
3131
public:
3232
MonitoringException(const std::string& source, const std::string& message);
33-
MonitoringException(int code, const std::string& source, const std::string& message);
3433
~MonitoringException() = default;
3534
const char* what() const throw();
3635

src/MonLogger.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ class MonLogger
7777
void setDate()
7878
{
7979
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
80-
mStream << std::put_time(std::localtime(&now), "%Y-%m-%d %X") << "\t";
80+
mStream << std::put_time(std::localtime(&now), "%Y-%m-%d %X") << "\t" << "Monitoring" << "\t";
8181
}
8282

8383
/// Sets log color based on severity
8484
/// \param severity - log severity
8585
void setSeverity(Severity severity)
8686
{
87-
mStream << "\033[1;" << static_cast<int>(severity) << "m";
87+
mStream << "\033[0;" << static_cast<int>(severity) << "m";
8888
}
8989

9090
/// Delete copy and move constructors

src/MonitoringFactory.cxx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
///
1515

1616
#include "Monitoring/MonitoringFactory.h"
17+
#include "Exceptions/MonitoringException.h"
1718
#include <functional>
18-
#include <stdexcept>
1919
#include <boost/algorithm/string.hpp>
2020
#include "MonLogger.h"
2121
#include "UriParser/UriParser.h"
@@ -87,7 +87,7 @@ std::unique_ptr<Backend> getInfluxDbv2(http::url uri)
8787
transport->addHeader("Authorization: Token " + token);
8888
return std::make_unique<backends::InfluxDB>(std::move(transport));
8989
#else
90-
throw std::runtime_error("HTTP transport is not enabled");
90+
throw MonitoringException("Factory", "HTTP transport is not enabled");
9191
#endif
9292
}
9393

@@ -120,10 +120,10 @@ std::unique_ptr<Backend> getInfluxDb(http::url uri)
120120
auto transport = std::make_unique<transports::Kafka>(uri.host, uri.port, uri.search);
121121
return std::make_unique<backends::InfluxDB>(std::move(transport));
122122
#else
123-
throw std::runtime_error("Kafka transport is not enabled");
123+
throw MonitoringException("Factory", "Kafka transport is not enabled");
124124
#endif
125125
}
126-
throw std::runtime_error("InfluxDB transport not supported: " + uri.protocol);
126+
throw MonitoringException("Factory", "InfluxDB transport not supported: " + uri.protocol);
127127
}
128128

129129
#ifdef O2_MONITORING_WITH_APPMON
@@ -134,7 +134,7 @@ std::unique_ptr<Backend> getApMon(http::url uri)
134134
#else
135135
std::unique_ptr<Backend> getApMon(http::url /*uri*/)
136136
{
137-
throw std::runtime_error("ApMon backend is not enabled");
137+
throw MonitoringException("Factory", "ApMon backend is not enabled");
138138
}
139139
#endif
140140

@@ -171,19 +171,22 @@ std::unique_ptr<Backend> MonitoringFactory::GetBackend(std::string& url)
171171

172172
http::url parsedUrl = http::ParseHttpUrl(url);
173173
if (parsedUrl.protocol.empty()) {
174-
throw std::runtime_error("Ill-formed URI");
174+
throw MonitoringException("Factory", "Ill-formed URI");
175175
}
176176

177177
auto iterator = map.find(parsedUrl.protocol);
178178
if (iterator == map.end()) {
179-
throw std::runtime_error("Unrecognized backend " + parsedUrl.protocol);
179+
throw MonitoringException("Factory", "Unrecognized backend " + parsedUrl.protocol);
180180
}
181-
182-
auto backend = iterator->second(parsedUrl);
183-
if (!parsedUrl.path.empty() && parsedUrl.path != "/") {
184-
SetVerbosity(parsedUrl.path.substr(parsedUrl.path.rfind("/")), backend);
181+
try {
182+
auto backend = iterator->second(parsedUrl);
183+
if (!parsedUrl.path.empty() && parsedUrl.path != "/") {
184+
SetVerbosity(parsedUrl.path.substr(parsedUrl.path.rfind("/")), backend);
185+
}
186+
return backend;
187+
} catch (...) {
188+
throw MonitoringException("Factory", "Unable to create backend " + parsedUrl.url);
185189
}
186-
return backend;
187190
}
188191

189192
std::unique_ptr<Monitoring> MonitoringFactory::Get(std::string urlsString)

src/Transports/HTTP.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ void HTTP::send(std::string&& post)
5656
response = curl_easy_perform(mCurl);
5757
curl_easy_getinfo(mCurl, CURLINFO_RESPONSE_CODE, &responseCode);
5858
if (response != CURLE_OK) {
59-
MonLogger::Get() << "HTTP Tranport " << curl_easy_strerror(response) << MonLogger::End();
59+
MonLogger::Get(Severity::Warn) << "HTTP Tranport " << curl_easy_strerror(response) << MonLogger::End();
6060
}
6161
if (responseCode < 200 || responseCode > 206) {
62-
MonLogger::Get() << "HTTP Transport: Response code : " << std::to_string(responseCode) << MonLogger::End();
62+
MonLogger::Get(Severity::Warn) << "HTTP Transport: Response code : " << std::to_string(responseCode) << MonLogger::End();
6363
}
6464
}
6565

src/Transports/TCP.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void TCP::send(std::string&& message)
5050
try {
5151
mSocket.send(boost::asio::buffer(message));
5252
} catch (const boost::system::system_error& e) {
53-
MonLogger::Get() << "TCP send: " << e.what() << MonLogger::End();
53+
MonLogger::Get(Severity::Error) << "TCP / " << e.what() << MonLogger::End();
5454
}
5555
}
5656

src/Transports/Unix.cxx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ Unix::Unix(const std::string& socketPath) : mSocket(mIoService), mEndpoint(socke
3434

3535
void Unix::send(std::string&& message)
3636
{
37-
mSocket.send_to(boost::asio::buffer(message, message.size()), mEndpoint);
37+
try {
38+
mSocket.send_to(boost::asio::buffer(message, message.size()), mEndpoint);
39+
} catch (const boost::system::system_error& e) {
40+
MonLogger::Get(Severity::Error) << "Unix socket / " << e.what() << MonLogger::End();
41+
}
3842
}
3943
#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
4044
} // namespace transports

0 commit comments

Comments
 (0)