2019-09-01 12:58:27 +00:00
|
|
|
#include <util/metrics/metrictank_publisher.hpp>
|
2019-04-10 11:53:37 +00:00
|
|
|
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-09-01 12:38:03 +00:00
|
|
|
#include <util/meta/variant.hpp>
|
2019-04-10 11:53:37 +00:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <absl/strings/str_cat.h>
|
|
|
|
#include <absl/strings/str_join.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2019-04-16 01:21:28 +00:00
|
|
|
// bzero and friends graduated from /usr/ucb*
|
|
|
|
// not too long ago
|
|
|
|
#include <strings.h>
|
2019-04-10 11:53:37 +00:00
|
|
|
#else
|
2019-04-14 17:12:11 +00:00
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
2019-04-10 11:53:37 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2019-04-14 17:12:11 +00:00
|
|
|
#endif
|
2019-04-10 11:53:37 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2019-06-20 07:26:15 +00:00
|
|
|
#include <wspiapi.h>
|
2019-04-16 01:04:08 +00:00
|
|
|
#include <lmcons.h>
|
2019-04-10 11:53:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace metrics
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
absl::optional< std::string >
|
|
|
|
makeStr(double d)
|
|
|
|
{
|
|
|
|
if(std::isnan(d) || std::isinf(d))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2019-07-06 17:03:40 +00:00
|
|
|
|
|
|
|
return std::to_string(d);
|
2019-04-10 11:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
absl::optional< std::string >
|
2019-06-11 20:46:51 +00:00
|
|
|
makeStr(int i)
|
|
|
|
{
|
|
|
|
if(i == std::numeric_limits< int >::min()
|
|
|
|
|| i == std::numeric_limits< int >::max())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::to_string(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename Value >
|
|
|
|
absl::optional< std::string >
|
|
|
|
formatValue(const Record< Value > &record, double elapsedTime,
|
2019-04-10 11:53:37 +00:00
|
|
|
Publication::Type publicationType)
|
|
|
|
{
|
|
|
|
switch(publicationType)
|
|
|
|
{
|
|
|
|
case Publication::Type::Unspecified:
|
|
|
|
{
|
|
|
|
assert(false && "Invalid publication type");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Publication::Type::Total:
|
|
|
|
{
|
|
|
|
return makeStr(record.total());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Publication::Type::Count:
|
|
|
|
{
|
|
|
|
return std::to_string(record.count());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Publication::Type::Min:
|
|
|
|
{
|
|
|
|
return makeStr(record.min());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Publication::Type::Max:
|
|
|
|
{
|
|
|
|
return makeStr(record.max());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Publication::Type::Avg:
|
|
|
|
{
|
2019-06-11 20:46:51 +00:00
|
|
|
return makeStr(static_cast< double >(record.total())
|
|
|
|
/ static_cast< double >(record.count()));
|
2019-04-10 11:53:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Publication::Type::Rate:
|
|
|
|
{
|
|
|
|
return makeStr(record.total() / elapsedTime);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Publication::Type::RateCount:
|
|
|
|
{
|
|
|
|
return makeStr(record.count() / elapsedTime);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-04-14 17:23:58 +00:00
|
|
|
assert(false && "Invalid publication type");
|
2019-05-19 22:11:07 +00:00
|
|
|
return {};
|
2019-04-10 11:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2019-06-13 21:58:17 +00:00
|
|
|
makeTagStr(const Tags &tags)
|
2019-04-10 11:53:37 +00:00
|
|
|
{
|
2019-06-13 21:58:17 +00:00
|
|
|
std::string tagStr;
|
|
|
|
|
|
|
|
auto overloaded = util::overloaded(
|
|
|
|
[](const std::string &str) { return str; },
|
|
|
|
[](double d) { return std::to_string(d); },
|
|
|
|
[](const std::int64_t i) { return std::to_string(i); });
|
|
|
|
|
|
|
|
for(const auto &tag : tags)
|
|
|
|
{
|
|
|
|
absl::StrAppend(&tagStr, ";", tag.first, "=",
|
|
|
|
absl::visit(overloaded, tag.second));
|
|
|
|
}
|
2019-06-19 22:57:46 +00:00
|
|
|
if(!tags.empty())
|
|
|
|
{
|
|
|
|
absl::StrAppend(&tagStr, ";");
|
|
|
|
}
|
2019-06-13 21:58:17 +00:00
|
|
|
return tagStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
addName(string_view id, string_view name, const Tags &tags,
|
|
|
|
string_view suffix)
|
|
|
|
{
|
|
|
|
return absl::StrCat(id, ".", name, makeTagStr(tags), suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool
|
|
|
|
isValid(int val)
|
|
|
|
{
|
|
|
|
return val != std::numeric_limits< int >::min()
|
|
|
|
&& val != std::numeric_limits< int >::max();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool
|
|
|
|
isValid(double val)
|
|
|
|
{
|
|
|
|
return Record< double >::DEFAULT_MIN() != val
|
|
|
|
&& Record< double >::DEFAULT_MAX() != val && !std::isnan(val)
|
|
|
|
&& !std::isinf(val);
|
2019-04-10 11:53:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:46:51 +00:00
|
|
|
template < typename Value >
|
2019-04-10 11:53:37 +00:00
|
|
|
std::vector< MetricTankPublisherInterface::PublishData >
|
2019-06-13 21:58:17 +00:00
|
|
|
recordToData(const TaggedRecords< Value > &taggedRecords, absl::Time time,
|
2019-06-04 19:18:10 +00:00
|
|
|
double elapsedTime, string_view suffix)
|
2019-04-10 11:53:37 +00:00
|
|
|
{
|
|
|
|
std::vector< MetricTankPublisherInterface::PublishData > result;
|
|
|
|
|
2019-06-13 21:58:17 +00:00
|
|
|
std::string id = taggedRecords.id.toString();
|
2019-04-10 11:53:37 +00:00
|
|
|
|
2019-06-13 21:58:17 +00:00
|
|
|
auto publicationType = taggedRecords.id.description()->type();
|
2019-04-10 11:53:37 +00:00
|
|
|
|
2019-06-13 21:58:17 +00:00
|
|
|
for(const auto &record : taggedRecords.data)
|
2019-04-10 11:53:37 +00:00
|
|
|
{
|
2019-06-13 21:58:17 +00:00
|
|
|
const auto &tags = record.first;
|
|
|
|
const auto &rec = record.second;
|
|
|
|
if(publicationType != Publication::Type::Unspecified)
|
2019-04-10 11:53:37 +00:00
|
|
|
{
|
2019-06-13 21:58:17 +00:00
|
|
|
auto val = formatValue(rec, elapsedTime, publicationType);
|
|
|
|
|
|
|
|
if(val)
|
|
|
|
{
|
|
|
|
result.emplace_back(
|
|
|
|
addName(id, Publication::repr(publicationType), tags, suffix),
|
|
|
|
val.value(), time);
|
|
|
|
}
|
2019-04-10 11:53:37 +00:00
|
|
|
}
|
2019-06-13 21:58:17 +00:00
|
|
|
else
|
2019-04-10 11:53:37 +00:00
|
|
|
{
|
2019-06-13 21:58:17 +00:00
|
|
|
result.emplace_back(addName(id, "count", tags, suffix),
|
|
|
|
std::to_string(rec.count()), time);
|
|
|
|
result.emplace_back(addName(id, "total", tags, suffix),
|
|
|
|
std::to_string(rec.total()), time);
|
|
|
|
|
|
|
|
if(isValid(rec.min()))
|
|
|
|
{
|
|
|
|
result.emplace_back(addName(id, "min", tags, suffix),
|
|
|
|
std::to_string(rec.min()), time);
|
|
|
|
}
|
|
|
|
if(isValid(rec.max()))
|
|
|
|
{
|
|
|
|
result.emplace_back(addName(id, "max", tags, suffix),
|
|
|
|
std::to_string(rec.max()), time);
|
|
|
|
}
|
2019-04-10 11:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
void
|
|
|
|
publishData(const std::vector< std::string > &toSend,
|
|
|
|
const std::string &host, short port)
|
|
|
|
{
|
2019-08-26 23:29:17 +00:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *addrs;
|
2019-04-10 11:53:37 +00:00
|
|
|
bzero(&hints, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
const std::string portAsStr = std::to_string(port);
|
|
|
|
|
|
|
|
if(getaddrinfo(host.c_str(), portAsStr.c_str(), &hints, &addrs) != 0)
|
|
|
|
{
|
|
|
|
LogError("Failed to get address info");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sock =
|
|
|
|
::socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol);
|
|
|
|
|
|
|
|
if(sock < 0)
|
|
|
|
{
|
|
|
|
LogError("Failed to open socket");
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(connect(sock, addrs->ai_addr, addrs->ai_addrlen) < 0)
|
|
|
|
{
|
|
|
|
LogError("Failed to connect to metrictank");
|
|
|
|
close(sock);
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
|
|
|
|
for(const std::string &val : toSend)
|
|
|
|
{
|
|
|
|
ssize_t sentLen = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sentLen =
|
|
|
|
::send(sock, val.c_str() + sentLen, val.size() - sentLen, 0);
|
|
|
|
if(sentLen == -1)
|
|
|
|
{
|
|
|
|
LogError("Error ", strerror(errno));
|
|
|
|
}
|
|
|
|
} while((0 <= sentLen)
|
|
|
|
&& (static_cast< size_t >(sentLen) < val.size()));
|
|
|
|
}
|
|
|
|
|
2019-06-04 19:18:10 +00:00
|
|
|
LogInfo("Sent ", toSend.size(), " metrics to metrictank");
|
|
|
|
|
2019-04-10 11:53:37 +00:00
|
|
|
shutdown(sock, SHUT_RDWR);
|
|
|
|
close(sock);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
publishData(const std::vector< std::string > &toSend,
|
|
|
|
const std::string &host, short port)
|
|
|
|
{
|
2019-04-14 17:12:11 +00:00
|
|
|
struct addrinfo *addrs = NULL, hints;
|
|
|
|
ZeroMemory(&hints, sizeof(hints));
|
2019-04-10 11:53:37 +00:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
|
|
|
const std::string portAsStr = std::to_string(port);
|
|
|
|
|
2019-04-14 17:12:11 +00:00
|
|
|
if(getaddrinfo(host.c_str(), portAsStr.c_str(), &hints, &addrs) != 0)
|
2019-04-10 11:53:37 +00:00
|
|
|
{
|
|
|
|
LogError("Failed to get address info");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-14 17:12:11 +00:00
|
|
|
SOCKET sock =
|
2019-04-10 11:53:37 +00:00
|
|
|
::socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol);
|
|
|
|
|
|
|
|
if(sock == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
LogError("Failed to open socket");
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(connect(sock, addrs->ai_addr, addrs->ai_addrlen) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
LogError("Failed to connect to metrictank");
|
|
|
|
closesocket(sock);
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(addrs);
|
|
|
|
|
|
|
|
for(const std::string &val : toSend)
|
|
|
|
{
|
2019-04-19 18:24:33 +00:00
|
|
|
int sentLen = 0;
|
2019-04-10 11:53:37 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sentLen =
|
|
|
|
::send(sock, val.c_str() + sentLen, val.size() - sentLen, 0);
|
|
|
|
if(sentLen == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
LogError("Error ", strerror(errno));
|
|
|
|
}
|
|
|
|
} while((0 <= sentLen)
|
|
|
|
&& (static_cast< size_t >(sentLen) < val.size()));
|
|
|
|
}
|
|
|
|
|
2019-04-14 17:12:11 +00:00
|
|
|
shutdown(sock, SD_SEND);
|
2019-04-10 11:53:37 +00:00
|
|
|
closesocket(sock);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MetricTankPublisherInterface::Tags
|
|
|
|
updateTags(MetricTankPublisherInterface::Tags tags)
|
|
|
|
{
|
|
|
|
if(tags.count("system") == 0)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) || defined(_WIN64) || defined(__NT__)
|
|
|
|
tags["system"] = "windows";
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
tags["system"] = "macos";
|
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
|
|
|
tags["system"] = "bsd";
|
|
|
|
#elif defined(__sun)
|
|
|
|
tags["system"] = "solaris";
|
|
|
|
#elif defined(__linux__)
|
|
|
|
tags["system"] = "linux";
|
|
|
|
#else
|
|
|
|
tags["system"] = "unknown";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::string
|
|
|
|
MetricTankPublisherInterface::makeSuffix(const Tags &tags)
|
|
|
|
{
|
2019-06-13 21:58:17 +00:00
|
|
|
return absl::StrJoin(updateTags(tags), ";", absl::PairFormatter("="));
|
2019-04-10 11:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-11 20:46:51 +00:00
|
|
|
MetricTankPublisherInterface::publish(const Sample &values)
|
2019-04-10 11:53:37 +00:00
|
|
|
{
|
|
|
|
if(values.recordCount() == 0)
|
|
|
|
{
|
|
|
|
// nothing to publish
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::Time sampleTime = values.sampleTime();
|
|
|
|
|
|
|
|
std::vector< PublishData > result;
|
|
|
|
result.reserve(values.recordCount());
|
|
|
|
|
|
|
|
auto gIt = values.begin();
|
|
|
|
auto prev = values.begin();
|
|
|
|
for(; gIt != values.end(); ++gIt)
|
|
|
|
{
|
2019-06-11 20:46:51 +00:00
|
|
|
const double elapsedTime = absl::ToDoubleSeconds(samplePeriod(*gIt));
|
|
|
|
|
2019-06-13 21:58:17 +00:00
|
|
|
absl::visit(
|
|
|
|
[&](const auto &d) {
|
|
|
|
for(const auto &record : d)
|
|
|
|
{
|
|
|
|
auto partial =
|
|
|
|
recordToData(record, sampleTime, elapsedTime, m_suffix);
|
|
|
|
result.insert(result.end(), partial.begin(), partial.end());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
*gIt);
|
2019-04-10 11:53:37 +00:00
|
|
|
|
|
|
|
prev = gIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
publish(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MetricTankPublisher::publish(const std::vector< PublishData > &data)
|
|
|
|
{
|
|
|
|
if(m_queue.tryPushBack(data) == thread::QueueReturn::QueueFull)
|
|
|
|
{
|
|
|
|
LogWarn("Dropping metrictank logs!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MetricTankPublisher::work()
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
auto data = m_queue.popFront(); // block until we get something
|
|
|
|
|
|
|
|
// Finish
|
|
|
|
if(absl::holds_alternative< StopStruct >(data))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(absl::holds_alternative< std::vector< PublishData > >(data));
|
|
|
|
|
|
|
|
auto vec = absl::get< std::vector< PublishData > >(data);
|
|
|
|
|
|
|
|
std::vector< std::string > toSend;
|
|
|
|
toSend.reserve(vec.size());
|
|
|
|
|
|
|
|
std::transform(vec.begin(), vec.end(), std::back_inserter(toSend),
|
|
|
|
[](const PublishData &d) -> std::string {
|
|
|
|
return absl::StrCat(
|
|
|
|
std::get< 0 >(d), " ", std::get< 1 >(d), " ",
|
|
|
|
absl::ToUnixSeconds(std::get< 2 >(d)), "\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
publishData(toSend, m_host, m_port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace metrics
|
|
|
|
} // namespace llarp
|