(cobra to statsd bot) bot init was missing + capture socket error

This commit is contained in:
Benjamin Sergeant 2020-03-29 22:03:27 -07:00
parent f8bf1fe7cd
commit e465f7af52
5 changed files with 53 additions and 48 deletions

View File

@ -1,6 +1,10 @@
# Changelog
All changes to this project will be documented in this file.
## [9.1.8] - 2020-03-29
(cobra to statsd bot) bot init was missing + capture socket error
## [9.1.7] - 2020-03-29
(cobra to statsd bot) add ability to extract a numerical value and send a gauge event to statsd

View File

@ -43,11 +43,10 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
namespace ix
{
const uint64_t StatsdClient::_maxQueueSize = 32768;
StatsdClient::StatsdClient(const std::string& host,
int port,
const std::string& prefix)
@ -56,23 +55,11 @@ namespace ix
, _prefix(prefix)
, _stop(false)
{
_thread = std::thread([this] {
_thread = std::thread([this]
{
while (!_stop)
{
std::deque<std::string> stagedQueue;
{
std::lock_guard<std::mutex> lock(_mutex);
_queue.swap(stagedQueue);
}
while (!stagedQueue.empty())
{
auto message = stagedQueue.front();
_socket.sendto(message);
stagedQueue.pop_front();
}
flushQueue();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
@ -127,31 +114,39 @@ namespace ix
return send(key, ms, "ms");
}
int StatsdClient::send(std::string key, size_t value, const std::string &type)
int StatsdClient::send(std::string key, size_t value, const std::string& type)
{
cleanup(key);
char buf[256];
snprintf(buf, sizeof(buf), "%s%s:%zd|%s",
snprintf(buf, sizeof(buf), "%s%s:%zd|%s\n",
_prefix.c_str(), key.c_str(), value, type.c_str());
return send(buf);
enqueue(buf);
return 0;
}
int StatsdClient::send(const std::string &message)
void StatsdClient::enqueue(const std::string& message)
{
std::lock_guard<std::mutex> lock(_mutex);
_queue.push_back(message);
}
void StatsdClient::flushQueue()
{
std::lock_guard<std::mutex> lock(_mutex);
if (_queue.empty() ||
_queue.back().length() > _maxQueueSize)
while (!_queue.empty())
{
_queue.push_back(message);
auto message = _queue.front();
auto ret = _socket.sendto(message);
if (ret != 0)
{
std::cerr << "error: "
<< strerror(UdpSocket::getErrno())
<< std::endl;
}
_queue.pop_front();
}
else
{
(*_queue.rbegin()).append("\n").append(message);
}
return 0;
}
} // end namespace ix

View File

@ -32,11 +32,7 @@ namespace ix
int timing(const std::string& key, size_t ms);
private:
/**
* (Low Level Api) manually send a message
* which might be composed of several lines.
*/
int send(const std::string& message);
void enqueue(const std::string& message);
/* (Low Level Api) manually send a message
* type = "c", "g" or "ms"
@ -44,6 +40,7 @@ namespace ix
int send(std::string key, size_t value, const std::string& type);
void cleanup(std::string& key);
void flushQueue();
UdpSocket _socket;
@ -56,7 +53,6 @@ namespace ix
std::mutex _mutex; // for the queue
std::deque<std::string> _queue;
static const uint64_t _maxQueueSize;
};
} // end namespace ix

View File

@ -6,4 +6,4 @@
#pragma once
#define IX_WEBSOCKET_VERSION "9.1.7"
#define IX_WEBSOCKET_VERSION "9.1.8"

View File

@ -459,17 +459,27 @@ int main(int argc, char** argv)
int runtime = -1;
ix::StatsdClient statsdClient(hostname, statsdPort, prefix);
ret = ix::cobra_to_statsd_bot(cobraConfig,
channel,
filter,
position,
statsdClient,
fields,
gauge,
verbose,
maxQueueSize,
enableHeartbeat,
runtime);
std::string errMsg;
bool initialized = statsdClient.init(errMsg);
if (!initialized)
{
spdlog::error(errMsg);
ret = 0;
}
else
{
ret = ix::cobra_to_statsd_bot(cobraConfig,
channel,
filter,
position,
statsdClient,
fields,
gauge,
verbose,
maxQueueSize,
enableHeartbeat,
runtime);
}
}
else if (app.got_subcommand("cobra_to_sentry"))
{