Skip to content

Commit

Permalink
feat: comprehensive opentelemtry metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Dec 3, 2023
1 parent cda6813 commit 424c749
Show file tree
Hide file tree
Showing 50 changed files with 785 additions and 46 deletions.
2 changes: 2 additions & 0 deletions cmake/modules/BaseConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ find_package(asio CONFIG REQUIRED)
find_package(eventpp CONFIG REQUIRED)
find_package(jsoncpp CONFIG REQUIRED)
find_package(magic_enum CONFIG REQUIRED)
find_package(opentelemetry-cpp CONFIG REQUIRED)
find_package(prometheus-cpp CONFIG REQUIRED)
find_package(mio REQUIRED)
find_package(pugixml CONFIG REQUIRED)
find_package(spdlog REQUIRED)
Expand Down
8 changes: 8 additions & 0 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ 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
)

if(CMAKE_BUILD_TYPE MATCHES Debug)
Expand Down
9 changes: 9 additions & 0 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,12 @@ vipKeepHouse = false
-- NOTE set rewardChestMaxCollectItems max items per collect action
rewardChestCollectEnabled = true
rewardChestMaxCollectItems = 200

-- Metrics
--- Prometheus
metricsEnablePrometheus = false
metricsPrometheusAddress = "0.0.0.0:9464"

--- OStream
metricsEnableOstream = false
metricsOstreamInterval = 1000
42 changes: 42 additions & 0 deletions metrics/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: "3"

Check failure on line 1 in metrics/docker-compose.yml

View workflow job for this annotation

GitHub Actions / yamllint

[yamllint] metrics/docker-compose.yml#L1

[warning] missing document start "---" (document-start)
Raw output
./metrics/docker-compose.yml:1:1: [warning] missing document start "---" (document-start)

services:
prometheus:
image: prom/prometheus:latest
restart: unless-stopped
volumes:
- ./prometheus:/etc/prometheus
- prometheus-data:/prometheus
command:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--storage.tsdb.path=/prometheus"
- "--web.enable-lifecycle"
- "--log.level=debug"
ports:
- "9090:9090"
extra_hosts:
- "host.docker.internal:host-gateway"
networks:
- monitoring-net

grafana:
image: grafana/grafana:latest
restart: unless-stopped
volumes:
- grafana-data:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin # Change this!

Check failure on line 28 in metrics/docker-compose.yml

View workflow job for this annotation

GitHub Actions / yamllint

[yamllint] metrics/docker-compose.yml#L28

[warning] too few spaces before comment (comments)
Raw output
./metrics/docker-compose.yml:28:42: [warning] too few spaces before comment (comments)
- GF_USERS_ALLOW_SIGN_UP=false
depends_on:
- prometheus
ports:
- "4444:3000"
networks:
- monitoring-net

volumes:
prometheus-data:
grafana-data:

networks:
monitoring-net:
8 changes: 8 additions & 0 deletions metrics/prometheus/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global:

Check failure on line 1 in metrics/prometheus/prometheus.yml

View workflow job for this annotation

GitHub Actions / yamllint

[yamllint] metrics/prometheus/prometheus.yml#L1

[warning] missing document start "---" (document-start)
Raw output
./metrics/prometheus/prometheus.yml:1:1: [warning] missing document start "---" (document-start)
scrape_interval: 5s
scrape_timeout: 2s
evaluation_interval: 5s
scrape_configs:
- job_name: canary
static_configs:
- targets: ['host.docker.internal:9464']
3 changes: 2 additions & 1 deletion src/account/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ namespace account {
}

