Skip to content

Commit

Permalink
add reason code functionality to monitor.h and use it in altimeter.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Oct 31, 2024
1 parent 9f743a4 commit 3c219a2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 38 deletions.
55 changes: 27 additions & 28 deletions src/tateyama/altimeter/altimeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,23 @@ DECLARE_string(monitor);
namespace tateyama::altimeter {

template <class T>
static tgctl::return_code post_processing(std::optional<T>& response_opt, const std::string_view sub_command) {
static monitor::reason post_processing(std::optional<T>& response_opt, const std::string_view sub_command) {
if (response_opt) {
auto response = response_opt.value();

switch(response.result_case()) {
case T::ResultCase::kSuccess:
return tgctl::return_code::ok;
return monitor::reason::absent;
case T::ResultCase::kError:
std::cerr << "altimeter " << sub_command << " error: " << response.error().message() << std::endl;
break;
return monitor::reason::server;
default:
std::cerr << "altimeter " << sub_command << " returns illegal result_case" << std::endl;
break;
std::cerr << "altimeter " << sub_command << " returns illegal response" << std::endl;
return monitor::reason::unknown;
}
} else {
std::cerr << "altimeter " << sub_command << " returns nullopt" << std::endl;
}
return tgctl::return_code::err;
std::cerr << "altimeter " << sub_command << " returns nullopt" << std::endl;
return monitor::reason::server;
}

tgctl::return_code set_enabled(const std::string& type, bool enabled) {
Expand All @@ -61,7 +60,7 @@ tgctl::return_code set_enabled(const std::string& type, bool enabled) {
monitor_output->start();
}

auto rtnv = tgctl::return_code::ok;
auto reason = monitor::reason::absent;
authentication::auth_options();
try {
auto transport = std::make_unique<tateyama::bootstrap::wire::transport>(tateyama::framework::service_id_altimeter);
Expand All @@ -77,16 +76,16 @@ tgctl::return_code set_enabled(const std::string& type, bool enabled) {
auto response_opt = transport->send<::tateyama::proto::altimeter::response::Configure>(request);
request.clear_configure();

rtnv = post_processing<::tateyama::proto::altimeter::response::Configure>(response_opt, "set_enabled");
reason = post_processing<::tateyama::proto::altimeter::response::Configure>(response_opt, "set_enabled");
} catch (std::runtime_error &ex) {
std::cerr << "could not connect to database with name '" << tateyama::bootstrap::wire::transport::database_name() << "'" << std::endl;
rtnv = tgctl::return_code::err;
reason = monitor::reason::connection;
}

if (monitor_output) {
monitor_output->finish(rtnv == tgctl::return_code::ok);
monitor_output->finish(reason);
}
return rtnv;
return (reason == monitor::reason::absent) ? tgctl::return_code::err : tgctl::return_code::err; // NOLINT(misc-redundant-expression)
}

tgctl::return_code set_log_level(const std::string& type, const std::string& level) {
Expand All @@ -97,7 +96,7 @@ tgctl::return_code set_log_level(const std::string& type, const std::string& lev
monitor_output->start();
}

auto rtnv = tgctl::return_code::ok;
auto reason = monitor::reason::absent;
authentication::auth_options();
try {
auto transport = std::make_unique<tateyama::bootstrap::wire::transport>(tateyama::framework::service_id_altimeter);
Expand All @@ -114,16 +113,16 @@ tgctl::return_code set_log_level(const std::string& type, const std::string& lev
auto response_opt = transport->send<::tateyama::proto::altimeter::response::Configure>(request);
request.clear_configure();

rtnv = post_processing<::tateyama::proto::altimeter::response::Configure>(response_opt, "set_log_level");
reason = post_processing<::tateyama::proto::altimeter::response::Configure>(response_opt, "set_log_level");
} catch (std::runtime_error &ex) {
std::cerr << "could not connect to database with name '" << tateyama::bootstrap::wire::transport::database_name() << "'" << std::endl;
rtnv = tgctl::return_code::err;
reason = monitor::reason::connection;
}

if (monitor_output) {
monitor_output->finish(rtnv == tgctl::return_code::ok);
monitor_output->finish(reason);
}
return rtnv;
return (reason == monitor::reason::absent) ? tgctl::return_code::err : tgctl::return_code::err; // NOLINT(misc-redundant-expression)
}

tgctl::return_code set_statement_duration(const std::string& value) {
Expand All @@ -134,7 +133,7 @@ tgctl::return_code set_statement_duration(const std::string& value) {
monitor_output->start();
}

auto rtnv = tgctl::return_code::ok;
auto reason = monitor::reason::absent;
authentication::auth_options();
try {
auto transport = std::make_unique<tateyama::bootstrap::wire::transport>(tateyama::framework::service_id_altimeter);
Expand All @@ -145,16 +144,16 @@ tgctl::return_code set_statement_duration(const std::string& value) {
auto response_opt = transport->send<::tateyama::proto::altimeter::response::Configure>(request);
request.clear_configure();

rtnv = post_processing<::tateyama::proto::altimeter::response::Configure>(response_opt, "set_statement_duration");
reason = post_processing<::tateyama::proto::altimeter::response::Configure>(response_opt, "set_statement_duration");
} catch (std::runtime_error &ex) {
std::cerr << "could not connect to database with name '" << tateyama::bootstrap::wire::transport::database_name() << "'" << std::endl;
rtnv = tgctl::return_code::err;
reason = monitor::reason::connection;
}

if (monitor_output) {
monitor_output->finish(rtnv == tgctl::return_code::ok);
monitor_output->finish(reason);
}
return rtnv;
return (reason == monitor::reason::absent) ? tgctl::return_code::err : tgctl::return_code::err; // NOLINT(misc-redundant-expression)
}

tgctl::return_code rotate(const std::string& type) {
Expand All @@ -165,7 +164,7 @@ tgctl::return_code rotate(const std::string& type) {
monitor_output->start();
}

auto rtnv = tgctl::return_code::ok;
auto reason = monitor::reason::absent;
authentication::auth_options();
try {
auto transport = std::make_unique<tateyama::bootstrap::wire::transport>(tateyama::framework::service_id_altimeter);
Expand All @@ -183,16 +182,16 @@ tgctl::return_code rotate(const std::string& type) {
auto response_opt = transport->send<::tateyama::proto::altimeter::response::LogRotate>(request);
request.clear_log_rotate();

rtnv = post_processing<::tateyama::proto::altimeter::response::LogRotate>(response_opt, "rotete");
reason = post_processing<::tateyama::proto::altimeter::response::LogRotate>(response_opt, "rotete");
} catch (std::runtime_error &ex) {
std::cerr << "could not connect to database with name '" << tateyama::bootstrap::wire::transport::database_name() << "'" << std::endl;
rtnv = tgctl::return_code::err;
reason = monitor::reason::connection;
}

if (monitor_output) {
monitor_output->finish(rtnv == tgctl::return_code::ok);
monitor_output->finish(reason);
}
return rtnv;
return (reason == monitor::reason::absent) ? tgctl::return_code::err : tgctl::return_code::err; // NOLINT(misc-redundant-expression)
}

} // tateyama::bootstrap::backup
50 changes: 48 additions & 2 deletions src/tateyama/monitor/monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ enum class status : std::int64_t {
unknown = -1,
};

enum class reason : std::int64_t {
absent = 0,
connection = 1,
server = 2,
not_found = 3,
ambiguous = 4,
permission = 5,
variable_not_defined = 6,
variable_invalid_value = 7,

unknown = -1,
};

/**
* @brief returns string representation of the value.
* @param value the target value
Expand All @@ -52,6 +65,27 @@ enum class status : std::int64_t {
return "illegal state"sv;
}

/**
* @brief returns string representation of the value.
* @param value the target value
* @return the corresponded string representation
*/
[[nodiscard]] constexpr inline std::string_view to_string_view(reason value) noexcept {
using namespace std::string_view_literals;
switch (value) {
case reason::absent:return "absent"sv;
case reason::connection:return "connection"sv;
case reason::server:return "server"sv;
case reason::not_found:return "not_found"sv;
case reason::ambiguous:return "ambiguous"sv;
case reason::permission:return "permission"sv;
case reason::variable_not_defined:return "variable_not_defined"sv;
case reason::variable_invalid_value:return "variable_invalid_value"sv;
case reason::unknown: return "unknown"sv;
}
return "illegal reason"sv;
}

class monitor {
constexpr static std::string_view TIME_STAMP = R"("timestamp": )";
constexpr static std::string_view KIND_START = R"("kind": "start")";
Expand All @@ -62,6 +96,7 @@ class monitor {
// status
constexpr static std::string_view FORMAT_STATUS = R"("format": "status")";
constexpr static std::string_view STATUS = R"("status": ")";
constexpr static std::string_view REASON = R"("reason": ")";
// session info
constexpr static std::string_view FORMAT_SESSION_INFO = R"("format": "session-info")";
constexpr static std::string_view SESSION_ID = R"("session_id": ":)";
Expand Down Expand Up @@ -97,10 +132,21 @@ class monitor {
void finish(bool status) {
if (status) {
strm_ << "{ " << TIME_STAMP << time(nullptr) << ", "
<< KIND_FINISH << ", " << STATUS << "success\" }" << std::endl;
<< KIND_FINISH << ", " << STATUS << R"(success" })" << std::endl;
} else {
strm_ << "{ " << TIME_STAMP << time(nullptr) << ", "
<< KIND_FINISH << ", " << STATUS << R"(failure" })" << std::endl;
}
strm_.flush();
}
void finish(reason rc) {
if (rc == reason::absent) {
strm_ << "{ " << TIME_STAMP << time(nullptr) << ", "
<< KIND_FINISH << ", " << STATUS << R"(success" })" << std::endl;
} else {
strm_ << "{ " << TIME_STAMP << time(nullptr) << ", "
<< KIND_FINISH << ", " << STATUS << "failure\" }" << std::endl;
<< KIND_FINISH << ", " << STATUS << R"(failure", )"
<< REASON << to_string_view(rc) << R"(" })" << std::endl;
}
strm_.flush();
}
Expand Down
9 changes: 1 addition & 8 deletions src/tateyama/proto/altimeter/request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ message LogSettings {

// the reporting threshold of statement duration, or keep the current settings.
oneof statement_duration_opt {

// the reporting threshold of statement duration, by nanoseconds.
uint64 statement_duration = 3;
}
Expand All @@ -37,13 +37,6 @@ message LogSettings {
message Configure {

// configure event log, or keep the current settings.
//// oneof log_opt {
//// // configuration for event log.
//// LogSettings event_log = 1;
////
//// // configuration for audit log.
//// LogSettings audit_log = 2;
//// }
oneof event_log_opt {

// configuration for event log.
Expand Down
4 changes: 4 additions & 0 deletions src/tateyama/transport/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ constexpr static std::size_t SESSION_MESSAGE_VERSION_MAJOR = 0;
constexpr static std::size_t SESSION_MESSAGE_VERSION_MINOR = 0;
constexpr static std::size_t METRICS_MESSAGE_VERSION_MAJOR = 0;
constexpr static std::size_t METRICS_MESSAGE_VERSION_MINOR = 0;
#ifdef ENABLE_ALTIMETER
constexpr static std::size_t ALTIMETER_MESSAGE_VERSION_MAJOR = 0;
constexpr static std::size_t ALTIMETER_MESSAGE_VERSION_MINOR = 0;
#endif
constexpr static std::int64_t EXPIRATION_SECONDS = 60;

class transport {
Expand Down Expand Up @@ -289,6 +291,7 @@ class transport {
return response;
}

#ifdef ENABLE_ALTIMETER
// altimeter
template <typename T>
std::optional<T> send(::tateyama::proto::altimeter::request::Request& request) {
Expand Down Expand Up @@ -321,6 +324,7 @@ class transport {
}
return response;
}
#endif

void close() {
wire_.close();
Expand Down

0 comments on commit 3c219a2

Please sign in to comment.