diff options
author | Kitzunu <24550914+Kitzunu@users.noreply.github.com> | 2025-07-05 12:00:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-05 12:00:08 +0200 |
commit | a1a11a7c38005628f60cc9bb8e7900b44392127e (patch) | |
tree | b439eb8bfa55085a250960263bcd395ed6a09e19 | |
parent | 161898b7e6c1d8a0208c907e363a95b75331d20d (diff) |
BREAKINGCHANGE(Metrics): Support InfluxDB v2 (#22358)
This commit introduces support for InfluxDB v2 in the metric logging system, updates configuration options, and enhances the code to handle both InfluxDB v1 and v2. The changes include updates to the `Metric` class, configuration file, and logging behavior.
### InfluxDB v2 Support:
* Added support for InfluxDB v2 in the `Metric` class by introducing new configuration options (`Metric.InfluxDB.Org`, `Metric.InfluxDB.Bucket`, `Metric.InfluxDB.Token`) and logic to handle v2-specific parameters.
* Updated the `SendBatch` method to construct HTTP requests differently based on whether v1 or v2 is enabled.
### Configuration Updates:
* Replaced the `Metric.ConnectionInfo` configuration with `Metric.InfluxDB.Connection` and added detailed comments and examples for both InfluxDB v1 and v2 configurations in `worldserver.conf.dist`.
* Added placeholder configuration entries for InfluxDB v2 parameters (`Metric.InfluxDB.Org`, `Metric.InfluxDB.Bucket`, `Metric.InfluxDB.Token`) in `worldserver.conf.dist`.
### Logging Enhancements:
* Added a commented-out logger configuration (`Logger.metric`) in `worldserver.conf.dist` for potential use in metric-specific logging.
-rw-r--r-- | src/common/Metric/Metric.cpp | 49 | ||||
-rw-r--r-- | src/common/Metric/Metric.h | 4 | ||||
-rw-r--r-- | src/server/apps/worldserver/worldserver.conf.dist | 41 |
3 files changed, 77 insertions, 17 deletions
diff --git a/src/common/Metric/Metric.cpp b/src/common/Metric/Metric.cpp index 7c4771dcad..c35fa30b6c 100644 --- a/src/common/Metric/Metric.cpp +++ b/src/common/Metric/Metric.cpp @@ -99,25 +99,48 @@ void Metric::LoadFromConfigs() // Cancel any scheduled operation if the config changed from Enabled to Disabled. if (_enabled && !previousValue) { - std::string connectionInfo = sConfigMgr->GetOption<std::string>("Metric.ConnectionInfo", ""); + std::string connectionInfo = sConfigMgr->GetOption<std::string>("Metric.InfluxDB.Connection", ""); if (connectionInfo.empty()) { - LOG_ERROR("metric", "'Metric.ConnectionInfo' not specified in configuration file."); + LOG_ERROR("metric", "Metric.InfluxDB.Connection not specified in configuration file."); return; } std::vector<std::string_view> tokens = Acore::Tokenize(connectionInfo, ';', true); - if (tokens.size() != 3) + if (tokens.size() != 2) { - LOG_ERROR("metric", "'Metric.ConnectionInfo' specified with wrong format in configuration file."); + LOG_ERROR("metric", "Metric.InfluxDB.Connection specified with wrong format in configuration file."); return; } _hostname.assign(tokens[0]); _port.assign(tokens[1]); - _databaseName.assign(tokens[2]); - Connect(); + _useV2 = sConfigMgr->GetOption<bool>("Metric.InfluxDB.v2", false); + if (_useV2) + { + _org = sConfigMgr->GetOption<std::string>("Metric.InfluxDB.Org", ""); + _bucket = sConfigMgr->GetOption<std::string>("Metric.InfluxDB.Bucket", ""); + _token = sConfigMgr->GetOption<std::string>("Metric.InfluxDB.Token", ""); + + if (_org.empty() || _bucket.empty() || _token.empty()) + { + LOG_ERROR("metric", "InfluxDB v2 parameters missing: org, bucket, or token."); + return; + } + } + else + { + if (tokens.size() != 3) + { + LOG_ERROR("metric", "Metric.InfluxDB.Connection specified with wrong format in configuration file."); + return; + } + + _databaseName.assign(tokens[2]); + } + + Connect(); ScheduleSend(); ScheduleOverallStatusLog(); } @@ -206,8 +229,18 @@ void Metric::SendBatch() if (!GetDataStream().good() && !Connect()) return; - GetDataStream() << "POST " << "/write?db=" << _databaseName << " HTTP/1.1\r\n"; - GetDataStream() << "Host: " << _hostname << ":" << _port << "\r\n"; + if (_useV2) + { + GetDataStream() << "POST " << "/api/v2/write?bucket=" << _bucket + << "&org=" << _org << "&precision=ns HTTP/1.1\r\n"; + GetDataStream() << "Host: " << _hostname << ":" << _port << "\r\n"; + GetDataStream() << "Authorization: Token " << _token << "\r\n"; + } + else + { + GetDataStream() << "POST " << "/write?db=" << _databaseName << " HTTP/1.1\r\n"; + GetDataStream() << "Host: " << _hostname << ":" << _port << "\r\n"; + } GetDataStream() << "Accept: */*\r\n"; GetDataStream() << "Content-Type: application/octet-stream\r\n"; GetDataStream() << "Content-Transfer-Encoding: binary\r\n"; diff --git a/src/common/Metric/Metric.h b/src/common/Metric/Metric.h index 729d2f7fc5..7ebc3cabb0 100644 --- a/src/common/Metric/Metric.h +++ b/src/common/Metric/Metric.h @@ -71,6 +71,10 @@ private: std::string _hostname; std::string _port; std::string _databaseName; + bool _useV2 = false; + std::string _org; + std::string _bucket; + std::string _token; std::function<void()> _overallStatusLogger; std::string _realmName; std::unordered_map<std::string, int64> _thresholds; diff --git a/src/server/apps/worldserver/worldserver.conf.dist b/src/server/apps/worldserver/worldserver.conf.dist index 01dc00bb03..d2b5af453b 100644 --- a/src/server/apps/worldserver/worldserver.conf.dist +++ b/src/server/apps/worldserver/worldserver.conf.dist @@ -678,6 +678,7 @@ Appender.Errors=2,5,0,Errors.log,w # Logger.root=2,Console Server +#Logger.metric=2,Console Server #Logger.commands.gm=4,Console GM Logger.diff=3,Console Server Logger.mmaps=4,Server @@ -812,6 +813,37 @@ Log.Async.Enable = 0 Metric.Enable = 0 # +# Metric.InfluxDB +# Description: Connection settings for InfluxDB. +# +# For InfluxDB v1: +# Only fill in Metric.InfluxDB.Connection. +# +# Example: +# Metric.InfluxDB.Connection = "hostname;port;database" +# +# For InfluxDB v2: +# Fill in every field. +# +# NOTE: Currently, Grafana will not work with the provided json files to visualize +# data from InfluxDB v2. +# +# Example: +# Metric.InfluxDB.Connection = "hostname;port" +# Metric.InfluxDB.v2 = 0 - (Disabled) +# 1 - (Enabled) +# Metric.InfluxDB.Org = "my-org" +# Metric.InfluxDB.Bucket = "my-bucket" +# Metric.InfluxDB.Token = "my-token" +# + +Metric.InfluxDB.Connection = "127.0.0.1;8086;worldserver" +Metric.InfluxDB.v2 = 0 +Metric.InfluxDB.Org = "" +Metric.InfluxDB.Bucket = "" +Metric.InfluxDB.Token = "" + +# # Metric.Interval # Description: Interval between every batch of data sent in seconds. # Longer interval means larger batch of data. If the batch @@ -822,15 +854,6 @@ Metric.Enable = 0 Metric.Interval = 1 # -# Metric.ConnectionInfo -# Description: Connection settings for metric database (currently InfluxDB). -# Example: "hostname;port;database" -# Default: "127.0.0.1;8086;worldserver" -# - -Metric.ConnectionInfo = "127.0.0.1;8086;worldserver" - -# # Metric.OverallStatusInterval # Description: Interval between every gathering of overall worldserver status data in seconds # Default: 1 second |