void Account::addPremiumDays(const int32_t &days) {
auto timeLeft = static_cast<int32_t>((m_account.premiumLastDay - getTimeNow()) % 86400);
auto timeLeft = std::max(0, static_cast<int>((m_account.premiumLastDay - getTimeNow()) % 86400));
setPremiumDays(m_account.premiumRemainingDays + days);
m_account.premiumDaysPurchased += days;

if (timeLeft > 0) {
m_account.premiumLastDay += timeLeft;
Expand Down
3 changes: 2 additions & 1 deletion src/account/account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "config/configmanager.hpp"
#include "utils/definitions.hpp"
#include "security/argon.hpp"
#include "utils/tools.hpp"

namespace account {
class Account {
Expand Down Expand Up @@ -106,7 +107,7 @@ namespace account {
void addPremiumDays(const int32_t &days);
void setPremiumDays(const int32_t &days);
[[nodiscard]] inline uint32_t getPremiumRemainingDays() const {
return m_account.premiumRemainingDays;
return m_account.premiumLastDay > getTimeNow() ? static_cast<uint32_t>((m_account.premiumLastDay - getTimeNow()) / 86400) : 0;
}

[[nodiscard]] inline uint32_t getPremiumDaysPurchased() const {
Expand Down
2 changes: 1 addition & 1 deletion src/account/account_repository_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ namespace account {

acc.id = result->getNumber<uint32_t>("id");
acc.accountType = static_cast<AccountType>(result->getNumber<int32_t>("type"));
acc.premiumRemainingDays = result->getNumber<uint32_t>("premdays");
acc.premiumLastDay = result->getNumber<time_t>("lastday");
acc.sessionExpires = result->getNumber<time_t>("expires");
acc.premiumDaysPurchased = result->getNumber<uint32_t>("premdays_purchased");
acc.creationTime = result->getNumber<uint32_t>("creation");
acc.premiumRemainingDays = acc.premiumLastDay > getTimeNow() ? (acc.premiumLastDay - getTimeNow()) / 86400 : 0;

setupLoyaltyInfo(acc);

Expand Down
11 changes: 11 additions & 0 deletions src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ int CanaryServer::run() {
loadConfigLua();

logger.info("Server protocol: {}.{}{}", CLIENT_VERSION_UPPER, CLIENT_VERSION_LOWER, g_configManager().getBoolean(OLD_PROTOCOL, __FUNCTION__) ? " and 10x allowed!" : "");
metrics::Options metricsOptions;
metricsOptions.enablePrometheusExporter = g_configManager().getBoolean(METRICS_ENABLE_PROMETHEUS, __FUNCTION__);
if (metricsOptions.enablePrometheusExporter) {
metricsOptions.prometheusOptions.url = g_configManager().getString(METRICS_PROMETHEUS_ADDRESS, __FUNCTION__);
}
metricsOptions.enableOStreamExporter = g_configManager().getBoolean(METRICS_ENABLE_OSTREAM, __FUNCTION__);
if (metricsOptions.enableOStreamExporter) {
metricsOptions.ostreamOptions.export_interval_millis = std::chrono::milliseconds(g_configManager().getNumber(METRICS_OSTREAM_INTERVAL, __FUNCTION__));
}
g_metrics().init(metricsOptions);

rsa.start();
initializeDatabase();
Expand Down Expand Up @@ -375,4 +385,5 @@ void CanaryServer::modulesLoadHelper(bool loaded, std::string moduleName) {
void CanaryServer::shutdown() {
inject<ThreadPool>().shutdown();
g_dispatcher().shutdown();
g_metrics().shutdown();
}
6 changes: 6 additions & 0 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ bool ConfigManager::load() {

loadBoolConfig(L, TOGGLE_RECEIVE_REWARD, "toggleReceiveReward", false);

loadBoolConfig(L, METRICS_ENABLE_PROMETHEUS, "metricsEnablePrometheus", false);
loadStringConfig(L, METRICS_PROMETHEUS_ADDRESS, "metricsPrometheusAddress", "localhost:9464");

loadBoolConfig(L, METRICS_ENABLE_OSTREAM, "metricsEnableOstream", false);
loadIntConfig(L, METRICS_OSTREAM_INTERVAL, "metricsOstreamInterval", 1000);

loaded = true;
lua_close(L);
return true;
Expand Down
7 changes: 6 additions & 1 deletion src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
#include "lua/creature/events.hpp"
#include "creatures/players/wheel/player_wheel.hpp"
#include "game/game.hpp"
#include "game/scheduling/dispatcher.hpp"
#include "io/iobestiary.hpp"
#include "creatures/monsters/monster.hpp"
#include "creatures/monsters/monsters.hpp"
#include "items/weapons/weapons.hpp"
#include "map/spectators.hpp"
#include "lib/metrics/metrics.hpp"

int32_t Combat::getLevelFormula(std::shared_ptr<Player> player, const std::shared_ptr<Spell> wheelSpell, const CombatDamage &damage) const {
if (!player) {
Expand Down Expand Up @@ -584,7 +586,7 @@ void Combat::CombatHealthFunc(std::shared_ptr<Creature> caster, std::shared_ptr<
}

damage.damageMultiplier += attackerPlayer->wheel()->getMajorStatConditional("Divine Empowerment", WheelMajor_t::DAMAGE);
g_logger().debug("Wheel Divine Empowerment damage multiplier {}", damage.damageMultiplier);
g_logger().trace("Wheel Divine Empowerment damage multiplier {}", damage.damageMultiplier);
}

if (g_game().combatBlockHit(damage, caster, target, params.blockedByShield, params.blockedByArmor, params.itemId != 0)) {
Expand Down Expand Up @@ -919,6 +921,7 @@ void Combat::doChainEffect(const Position &origin, const Position &dest, uint8_t
}

bool Combat::doCombatChain(std::shared_ptr<Creature> caster, std::shared_ptr<Creature> target, bool aggressive) const {
metrics::method_latency measure(__METHOD_NAME__);
if (!params.chainCallback) {
return false;
}
Expand Down Expand Up @@ -1309,6 +1312,7 @@ void Combat::setRuneSpellName(const std::string &value) {
}

std::vector<std::pair<Position, std::vector<uint32_t>>> Combat::pickChainTargets(std::shared_ptr<Creature> caster, const CombatParams &params, uint8_t chainDistance, uint8_t maxTargets, bool backtracking, bool aggressive, std::shared_ptr<Creature> initialTarget /* = nullptr */) {
metrics::method_latency measure(__METHOD_NAME__);
if (!caster) {
return {};
}
Expand Down Expand Up @@ -2016,6 +2020,7 @@ void MagicField::onStepInField(const std::shared_ptr<Creature> &creature) {
}

void Combat::applyExtensions(std::shared_ptr<Creature> caster, std::shared_ptr<Creature> target, CombatDamage &damage, const CombatParams &params) {
metrics::method_latency measure(__METHOD_NAME__);
if (damage.extension || !caster || damage.primary.type == COMBAT_HEALING) {
return;
}
Expand Down
Loading

0 comments on commit 424c749

Please sign in to comment.