IXWebSocket/ws/ws_cobra_to_sentry.cpp

264 lines
9.4 KiB
C++
Raw Normal View History

/*
* ws_cobra_to_sentry.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include <atomic>
2019-09-23 19:25:23 +02:00
#include <chrono>
#include <condition_variable>
2019-09-23 19:25:23 +02:00
#include <iostream>
#include <ixcobra/IXCobraConnection.h>
2019-11-16 15:51:53 +01:00
#include <ixsentry/IXSentryClient.h>
2019-09-23 19:25:23 +02:00
#include <mutex>
#include <queue>
2019-06-06 03:47:48 +02:00
#include <spdlog/spdlog.h>
2019-09-23 19:25:23 +02:00
#include <sstream>
#include <thread>
#include <vector>
namespace ix
{
int ws_cobra_to_sentry_main(const std::string& appkey,
const std::string& endpoint,
const std::string& rolename,
const std::string& rolesecret,
const std::string& channel,
const std::string& filter,
const std::string& dsn,
bool verbose,
bool strict,
int jobs)
{
ix::CobraConnection conn;
2019-09-23 19:25:23 +02:00
conn.configure(
appkey, endpoint, rolename, rolesecret, ix::WebSocketPerMessageDeflateOptions(true));
conn.connect();
Json::FastWriter jsonWriter;
std::atomic<uint64_t> sentCount(0);
std::atomic<uint64_t> receivedCount(0);
std::atomic<bool> errorSending(false);
std::atomic<bool> stop(false);
std::atomic<bool> throttled(false);
std::condition_variable condition;
std::mutex conditionVariableMutex;
std::queue<Json::Value> queue;
auto timer = [&sentCount, &receivedCount] {
while (true)
{
spdlog::info("messages received {} sent {}", receivedCount, sentCount);
auto duration = std::chrono::seconds(1);
std::this_thread::sleep_for(duration);
}
};
std::thread t1(timer);
2019-09-23 19:25:23 +02:00
auto sentrySender = [&condition,
&conditionVariableMutex,
&queue,
verbose,
&errorSending,
&sentCount,
&stop,
&throttled,
2019-09-23 19:25:23 +02:00
&dsn] {
2019-06-06 04:37:51 +02:00
SentryClient sentryClient(dsn);
while (true)
{
Json::Value msg;
{
std::unique_lock<std::mutex> lock(conditionVariableMutex);
2019-09-23 19:25:23 +02:00
condition.wait(lock, [&queue, &stop] { return !queue.empty() && !stop; });
msg = queue.front();
queue.pop();
}
2019-06-06 03:47:48 +02:00
auto ret = sentryClient.send(msg, verbose);
HttpResponsePtr response = ret.first;
if (!response)
{
spdlog::warn("Null HTTP Response");
continue;
}
if (verbose)
{
for (auto it : response->headers)
{
spdlog::info("{}: {}", it.first, it.second);
}
spdlog::info("Upload size: {}", response->uploadSize);
spdlog::info("Download size: {}", response->downloadSize);
spdlog::info("Status: {}", response->statusCode);
if (response->errorCode != HttpErrorCode::Ok)
{
spdlog::info("error message: {}", response->errorMsg);
}
if (response->headers["Content-Type"] != "application/octet-stream")
{
spdlog::info("payload: {}", response->payload);
}
}
2019-06-06 03:47:48 +02:00
if (response->statusCode != 200)
{
2019-06-06 03:47:48 +02:00
spdlog::error("Error sending data to sentry: {}", response->statusCode);
2019-06-06 04:37:51 +02:00
spdlog::error("Body: {}", ret.second);
2019-06-06 03:47:48 +02:00
spdlog::error("Response: {}", response->payload);
errorSending = true;
// Error 429 Too Many Requests
if (response->statusCode == 429)
{
auto retryAfter = response->headers["Retry-After"];
std::stringstream ss;
ss << retryAfter;
int seconds;
ss >> seconds;
if (!ss.eof() || ss.fail())
{
seconds = 30;
spdlog::warn("Error parsing Retry-After header. "
"Using {} for the sleep duration", seconds);
}
spdlog::warn("Error 429 - Too Many Requests. ws will sleep "
"and retry after {} seconds", retryAfter);
throttled = true;
auto duration = std::chrono::seconds(seconds);
std::this_thread::sleep_for(duration);
throttled = false;
}
}
else
{
++sentCount;
}
if (stop) return;
}
};
// Create a thread pool
std::cerr << "Starting " << jobs << " sentry sender jobs" << std::endl;
std::vector<std::thread> pool;
for (int i = 0; i < jobs; i++)
{
pool.push_back(std::thread(sentrySender));
}
2019-09-23 19:25:23 +02:00
conn.setEventCallback([&conn,
&channel,
&filter,
&jsonWriter,
verbose,
&throttled,
2019-09-23 19:25:23 +02:00
&receivedCount,
&condition,
&conditionVariableMutex,
&queue](ix::CobraConnectionEventType eventType,
const std::string& errMsg,
const ix::WebSocketHttpHeaders& headers,
const std::string& subscriptionId,
CobraConnection::MsgId msgId) {
if (eventType == ix::CobraConnection_EventType_Open)
{
2019-09-23 19:25:23 +02:00
spdlog::info("Subscriber connected");
2019-09-23 19:25:23 +02:00
for (auto it : headers)
{
2019-09-23 19:25:23 +02:00
spdlog::info("{}: {}", it.first, it.second);
}
2019-09-23 19:25:23 +02:00
}
if (eventType == ix::CobraConnection_EventType_Closed)
{
spdlog::info("Subscriber closed");
}
else if (eventType == ix::CobraConnection_EventType_Authenticated)
{
std::cerr << "Subscriber authenticated" << std::endl;
conn.subscribe(channel,
filter,
[&jsonWriter,
verbose,
&throttled,
2019-09-23 19:25:23 +02:00
&receivedCount,
&condition,
&conditionVariableMutex,
&queue](const Json::Value& msg) {
if (verbose)
{
2019-09-23 19:25:23 +02:00
spdlog::info(jsonWriter.write(msg));
}
2019-09-23 19:25:23 +02:00
// If we cannot send to sentry fast enough, drop the message
if (throttled)
2019-09-23 19:25:23 +02:00
{
condition.notify_one();
return;
}
2019-09-23 19:25:23 +02:00
++receivedCount;
2019-09-23 19:25:23 +02:00
{
std::unique_lock<std::mutex> lock(conditionVariableMutex);
queue.push(msg);
}
2019-09-23 19:25:23 +02:00
condition.notify_one();
});
}
else if (eventType == ix::CobraConnection_EventType_Subscribed)
{
spdlog::info("Subscriber: subscribed to channel {}", subscriptionId);
}
else if (eventType == ix::CobraConnection_EventType_UnSubscribed)
{
spdlog::info("Subscriber: unsubscribed from channel {}", subscriptionId);
}
else if (eventType == ix::CobraConnection_EventType_Error)
{
spdlog::error("Subscriber: error {}", errMsg);
}
else if (eventType == ix::CobraConnection_EventType_Published)
{
spdlog::error("Published message hacked: {}", msgId);
}
2019-09-23 19:25:23 +02:00
});
while (true)
{
auto duration = std::chrono::seconds(1);
std::this_thread::sleep_for(duration);
if (strict && errorSending) break;
}
conn.disconnect();
2019-06-06 03:47:48 +02:00
// join all the bg threads and stop them.
stop = true;
for (int i = 0; i < jobs; i++)
{
spdlog::error("joining thread {}", i);
pool[i].join();
}
2019-06-06 04:37:51 +02:00
return (strict && errorSending) ? 1 : 0;
}
2019-09-23 19:25:23 +02:00
} // namespace ix