aboutsummaryrefslogtreecommitdiff
path: root/src/common/Metric
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2017-05-11 22:29:51 +0200
committerShauren <shauren.trinity@gmail.com>2017-05-11 22:29:51 +0200
commit3a418a0bbc8e155e5395595c5e25c038d3c7c773 (patch)
tree45359566bd6e360dd133e7ef43bc2a42e26baf56 /src/common/Metric
parent8abc56c540b5d2c583e40ad3d302c43068778ed1 (diff)
Core/Common: Include cleanup
Diffstat (limited to 'src/common/Metric')
-rw-r--r--src/common/Metric/Metric.cpp129
-rw-r--r--src/common/Metric/Metric.h85
2 files changed, 139 insertions, 75 deletions
diff --git a/src/common/Metric/Metric.cpp b/src/common/Metric/Metric.cpp
index 7091dd0d376..963528c0627 100644
--- a/src/common/Metric/Metric.cpp
+++ b/src/common/Metric/Metric.cpp
@@ -1,27 +1,33 @@
/*
-* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
-*
-* This program is free software; you can redistribute it and/or modify it
-* under the terms of the GNU General Public License as published by the
-* Free Software Foundation; either version 2 of the License, or (at your
-* option) any later version.
-*
-* This program is distributed in the hope that it will be useful, but WITHOUT
-* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-* more details.
-*
-* You should have received a copy of the GNU General Public License along
-* with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
#include "Metric.h"
-#include "Log.h"
+#include "AsioHacksImpl.h"
+#include "Common.h"
#include "Config.h"
+#include "Log.h"
#include "Util.h"
+#include <boost/algorithm/string/replace.hpp>
+#include <boost/asio/deadline_timer.hpp>
+#include <boost/asio/ip/tcp.hpp>
void Metric::Initialize(std::string const& realmName, boost::asio::io_service& ioService, std::function<void()> overallStatusLogger)
{
+ _dataStream = Trinity::make_unique<boost::asio::ip::tcp::iostream>();
_realmName = FormatInfluxDBTagValue(realmName);
_batchTimer = Trinity::make_unique<boost::asio::deadline_timer>(ioService);
_overallStatusTimer = Trinity::make_unique<boost::asio::deadline_timer>(ioService);
@@ -31,8 +37,9 @@ void Metric::Initialize(std::string const& realmName, boost::asio::io_service& i
bool Metric::Connect()
{
- _dataStream.connect(_hostname, _port);
- auto error = _dataStream.error();
+ auto& stream = static_cast<boost::asio::ip::tcp::iostream&>(GetDataStream());
+ stream.connect(_hostname, _port);
+ auto error = stream.error();
if (error)
{
TC_LOG_ERROR("metric", "Error connecting to '%s:%s', disabling Metric. Error message : %s",
@@ -40,7 +47,7 @@ bool Metric::Connect()
_enabled = false;
return false;
}
- _dataStream.clear();
+ stream.clear();
return true;
}
@@ -156,22 +163,22 @@ void Metric::SendBatch()
return;
}
- if (!_dataStream.good() && !Connect())
+ if (!GetDataStream().good() && !Connect())
return;
- _dataStream << "POST " << "/write?db=" << _databaseName << " HTTP/1.1\r\n";
- _dataStream << "Host: " << _hostname << ":" << _port << "\r\n";
- _dataStream << "Accept: */*\r\n";
- _dataStream << "Content-Type: application/octet-stream\r\n";
- _dataStream << "Content-Transfer-Encoding: binary\r\n";
+ 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";
- _dataStream << "Content-Length: " << std::to_string(batchedData.tellp()) << "\r\n\r\n";
- _dataStream << batchedData.rdbuf();
+ GetDataStream() << "Content-Length: " << std::to_string(batchedData.tellp()) << "\r\n\r\n";
+ GetDataStream() << batchedData.rdbuf();
std::string http_version;
- _dataStream >> http_version;
+ GetDataStream() >> http_version;
unsigned int status_code = 0;
- _dataStream >> status_code;
+ GetDataStream() >> status_code;
if (status_code != 204)
{
TC_LOG_ERROR("metric", "Error sending data, returned HTTP code: %u", status_code);
@@ -179,14 +186,12 @@ void Metric::SendBatch()
// Read and ignore the status description
std::string status_description;
- std::getline(_dataStream, status_description);
+ std::getline(GetDataStream(), status_description);
// Read headers
std::string header;
- while (std::getline(_dataStream, header) && header != "\r")
- {
+ while (std::getline(GetDataStream(), header) && header != "\r")
if (header == "Connection: close\r")
- _dataStream.close();
- }
+ static_cast<boost::asio::ip::tcp::iostream&>(GetDataStream()).close();
ScheduleSend();
}
@@ -200,7 +205,7 @@ void Metric::ScheduleSend()
}
else
{
- _dataStream.close();
+ static_cast<boost::asio::ip::tcp::iostream&>(GetDataStream()).close();
MetricData* data;
// Clear the queue
while (_queuedData.Dequeue(data))
@@ -228,8 +233,62 @@ void Metric::ScheduleOverallStatusLog()
}
}
+std::string Metric::FormatInfluxDBValue(bool value)
+{
+ return value ? "t" : "f";
+}
+
+template<class T>
+std::string Metric::FormatInfluxDBValue(T value)
+{
+ return std::to_string(value) + 'i';
+}
+
+std::string Metric::FormatInfluxDBValue(std::string const& value)
+{
+ return '"' + boost::replace_all_copy(value, "\"", "\\\"") + '"';
+}
+
+std::string Metric::FormatInfluxDBValue(const char* value)
+{
+ return FormatInfluxDBValue(std::string(value));
+}
+
+std::string Metric::FormatInfluxDBValue(double value)
+{
+ return std::to_string(value);
+}
+
+std::string Metric::FormatInfluxDBValue(float value)
+{
+ return FormatInfluxDBValue(double(value));
+}
+
+std::string Metric::FormatInfluxDBTagValue(std::string const& value)
+{
+ // ToDo: should handle '=' and ',' characters too
+ return boost::replace_all_copy(value, " ", "\\ ");
+}
+
+Metric::Metric()
+{
+}
+
+Metric::~Metric()
+{
+}
+
Metric* Metric::instance()
{
static Metric instance;
return &instance;
}
+
+template TC_COMMON_API std::string Metric::FormatInfluxDBValue(int8);
+template TC_COMMON_API std::string Metric::FormatInfluxDBValue(uint8);
+template TC_COMMON_API std::string Metric::FormatInfluxDBValue(int16);
+template TC_COMMON_API std::string Metric::FormatInfluxDBValue(uint16);
+template TC_COMMON_API std::string Metric::FormatInfluxDBValue(int32);
+template TC_COMMON_API std::string Metric::FormatInfluxDBValue(uint32);
+template TC_COMMON_API std::string Metric::FormatInfluxDBValue(int64);
+template TC_COMMON_API std::string Metric::FormatInfluxDBValue(uint64);
diff --git a/src/common/Metric/Metric.h b/src/common/Metric/Metric.h
index 8e4d1292519..b54d1b758ef 100644
--- a/src/common/Metric/Metric.h
+++ b/src/common/Metric/Metric.h
@@ -1,28 +1,39 @@
/*
-* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
-*
-* This program is free software; you can redistribute it and/or modify it
-* under the terms of the GNU General Public License as published by the
-* Free Software Foundation; either version 2 of the License, or (at your
-* option) any later version.
-*
-* This program is distributed in the hope that it will be useful, but WITHOUT
-* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-* more details.
-*
-* You should have received a copy of the GNU General Public License along
-* with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
#ifndef METRIC_H__
#define METRIC_H__
-#include "Common.h"
-#include "Threading/MPSCQueue.h"
-#include <boost/asio/ip/tcp.hpp>
-#include <boost/algorithm/string.hpp>
-#include <type_traits>
+#include "Define.h"
+#include "AsioHacksFwd.h"
+#include "MPSCQueue.h"
+#include <chrono>
+#include <functional>
+#include <iosfwd>
+#include <memory>
+#include <string>
+
+namespace boost
+{
+ namespace asio
+ {
+ class io_service;
+ }
+}
enum MetricDataType
{
@@ -33,7 +44,7 @@ enum MetricDataType
struct MetricData
{
std::string Category;
- std::chrono::time_point<std::chrono::system_clock> Timestamp;
+ std::chrono::system_clock::time_point Timestamp;
MetricDataType Type;
// LogValue-specific fields
@@ -47,7 +58,8 @@ struct MetricData
class TC_COMMON_API Metric
{
private:
- boost::asio::ip::tcp::iostream _dataStream;
+ std::iostream& GetDataStream() { return *_dataStream; }
+ std::unique_ptr<std::iostream> _dataStream;
MPSCQueue<MetricData> _queuedData;
std::unique_ptr<boost::asio::deadline_timer> _batchTimer;
std::unique_ptr<boost::asio::deadline_timer> _overallStatusTimer;
@@ -66,31 +78,24 @@ private:
void ScheduleSend();
void ScheduleOverallStatusLog();
- template<class T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
- static std::string FormatInfluxDBValue(T value) { return std::to_string(value) + 'i'; }
-
- static std::string FormatInfluxDBValue(std::string const& value)
- {
- return '"' + boost::replace_all_copy(value, "\"", "\\\"") + '"';
- }
-
- static std::string FormatInfluxDBValue(bool value) { return value ? "t" : "f"; }
- static std::string FormatInfluxDBValue(const char* value) { return FormatInfluxDBValue(std::string(value)); }
- static std::string FormatInfluxDBValue(double value) { return std::to_string(value); }
- static std::string FormatInfluxDBValue(float value) { return FormatInfluxDBValue(double(value)); }
+ static std::string FormatInfluxDBValue(bool value);
+ template<class T>
+ static std::string FormatInfluxDBValue(T value);
+ static std::string FormatInfluxDBValue(std::string const& value);
+ static std::string FormatInfluxDBValue(const char* value);
+ static std::string FormatInfluxDBValue(double value);
+ static std::string FormatInfluxDBValue(float value);
- static std::string FormatInfluxDBTagValue(std::string const& value)
- {
- // ToDo: should handle '=' and ',' characters too
- return boost::replace_all_copy(value, " ", "\\ ");
- }
+ static std::string FormatInfluxDBTagValue(std::string const& value);
// ToDo: should format TagKey and FieldKey too in the same way as TagValue
public:
+ Metric();
+ ~Metric();
static Metric* instance();
- void Initialize(std::string const& realmName, boost::asio::io_service& ioService, std::function<void()> overallStatusLogger = [](){});
+ void Initialize(std::string const& realmName, boost::asio::io_service& ioService, std::function<void()> overallStatusLogger);
void LoadFromConfigs();
void Update();