2019-04-12 01:03:05 +02:00
|
|
|
/*
|
|
|
|
* IXSentryClient.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXSentryClient.h"
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
2019-06-06 03:47:48 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-04-12 01:03:05 +02:00
|
|
|
|
|
|
|
#include <ixwebsocket/IXWebSocketHttpHeaders.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
SentryClient::SentryClient(const std::string& dsn) :
|
|
|
|
_dsn(dsn),
|
|
|
|
_validDsn(false),
|
|
|
|
_luaFrameRegex("\t([^/]+):([0-9]+): in function '([^/]+)'")
|
|
|
|
{
|
|
|
|
const std::regex dsnRegex("(http[s]?)://([^:]+):([^@]+)@([^/]+)/([0-9]+)");
|
|
|
|
std::smatch group;
|
|
|
|
|
2019-06-03 05:46:20 +02:00
|
|
|
if (std::regex_match(dsn, group, dsnRegex) && group.size() == 6)
|
2019-04-12 01:03:05 +02:00
|
|
|
{
|
|
|
|
_validDsn = true;
|
|
|
|
|
|
|
|
const auto scheme = group.str(1);
|
|
|
|
const auto host = group.str(4);
|
|
|
|
const auto project_id = group.str(5);
|
|
|
|
_url = scheme + "://" + host + "/api/" + project_id + "/store/";
|
|
|
|
|
|
|
|
_publicKey = group.str(2);
|
|
|
|
_secretKey = group.str(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t SentryClient::getTimestamp()
|
|
|
|
{
|
|
|
|
const auto tp = std::chrono::system_clock::now();
|
|
|
|
const auto dur = tp.time_since_epoch();
|
|
|
|
return std::chrono::duration_cast<std::chrono::seconds>(dur).count();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SentryClient::getIso8601()
|
|
|
|
{
|
|
|
|
std::time_t now;
|
|
|
|
std::time(&now);
|
2019-06-03 05:46:20 +02:00
|
|
|
char buf[sizeof("2011-10-08T07:07:09Z")];
|
|
|
|
std::strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", std::gmtime(&now));
|
2019-04-12 01:03:05 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SentryClient::computeAuthHeader()
|
|
|
|
{
|
|
|
|
std::string securityHeader("Sentry sentry_version=5");
|
|
|
|
securityHeader += ",sentry_client=ws/1.0.0";
|
|
|
|
securityHeader += ",sentry_timestamp=" + std::to_string(SentryClient::getTimestamp());
|
|
|
|
securityHeader += ",sentry_key=" + _publicKey;
|
|
|
|
securityHeader += ",sentry_secret=" + _secretKey;
|
|
|
|
|
|
|
|
return securityHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value SentryClient::parseLuaStackTrace(const std::string& stack)
|
|
|
|
{
|
|
|
|
Json::Value frames;
|
|
|
|
|
|
|
|
// Split by lines
|
|
|
|
std::string line;
|
|
|
|
std::stringstream tokenStream(stack);
|
|
|
|
|
|
|
|
std::smatch group;
|
|
|
|
|
|
|
|
while (std::getline(tokenStream, line))
|
|
|
|
{
|
|
|
|
// MapScene.lua:2169: in function 'singleCB'
|
|
|
|
if (std::regex_match(line, group, _luaFrameRegex))
|
|
|
|
{
|
|
|
|
const auto fileName = group.str(1);
|
|
|
|
const auto linenoStr = group.str(2);
|
|
|
|
const auto function = group.str(3);
|
|
|
|
|
2019-06-13 19:18:40 +02:00
|
|
|
std::stringstream ss;
|
2019-04-12 01:03:05 +02:00
|
|
|
ss << linenoStr;
|
|
|
|
uint64_t lineno;
|
|
|
|
ss >> lineno;
|
|
|
|
|
|
|
|
Json::Value frame;
|
2019-06-04 22:45:29 +02:00
|
|
|
frame["lineno"] = Json::UInt64(lineno);
|
2019-04-12 01:03:05 +02:00
|
|
|
frame["filename"] = fileName;
|
|
|
|
frame["function"] = function;
|
|
|
|
|
|
|
|
frames.append(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:18:40 +02:00
|
|
|
std::reverse(frames.begin(), frames.end());
|
|
|
|
|
2019-04-12 01:03:05 +02:00
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
2019-05-31 09:43:22 +02:00
|
|
|
std::string parseExceptionName(const std::string& stack)
|
|
|
|
{
|
|
|
|
// Split by lines
|
|
|
|
std::string line;
|
|
|
|
std::stringstream tokenStream(stack);
|
|
|
|
|
|
|
|
// Extract the first line
|
|
|
|
std::getline(tokenStream, line);
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2019-04-12 01:03:05 +02:00
|
|
|
std::string SentryClient::computePayload(const Json::Value& msg)
|
|
|
|
{
|
|
|
|
Json::Value payload;
|
2019-06-06 03:47:48 +02:00
|
|
|
|
2019-04-12 01:03:05 +02:00
|
|
|
payload["platform"] = "python";
|
|
|
|
payload["sdk"]["name"] = "ws";
|
|
|
|
payload["sdk"]["version"] = "1.0.0";
|
|
|
|
payload["timestamp"] = SentryClient::getIso8601();
|
|
|
|
|
2019-05-31 09:43:22 +02:00
|
|
|
bool isNoisyTypes = msg["id"].asString() == "game_noisytypes_id";
|
2019-04-12 01:03:05 +02:00
|
|
|
|
2019-05-31 09:43:22 +02:00
|
|
|
std::string stackTraceFieldName = isNoisyTypes ? "traceback" : "stack";
|
|
|
|
std::string stack(msg["data"][stackTraceFieldName].asString());
|
2019-04-18 05:31:34 +02:00
|
|
|
|
2019-05-31 09:43:22 +02:00
|
|
|
Json::Value exception;
|
|
|
|
exception["stacktrace"]["frames"] = parseLuaStackTrace(stack);
|
|
|
|
exception["value"] = isNoisyTypes ? parseExceptionName(stack) : msg["data"]["message"];
|
2019-04-12 01:03:05 +02:00
|
|
|
|
|
|
|
payload["exception"].append(exception);
|
|
|
|
|
|
|
|
Json::Value extra;
|
|
|
|
extra["cobra_event"] = msg;
|
2019-06-06 03:47:48 +02:00
|
|
|
extra["cobra_event"] = msg;
|
2019-04-12 01:03:05 +02:00
|
|
|
|
2019-06-06 03:47:48 +02:00
|
|
|
//
|
|
|
|
// "tags": [
|
|
|
|
// [
|
|
|
|
// "a",
|
|
|
|
// "b"
|
|
|
|
// ],
|
|
|
|
// ]
|
2019-06-06 22:48:53 +02:00
|
|
|
//
|
2019-06-06 03:47:48 +02:00
|
|
|
Json::Value tags;
|
|
|
|
|
|
|
|
Json::Value gameTag;
|
|
|
|
gameTag.append("game");
|
|
|
|
gameTag.append(msg["device"]["game"]);
|
|
|
|
tags.append(gameTag);
|
|
|
|
|
|
|
|
Json::Value userIdTag;
|
|
|
|
userIdTag.append("userid");
|
|
|
|
userIdTag.append(msg["device"]["user_id"]);
|
|
|
|
tags.append(userIdTag);
|
|
|
|
|
|
|
|
Json::Value environmentTag;
|
|
|
|
environmentTag.append("environment");
|
|
|
|
environmentTag.append(msg["device"]["environment"]);
|
|
|
|
tags.append(environmentTag);
|
|
|
|
|
|
|
|
payload["tags"] = tags;
|
2019-04-12 01:03:05 +02:00
|
|
|
|
|
|
|
return _jsonWriter.write(payload);
|
|
|
|
}
|
|
|
|
|
2019-06-06 03:47:48 +02:00
|
|
|
std::pair<HttpResponsePtr, std::string> SentryClient::send(const Json::Value& msg,
|
|
|
|
bool verbose)
|
2019-04-12 01:03:05 +02:00
|
|
|
{
|
2019-06-06 02:04:24 +02:00
|
|
|
auto args = _httpClient.createRequest();
|
|
|
|
args->extraHeaders["X-Sentry-Auth"] = SentryClient::computeAuthHeader();
|
|
|
|
args->connectTimeout = 60;
|
|
|
|
args->transferTimeout = 5 * 60;
|
|
|
|
args->followRedirects = true;
|
|
|
|
args->verbose = verbose;
|
2019-06-06 04:37:51 +02:00
|
|
|
args->logger = [](const std::string& msg)
|
2019-04-12 01:03:05 +02:00
|
|
|
{
|
2019-06-06 04:37:51 +02:00
|
|
|
spdlog::info("request logger: {}", msg);
|
2019-04-12 01:03:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string body = computePayload(msg);
|
2019-06-06 02:04:24 +02:00
|
|
|
HttpResponsePtr response = _httpClient.post(_url, body, args);
|
2019-04-12 01:03:05 +02:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
{
|
2019-06-06 02:04:24 +02:00
|
|
|
for (auto it : response->headers)
|
2019-04-12 01:03:05 +02:00
|
|
|
{
|
2019-06-06 03:47:48 +02:00
|
|
|
spdlog::info("{}: {}", it.first, it.second);
|
2019-04-12 01:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-06 03:47:48 +02:00
|
|
|
spdlog::info("Upload size: {}", response->uploadSize);
|
|
|
|
spdlog::info("Download size: {}", response->downloadSize);
|
2019-04-12 01:03:05 +02:00
|
|
|
|
2019-06-06 04:37:51 +02:00
|
|
|
spdlog::info("Status: {}", response->statusCode);
|
2019-06-06 02:04:24 +02:00
|
|
|
if (response->errorCode != HttpErrorCode::Ok)
|
2019-04-12 01:03:05 +02:00
|
|
|
{
|
2019-06-06 03:47:48 +02:00
|
|
|
spdlog::info("error message: {}", response->errorMsg);
|
2019-04-12 01:03:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-06 02:04:24 +02:00
|
|
|
if (response->headers["Content-Type"] != "application/octet-stream")
|
2019-04-12 01:03:05 +02:00
|
|
|
{
|
2019-06-06 03:47:48 +02:00
|
|
|
spdlog::info("payload: {}", response->payload);
|
2019-04-12 01:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 04:37:51 +02:00
|
|
|
return std::make_pair(response, body);
|
2019-04-12 01:03:05 +02:00
|
|
|
}
|
|
|
|
} // namespace ix
|