Skip to content

Commit

Permalink
Refactor everything to use prometheus-cpp
Browse files Browse the repository at this point in the history
If only I had known about this earlier ;w;
  • Loading branch information
Alexandre Flion committed Nov 4, 2023
1 parent 6d3d069 commit 6831719
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 274 deletions.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ option(NO_GUI "Build without GUI" OFF)
option(STATIC_LINK "Link the binary statically" OFF)
option(GTEST "Build with Google Test" OFF)
option(ARM "Build for ARM" OFF)
option(PROMETHEUS "Build with Prometheus support" ON)

message(STATUS "Debugging network: ${DEBUG_NETWORK}")
message(STATUS "Building without GUI: ${NO_GUI}")
message(STATUS "Linking statically: ${STATIC_LINK}")
message(STATUS "Building with Google Test: ${GTEST}")
message(STATUS "Building for ARM: ${ARM}")
message(STATUS "Building with Prometheus support: ${PROMETHEUS}")

if (USE_CLANG)
find_program(CLANG_EXECUTABLE clang REQUIRED)
Expand Down Expand Up @@ -152,6 +154,20 @@ target_include_directories(spdlog SYSTEM INTERFACE ${spdlog_include})
get_target_property(yaml_cpp_include yaml-cpp INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(yaml-cpp SYSTEM INTERFACE ${yaml_cpp_include})

if(PROMETHEUS)
FetchContent_Declare(
prometheus-cpp
GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp.git
GIT_TAG v1.1.0
GIT_PROGRESS TRUE
GIT_SHALLOW TRUE
)

FetchContent_MakeAvailable(
prometheus-cpp
)
endif()

if(GTEST) # if the flag GTEST is true
FetchContent_Declare(
googletest
Expand Down Expand Up @@ -195,6 +211,7 @@ target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
PROGRAM_VERSION="${CMAKE_PROJECT_VERSION}"
MINECRAFT_VERSION="${MINECRAFT_VERSION}"
GUI_UNAVAILABLE=$<BOOL:${NO_GUI}>
PROMETHEUS_SUPPORT=$<BOOL:${PROMETHEUS}>
)

if (STATIC_LINK)
Expand Down Expand Up @@ -237,6 +254,10 @@ target_link_libraries (${CMAKE_PROJECT_NAME} PRIVATE
$<$<NOT:$<BOOL:${Boost_FOUND}>>:Boost::circular_buffer>
)

if (PROMETHEUS)
target_link_libraries (${CMAKE_PROJECT_NAME} PRIVATE prometheus-cpp::pull)
endif ()

if (CMAKE_BUILD_TYPE MATCHES RELWITHDEBINFO)
target_link_libraries (${CMAKE_PROJECT_NAME} PRIVATE
asan
Expand Down
8 changes: 7 additions & 1 deletion cubic-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ target_sources (${CMAKE_PROJECT_NAME} PRIVATE
CompressionUtils.hpp
)

if(PROMETHEUS)
target_sources (${CMAKE_PROJECT_NAME} PRIVATE
PrometheusExporter.cpp
PrometheusExporter.hpp
)
endif()

file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/plugins)

if (GTEST)
Expand Down Expand Up @@ -110,4 +117,3 @@ add_subdirectory (scoreboard)
add_subdirectory (registry)
add_subdirectory (entities)
add_subdirectory (ai)
add_subdirectory (http)
29 changes: 29 additions & 0 deletions cubic-server/PrometheusExporter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <memory>

#include <prometheus/counter.h>
#include <prometheus/registry.h>

#include "PrometheusExporter.hpp"
#include "options.hpp"

PrometheusExporter::PrometheusExporter(const std::string &bind):
_exposer(bind)
{
}

void PrometheusExporter::registerMetrics()
{
_registry = std::make_shared<prometheus::Registry>();

// Just an example of what you can put here
// To add anything else to the metrics follow the readme of prometheus-cpp:
// https://github.com/jupp0r/prometheus-cpp/blob/master/README.md
// clang-format off
UNUSED auto &packet_counter = prometheus::BuildCounter()
.Name("observed_packets_total")
.Help("Number of observed packets")
.Register(*_registry);
// clang-format on

_exposer.RegisterCollectable(_registry);
}
20 changes: 20 additions & 0 deletions cubic-server/PrometheusExporter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef D5AEBC4C_6028_4506_86F2_DA6261F54832
#define D5AEBC4C_6028_4506_86F2_DA6261F54832

#include <memory>
#include <string>

#include "prometheus/exposer.h"
#include "prometheus/registry.h"

class PrometheusExporter {
public:
PrometheusExporter(const std::string &bind);
void registerMetrics();

private:
prometheus::Exposer _exposer;
std::shared_ptr<prometheus::Registry> _registry;
};

#endif /* D5AEBC4C_6028_4506_86F2_DA6261F54832 */
42 changes: 5 additions & 37 deletions cubic-server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <mbedtls/rsa.h>

#include "PrometheusExporter.hpp"
#include "Server.hpp"
#include "World.hpp"

Expand All @@ -26,7 +27,6 @@
#include "command_parser/commands/InventoryDump.hpp"
#include "command_parser/commands/Teleport.hpp"
#include "default/DefaultWorldGroup.hpp"
#include "http/HttpServer.hpp"
#include "logging/logging.hpp"
#include "registry/Biome.hpp"
#include "registry/Chat.hpp"
Expand All @@ -38,20 +38,10 @@ using boost::asio::ip::tcp;

Server::Server():
_running(false),
// _sockfd(-1),
_config(),
_pluginManager(this),
_toSend(1024)
{
// _config.load("./config.yml");
// _config.parse("./config.yml");
// _config.parse(2, (const char * const *){"./CubicServer", "--nogui"});
// _host = _config.getIP();
// _port = _config.getPort();
// _maxPlayer = _config.getMaxPlayers();
// _motd = _config.getMotd();
// _enforceWhitelist = _config.getEnforceWhitelist();

_commands.emplace_back(std::make_unique<command_parser::Help>());
_commands.emplace_back(std::make_unique<command_parser::QuestionMark>());
_commands.emplace_back(std::make_unique<command_parser::Stop>());
Expand Down Expand Up @@ -166,8 +156,10 @@ void Server::launch(const configuration::ConfigHandler &config)

_writeThread = std::thread(&Server::_writeLoop, this);

// http stuff
_httpThread = std::thread(http::launchHttpServer);
if (CONFIG["monitoring-prometheus-enable"].as<bool>()) {
_prometheusExporter = std::make_unique<PrometheusExporter>("0.0.0.0:4242");
_prometheusExporter->registerMetrics();
}

_doAccept();

Expand Down Expand Up @@ -229,15 +221,6 @@ void Server::triggerClientCleanup(size_t clientID)
_clients.erase(clientID);
return;
}
// boost::lockfree::queue<size_t> toDelete(_clients.size());
// for (auto [id, cli] : _clients) {
// if (!cli) {
// _clients.erase(id);
// } else if (cli->isDisconnected()) {
// cli->getThread().join();
// _clients.erase(id);
// }
// }
std::erase_if(_clients, [](const auto augh) {
if (augh.second->isDisconnected()) {
if (augh.second->getThread().joinable())
Expand All @@ -252,17 +235,6 @@ void Server::addCommand(std::unique_ptr<CommandBase> command) { this->_commands.

void Server::_doAccept()
{
// while (_running) {
// boost::system::error_code ec;
// tcp::socket socket(_io_context);

// _acceptor->accept(socket, ec);
// if (!ec) {
// std::shared_ptr<Client> _cli(new Client(std::move(socket), currentClientID));
// _clients.emplace(currentClientID++, _cli);
// _cli->run();
// }
// }
tcp::socket *socket = new tcp::socket(_io_context);

_acceptor->async_accept(*socket, [socket, this](const boost::system::error_code &error) {
Expand All @@ -274,10 +246,6 @@ void Server::_doAccept()
}
delete socket;
if (this->_running) {
// for (auto [id, cli] : _clients) {
// if (!cli || cli->isDisconnected()) // Somehow they can already be freed before we get here...
// _clients.erase(id);
// }
this->_doAccept();
}
});
Expand Down
13 changes: 11 additions & 2 deletions cubic-server/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@

#include "registry/MasterRegistry.hpp"

#if PROMETHEUS_SUPPORT == 1
#include "PrometheusExporter.hpp"
#endif

constexpr char MC_VERSION[] = "1.19.3";
constexpr char MC_VERSION_BRANDING[] = "CubicServer 1.19.3";
constexpr uint16_t MC_PROTOCOL = 761;
Expand Down Expand Up @@ -155,8 +159,13 @@ class Server {

RSAEncryptionHandler _rsaKey;

// http stuff
std::thread _httpThread;
#if PROMETHEUS_SUPPORT == 1
std::unique_ptr<PrometheusExporter> _prometheusExporter;

public:
NODISCARD inline const PrometheusExporter &getPrometheusExporter() const { return *_prometheusExporter; }
NODISCARD inline PrometheusExporter &getPrometheusExporter() { return *_prometheusExporter; }
#endif

public:
NODISCARD inline bool isCompressed() const { return _config["compression"].as<bool>(); }
Expand Down
6 changes: 0 additions & 6 deletions cubic-server/http/CMakeLists.txt

This file was deleted.

98 changes: 0 additions & 98 deletions cubic-server/http/HttpConnection.cpp

This file was deleted.

Loading

0 comments on commit 6831719

Please sign in to comment.