Skip to content

Commit

Permalink
implement altimeter log setup
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Jan 18, 2024
1 parent 2e1fb3c commit 259424d
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ option(BUILD_TESTS "Build test programs" ON)
option(BUILD_DOCUMENTS "build documents" ON)
option(OGAWAYAMA "activate ogawayama brigde" ON)
option(ENABLE_JEMALLOC "use jemalloc instead of default malloc" OFF)
option(ENABLE_ALTIMETER "enable altimeter logging" OFF)

if(NOT DEFINED SHARKSFIN_IMPLEMENTATION)
set(
Expand Down Expand Up @@ -75,6 +76,16 @@ if (ENABLE_JEMALLOC)
find_package(jemalloc REQUIRED)
endif (ENABLE_JEMALLOC)

if (ENABLE_ALTIMETER)
find_package(altimeter REQUIRED)
find_package(Boost
COMPONENTS container
REQUIRED
)
find_package(fmt REQUIRED)
find_package(Threads REQUIRED)
endif()

if (BUILD_TESTS)
add_subdirectory(third_party) # should be before enable_testing()
endif()
Expand Down
22 changes: 21 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ file(GLOB SERVER_SOURCES
"tateyama/server/*.cpp"
"tateyama/configuration/*.cpp"
)
if (ENABLE_ALTIMETER)
set(SERVER_SOURCES ${SERVER_SOURCES} "tateyama/altimeter/altimeter_helper.cpp")
endif ()

add_executable(server
${SERVER_SOURCES}
Expand Down Expand Up @@ -43,7 +46,7 @@ target_link_libraries(server
if (OGAWAYAMA)
target_link_libraries(server
PRIVATE ${ogawayama_bridge}
)
)
target_compile_definitions(server PUBLIC OGAWAYAMA)
endif()

Expand All @@ -52,6 +55,16 @@ if (ENABLE_JEMALLOC)
PRIVATE jemalloc::jemalloc)
endif (ENABLE_JEMALLOC)

if (ENABLE_ALTIMETER)
target_link_libraries(server
PUBLIC altimeter
PUBLIC Boost::container
PUBLIC fmt::fmt
PUBLIC Threads::Threads
)
target_compile_definitions(server PUBLIC ALTIMETER)
endif()

set_compile_options(server)

install_libexec(server ${export_name})
Expand Down Expand Up @@ -126,6 +139,13 @@ target_link_libraries(tgctl
PRIVATE rt
)

if (ENABLE_ALTIMETER)
target_include_directories(tgctl
PRIVATE altimeter
)
target_compile_definitions(tgctl PUBLIC ALTIMETER)
endif()

set_compile_options(tgctl)

install_custom(tgctl ${export_name})
76 changes: 76 additions & 0 deletions src/tateyama/altimeter/altimeter_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2018-2024 Project Tsurugi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <filesystem>

#include <glog/logging.h>

#include <tateyama/api/configuration.h>
#include <tateyama/altimeter/events.h>
#include <altimeter/logger.h>

#include "altimeter_helper.h"

namespace tateyama::altimeter {

altimeter_helper::altimeter_helper(tateyama::api::configuration::whole* conf) {
setup(cfgs_.at(0), conf->get_section("event_log"), log_type::event_log);
setup(cfgs_.at(1), conf->get_section("audit_log"), log_type::audit_log);
}

altimeter_helper::~altimeter_helper() {
shutdown();
}

//
// The following method is created with reference to altimeter/logger/examples/altimeter/main.cpp
//
void altimeter_helper::setup(::altimeter::configuration& configuration, tateyama::api::configuration::section* section, log_type type) {
configuration.category((type == log_type::event_log) ? log_category::event : log_category::audit);
configuration.output(section->get<bool>("output").value());
configuration.directory(section->get<std::filesystem::path>("directory").value().string());
configuration.level(section->get<int>("level").value());
configuration.file_number(section->get<std::uint32_t>("file_number").value());
configuration.sync(section->get<bool>("sync").value());
configuration.buffer_size(section->get<std::size_t>("buffer_size").value());
configuration.flush_interval(section->get<std::size_t>("flush_interval").value());
configuration.flush_file_size(section->get<std::size_t>("flush_file_size").value());
configuration.max_file_size(section->get<std::size_t>("max_file_size").value());

std::string_view log_type_name = (type == log_type::event_log) ? "event log" : "audit log";
configuration.error_handler([log_type_name](std::string_view error_message) {
LOG(ERROR) << "Failed to flush or rotate " << log_type_name << " files: "
<< error_message << "\n";
});
configuration.log_write_error_handler(
[log_type_name](std::string_view error_message, std::string_view log) {
LOG(ERROR) << "Failed to " << log_type_name << " write: " << error_message
<< ", log: " << log << "\n";
});
}

void altimeter_helper::start() {
::altimeter::logger::start(cfgs_);
}

void altimeter_helper::shutdown() {
if (!shutdown_) {
::altimeter::logger::shutdown();
shutdown_ = true;
}
}

} // tateyama::altimeter
79 changes: 79 additions & 0 deletions src/tateyama/altimeter/altimeter_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2018-2023 Project Tsurugi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <vector>

#include <tateyama/api/configuration.h>
#include <altimeter/configuration.h>

namespace tateyama::altimeter {

class altimeter_helper {
enum class log_type : std::uint32_t {
/**
* @brief event log.
*/
event_log = 0,

/**
* @brief audit log.
*/
audit_log = 1
};

public:
/**
* @brief default constructer is deleted
*/
altimeter_helper() = delete;

/**
* @brief create a object reflects the configuration
*/
explicit altimeter_helper(tateyama::api::configuration::whole* conf);

/**
* @brief destruct the object
*/
~altimeter_helper();

altimeter_helper(altimeter_helper const& other) = delete;
altimeter_helper& operator=(altimeter_helper const& other) = delete;
altimeter_helper(altimeter_helper&& other) noexcept = delete;
altimeter_helper& operator=(altimeter_helper&& other) noexcept = delete;

/**
* @brief start an altimeter logger
*/
void start();

/**
* @brief shutdown the altimeter logger
*/
void shutdown();

private:
std::vector<::altimeter::configuration> cfgs_ = {
::altimeter::configuration{}, // event_log_cfg
::altimeter::configuration{} // audit_log_cfg
};
bool shutdown_{};

void setup(::altimeter::configuration& configuration, tateyama::api::configuration::section* section, log_type type);
};

} // tateyama::altimeter
25 changes: 25 additions & 0 deletions src/tateyama/configuration/bootstrap_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ static constexpr std::string_view default_configuration { // NOLINT

"[glog]\n"
"dummy=\n" // just for retain glog section in default configuration

#ifdef ALTIMETER
"[event_log]\n"
"output=true\n"
"directory=/var/log/altimeter/event\n"
"level=50\n"
"file_number=10\n"
"sync=false\n"
"buffer_size=52428800\n"
"flush_interval=10000\n"
"flush_file_size=1048576\n"
"max_file_size=1073741824\n"

"[audit_log]\n"
"output=true\n"
"directory=/var/log/altimeter/audit\n"
"level=50\n"
"file_number=10\n"
"sync=true\n"
"buffer_size=0\n"
"flush_interval=0\n"
"flush_file_size=0\n"
"max_file_size=1073741824\n"
#endif

};

} // namespace details
Expand Down
8 changes: 8 additions & 0 deletions src/tateyama/server/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
#include "server.h"
#include "utils.h"
#include "logging.h"
#ifdef ALTIMETER
#include <altimeter/logger.h>
#include "tateyama/altimeter/altimeter_helper.h"
#endif

DEFINE_string(conf, "", "the configuration file"); // NOLINT
DEFINE_string(location, "./db", "database location on file system"); // NOLINT
Expand Down Expand Up @@ -195,6 +199,10 @@ int backend_main(int argc, char **argv) {
exit(1);
}
setup_glog(conf.get());
#ifdef ALTIMETER
auto altimeter_object = std::make_unique<tateyama::altimeter::altimeter_helper>(conf.get());
altimeter_object->start();
#endif
try {
std::ostringstream oss;
boost::property_tree::json_parser::write_json(oss, conf->get_ptree());
Expand Down

0 comments on commit 259424d

Please sign in to comment.