Skip to content

Commit

Permalink
feat: disable metrics at compile-time (opentibiabr#2509)
Browse files Browse the repository at this point in the history
To install the libraries, need to run the following commands:

For Windows systems (with static linking):
• vcpkg install opentelemetry-cpp[default-features,otlp-http,prometheus] --triplet x64-windows-static

For Windows systems (with dynamic linking):
• vcpkg install opentelemetry-cpp[default-features,otlp-http,prometheus] --triplet x64-windows

For Linux systems:
• vcpkg install opentelemetry-cpp[default-features,otlp-http,prometheus] --triplet x64-linux
  • Loading branch information
beats-dh authored Apr 1, 2024
1 parent 2f41230 commit 0c0e546
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 60 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,18 @@ include(LoggingHelper)
option(OPTIONS_ENABLE_CCACHE "Enable ccache" OFF)
option(OPTIONS_ENABLE_SCCACHE "Use sccache to speed up compilation process" OFF)
option(OPTIONS_ENABLE_IPO "Check and Enable interprocedural optimization (IPO/LTO)" ON)
option(FEATURE_METRICS "Enable metrics feature" OFF)

# *****************************************************************************
# Options Code
# *****************************************************************************

if(FEATURE_METRIC)
log_option_enabled("metrics")
else ()
log_option_disabled("metrics")
endif ()

# === CCACHE ===
if(OPTIONS_ENABLE_CCACHE)
find_program(CCACHE ccache)
Expand Down
6 changes: 4 additions & 2 deletions cmake/modules/BaseConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ find_package(absl CONFIG REQUIRED)
find_package(asio CONFIG REQUIRED)
find_package(eventpp CONFIG REQUIRED)
find_package(magic_enum CONFIG REQUIRED)
find_package(opentelemetry-cpp CONFIG REQUIRED)
find_package(prometheus-cpp CONFIG REQUIRED)
if(FEATURE_METRICS)
find_package(opentelemetry-cpp CONFIG REQUIRED)
find_package(prometheus-cpp CONFIG REQUIRED)
endif()
find_package(mio REQUIRED)
find_package(pugixml CONFIG REQUIRED)
find_package(spdlog REQUIRED)
Expand Down
24 changes: 16 additions & 8 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,25 @@ target_link_libraries(${PROJECT_NAME}_lib
unofficial::argon2::libargon2
unofficial::libmariadb
unofficial::mariadbclient
opentelemetry-cpp::common
opentelemetry-cpp::metrics
opentelemetry-cpp::api
opentelemetry-cpp::ext
opentelemetry-cpp::sdk
opentelemetry-cpp::logs
opentelemetry-cpp::ostream_metrics_exporter
opentelemetry-cpp::prometheus_exporter
protobuf
)

if(FEATURE_METRICS)
add_definitions(-DFEATURE_METRICS)

target_link_libraries(${PROJECT_NAME}_lib
PUBLIC
opentelemetry-cpp::common
opentelemetry-cpp::metrics
opentelemetry-cpp::api
opentelemetry-cpp::ext
opentelemetry-cpp::sdk
opentelemetry-cpp::logs
opentelemetry-cpp::ostream_metrics_exporter
opentelemetry-cpp::prometheus_exporter
)
endif()

if(CMAKE_BUILD_TYPE MATCHES Debug)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${ZLIB_LIBRARY_DEBUG})
else()
Expand Down
3 changes: 2 additions & 1 deletion src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ int CanaryServer::run() {
loadConfigLua();

logger.info("Server protocol: {}.{}{}", CLIENT_VERSION_UPPER, CLIENT_VERSION_LOWER, g_configManager().getBoolean(OLD_PROTOCOL, __FUNCTION__) ? " and 10x allowed!" : "");
#ifdef FEATURE_METRICS
metrics::Options metricsOptions;
metricsOptions.enablePrometheusExporter = g_configManager().getBoolean(METRICS_ENABLE_PROMETHEUS, __FUNCTION__);
if (metricsOptions.enablePrometheusExporter) {
Expand All @@ -71,7 +72,7 @@ int CanaryServer::run() {
metricsOptions.ostreamOptions.export_interval_millis = std::chrono::milliseconds(g_configManager().getNumber(METRICS_OSTREAM_INTERVAL, __FUNCTION__));
}
g_metrics().init(metricsOptions);

#endif
rsa.start();
initializeDatabase();
loadModules();
Expand Down
5 changes: 4 additions & 1 deletion src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
di/soft_singleton.cpp
logging/log_with_spd_log.cpp
metrics/metrics.cpp
thread/thread_pool.cpp
)

if(FEATURE_METRICS)
target_sources(${PROJECT_NAME}_lib PRIVATE metrics/metrics.cpp)
endif()
9 changes: 6 additions & 3 deletions src/lib/metrics/metrics.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#ifdef FEATURE_METRICS
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <[email protected]>
Expand All @@ -7,8 +8,8 @@
* Website: https://docs.opentibiabr.com/
*/

#include "metrics.hpp"
#include "lib/di/container.hpp"
#include "metrics.hpp"
#include "lib/di/container.hpp"

using namespace metrics;

Expand Down Expand Up @@ -41,7 +42,7 @@ void Metrics::init(Options opts) {
initHistograms();
}

void Metrics ::initHistograms() {
void Metrics::initHistograms() {
for (auto name : latencyNames) {
auto instrumentSelector = metrics_sdk::InstrumentSelectorFactory::Create(metrics_sdk::InstrumentType::kHistogram, name, "us");
auto meterSelector = metrics_sdk::MeterSelectorFactory::Create("performance", otelVersion, otelSchema);
Expand Down Expand Up @@ -108,3 +109,5 @@ void ScopedLatency::stop() {
auto attrskv = opentelemetry::common::KeyValueIterableView<decltype(attrs)> { attrs };
histogram->Record(elapsed, attrskv, context);
}

#endif // FEATURE_METRICS
127 changes: 90 additions & 37 deletions src/lib/metrics/metrics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,29 @@

#pragma once

#include "game/scheduling/dispatcher.hpp"
#include <opentelemetry/exporters/ostream/metric_exporter_factory.h>
#include <opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h>
#include <opentelemetry/exporters/prometheus/exporter_factory.h>
#include <opentelemetry/exporters/prometheus/exporter_options.h>
#include <opentelemetry/metrics/provider.h>
#include <opentelemetry/sdk/metrics/aggregation/default_aggregation.h>
#include <opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h>
#include <opentelemetry/sdk/metrics/push_metric_exporter.h>
#include <opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h>
#include <opentelemetry/sdk/metrics/meter.h>
#include <opentelemetry/sdk/metrics/meter_provider.h>
#include <opentelemetry/sdk/metrics/meter_provider_factory.h>
#include <opentelemetry/sdk/metrics/view/instrument_selector_factory.h>
#include <opentelemetry/sdk/metrics/view/meter_selector_factory.h>
#include <opentelemetry/sdk/metrics/view/view_factory.h>
#ifdef FEATURE_METRICS
#include "game/scheduling/dispatcher.hpp"
#include <opentelemetry/exporters/ostream/metric_exporter_factory.h>
#include <opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h>
#include <opentelemetry/exporters/prometheus/exporter_factory.h>
#include <opentelemetry/exporters/prometheus/exporter_options.h>
#include <opentelemetry/metrics/provider.h>
#include <opentelemetry/sdk/metrics/aggregation/default_aggregation.h>
#include <opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h>
#include <opentelemetry/sdk/metrics/push_metric_exporter.h>
#include <opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h>
#include <opentelemetry/sdk/metrics/meter.h>
#include <opentelemetry/sdk/metrics/meter_provider.h>
#include <opentelemetry/sdk/metrics/meter_provider_factory.h>
#include <opentelemetry/sdk/metrics/view/instrument_selector_factory.h>
#include <opentelemetry/sdk/metrics/view/meter_selector_factory.h>
#include <opentelemetry/sdk/metrics/view/view_factory.h>

namespace metrics_sdk = opentelemetry::sdk::metrics;
namespace common = opentelemetry::common;
namespace metrics_exporter = opentelemetry::exporter::metrics;
namespace metrics_api = opentelemetry::metrics;

constexpr std::string_view methodName(const char* s) {
std::string_view prettyFunction(s);
size_t bracket = prettyFunction.rfind("(");
size_t space = prettyFunction.rfind(" ", bracket) + 1;
return prettyFunction.substr(space, bracket - space);
}

#if defined(__GNUC__) || defined(__clang__)
#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__)
#elif defined(_MSC_VER)
#define __METHOD_NAME__ methodName(__FUNCSIG__)
#else
#error "Compiler not supported"
#endif

namespace metrics {
using Meter = opentelemetry::nostd::shared_ptr<metrics_api::Meter>;

Expand Down Expand Up @@ -85,12 +71,12 @@ namespace metrics {
bool stopped { false };
};

#define DEFINE_LATENCY_CLASS(class_name, histogram_name, category) \
class class_name##_latency final : public ScopedLatency { \
public: \
class_name##_latency(std::string_view name) : \
ScopedLatency(name, histogram_name "_latency", category) { } \
}
#define DEFINE_LATENCY_CLASS(class_name, histogram_name, category) \
class class_name##_latency final : public ScopedLatency { \
public: \
class_name##_latency(std::string_view name) : \
ScopedLatency(name, histogram_name "_latency", category) { } \
}

DEFINE_LATENCY_CLASS(method, "method", "method");
DEFINE_LATENCY_CLASS(lua, "lua", "scope");
Expand Down Expand Up @@ -170,3 +156,70 @@ namespace metrics {

constexpr auto g_metrics
= metrics::Metrics::getInstance;

#else // FEATURE_METRICS

#include "lib/di/container.hpp"

struct Options {
bool enablePrometheusExporter;
bool enableOStreamExporter;
};

class ScopedLatency {
public:
explicit ScopedLatency([[maybe_unused]] std::string_view name, [[maybe_unused]] const std::string &histogramName, [[maybe_unused]] const std::string &scopeKey) {};
explicit ScopedLatency([[maybe_unused]] std::string_view name, [[maybe_unused]] std::set<double> &histogram, [[maybe_unused]] const std::map<std::string, std::string> &attrs = {}, [[maybe_unused]] const std::string &context = std::string()) {};

void stop() {};

~ScopedLatency() = default;
};

namespace metrics {
#define DEFINE_LATENCY_CLASS(class_name, histogram_name, category) \
class class_name##_latency final : public ScopedLatency { \
public: \
class_name##_latency(std::string_view name) : \
ScopedLatency(name, histogram_name "_latency", category) { } \
}

DEFINE_LATENCY_CLASS(method, "method", "method");
DEFINE_LATENCY_CLASS(lua, "lua", "scope");
DEFINE_LATENCY_CLASS(query, "query", "truncated_query");
DEFINE_LATENCY_CLASS(task, "task", "task");
DEFINE_LATENCY_CLASS(lock, "lock", "scope");

const std::vector<std::string> latencyNames {
"method_latency",
"lua_latency",
"query_latency",
"task_latency",
"lock_latency",
};

class Metrics final {
public:
Metrics() = default;
~Metrics() = default;

void init([[maybe_unused]] Options opts) {};
void initHistograms() {};
void shutdown() {};

static Metrics &getInstance() {
return inject<Metrics>();
};

void addCounter([[maybe_unused]] std::string_view name, [[maybe_unused]] double value, [[maybe_unused]] const std::map<std::string, std::string> &attrs = {}) { }

void addUpDownCounter([[maybe_unused]] std::string_view name, [[maybe_unused]] int value, [[maybe_unused]] const std::map<std::string, std::string> &attrs = {}) { }

friend class ScopedLatency;
};
}

constexpr auto g_metrics
= metrics::Metrics::getInstance;

#endif // FEATURE_METRICS
15 changes: 15 additions & 0 deletions src/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,18 @@ struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<E>, char>> : formatter<
#include <eventpp/eventdispatcher.h>

#include "lua/global/shared_object.hpp"

constexpr std::string_view methodName(const char* s) {
std::string_view prettyFunction(s);
size_t bracket = prettyFunction.rfind('(');
size_t space = prettyFunction.rfind(' ', bracket) + 1;
return prettyFunction.substr(space, bracket - space);
}

#if defined(__GNUC__) || defined(__clang__)
#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__)
#elif defined(_MSC_VER)
#define __METHOD_NAME__ methodName(__FUNCSIG__)
#else
#error "Compiler not supported"
#endif
8 changes: 0 additions & 8 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
"luajit",
"magic-enum",
"mio",
{
"name": "opentelemetry-cpp",
"default-features": true,
"features": [
"otlp-http",
"prometheus"
]
},
"parallel-hashmap",
"protobuf",
"pugixml",
Expand Down

0 comments on commit 0c0e546

Please sign in to comment.