Browse Source

!2 guardian can send gettoken beidou log now

chenliangyu 6 tháng trước cách đây
mục cha
commit
126d193aeb
2 tập tin đã thay đổi với 179 bổ sung65 xóa
  1. 176 54
      Other/libpublicFun/exLog/skyeyeLog.cpp
  2. 3 11
      Other/libpublicFun/exLog/skyeyeLog.h

+ 176 - 54
Other/libpublicFun/exLog/skyeyeLog.cpp

@@ -32,6 +32,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #endif
+#include <SpBase.h>
 
 #define MAX_LOG_LEN 1900
 void skyeye_GetToken(char* channelId, char* token, char* terminalno, char* reserve1);
@@ -111,6 +112,7 @@ int GetLocalIP(char* ip_str)
 }
 
 
+
 std::string get_format_uuid()
 {
 	char strbuffer[64] = "";
@@ -162,6 +164,30 @@ public:
 	std::pair<std::string ,std::string> getToken()
 	{
 		auto execute_refreshToken = [this] {
+			std::string srcBusId = "LR0402getToken";
+			std::string srcTraceId = uuid4_generate(32);
+			std::string srcSpanId = uuid4_generate(16);
+			std::string srcParentSpanId = "0";
+			std::string srcTimestamp = std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
+
+			HttpClientTraceLink headLink;
+			memset(&headLink, 0, sizeof(headLink));
+			strncpy(headLink.X_B3_BusinessId, srcBusId.c_str(), 32);
+			strncpy(headLink.X_B3_TraceId, srcTraceId.c_str(), 32);
+			strncpy(headLink.X_B3_SpanId, srcSpanId.c_str(), 16);
+			strncpy(headLink.X_B3_ParentSpanId, srcParentSpanId.c_str(), 16);
+			strncpy(headLink.X_B3_Timestamp, srcTimestamp.c_str(), 16);
+
+			beidou_logger::getDefaultLogger(headLink, "guardian_getToken")();
+			HttpClientTraceLink nextLink;
+			memset(&nextLink, 0, sizeof(nextLink));
+			std::string nextSpanId = uuid4_generate(16);
+			strncpy(nextLink.X_B3_BusinessId, srcBusId.c_str(), 32);
+			strncpy(nextLink.X_B3_TraceId, srcTraceId.c_str(), 32);
+			strncpy(nextLink.X_B3_SpanId, nextSpanId.c_str(), 16);
+			strncpy(nextLink.X_B3_ParentSpanId, srcSpanId.c_str(), 16);
+			strncpy(nextLink.X_B3_Timestamp, srcTimestamp.c_str(), 16);
+
 			HttpClientRequestConfig config(HttpRequestMethod::POST, endpoint, NULL);
 			HttpClientResponseResult result;
 
@@ -181,7 +207,7 @@ public:
 			config.SetJsonBody(root_str);
 
 			RestfulClient client = RestfulClient::getInstance();
-			client.Do(&config, &result, NULL);
+			client.Do(&config, &result, &nextLink);
 			if (result.ResponseOK()) {
 				//解析返回字符串
 				Json::Reader reader;
@@ -307,8 +333,42 @@ class skyeye_sender {
 private:
 	std::mutex m_mtx;
 	std::list<Link_private> m_msgArr;
+	std::list<Beidou_private> m_beidouMsgArr;
 	int m_TimeSn;
 	std::chrono::system_clock::time_point m_beginTime;
+	std::string convertBeidouPrivateToStr(const Beidou_private& cur)
+	{
+		static char local_ip_str[64] = "0.0.0.0";
+		static long ipInit = 0;
+		//get ip every 1000 times
+		if (ipInit % 1000 == 0)
+			GetLocalIP(local_ip_str);
+		ipInit++;
+
+		const char* t_header = "[[tracing]]";
+		Json::Value root;
+		root["businessId"] = cur.BussID;
+		root["traceId"] = cur.TraceID;
+		root["spanId"] = cur.SpanId;
+		root["parentSpanId"] = cur.ParentSpanId;
+		root["timestamp"] = cur.t_timeStamp;
+		root["deployUnitId"] = skyeye_logger::getDefaultLogger().m_constParam.m_deployUnitId;
+		root["serviceUnitId"] = skyeye_logger::getDefaultLogger().m_constParam.m_serviceUnitId;
+		root["host"] = cur.Host;
+		root["api"] = cur.Api;
+		root["responseTime"] = cur.ResponseTime;
+		root["returnCode"] = cur.ReturnCode;
+
+		root["callStack"] = Json::arrayValue;
+		root["tags"]["TerminalNo"] = skyeye_logger::getDefaultLogger().m_constParam.m_terminalNo;
+		root["dbStack"] = Json::arrayValue; // 空数组
+
+		Json::FastWriter writer;
+		std::string dst = t_header + writer.write(root) + t_header;
+		return dst;
+	}
+
+
 	std::string convertLinkPrivateToStr(const Link_private& cur)
 	{
 		static char local_ip_str[64] = "0.0.0.0";
@@ -368,20 +428,99 @@ public:
 	}
 	void LogToSender(Link_private* msg)
 	{
-		
 		m_mtx.lock();
 		m_msgArr.push_back(*msg);
 		m_mtx.unlock();
-		
 	}
-	void InitSender(std::string t_endpoint, std::string t_topic)
+	void LogToBeidouSender(Beidou_private* msg)
+	{
+		m_mtx.lock();
+		m_beidouMsgArr.push_back(*msg);
+		m_mtx.unlock();
+	}
+
+
+	void InitSender(std::string t_endpoint, std::string t_topic, std::string t_beidou_topic)
 	{
 		
-		auto skyeye_sender = [this](std::string t_endpoint, std::string t_topic) {
-			while (true)
-			{
-				if (m_msgArr.empty())
-					std::this_thread::sleep_for(std::chrono::seconds(1));//every 1 seconds
+		auto skyeye_sender = [this](std::string t_endpoint, std::string t_topic, std::string t_beidou_topic) {
+			auto beidou_send = [&]{
+				std::vector<Beidou_private> sendArr;
+				m_mtx.lock();
+				//max send 12 logs
+				for (int i = 0; i < 12; i++)
+				{
+					if (m_beidouMsgArr.size() == 0)
+						break;
+					sendArr.emplace_back(m_beidouMsgArr.front());
+					m_beidouMsgArr.pop_front();
+				}
+				m_mtx.unlock();
+
+				if (sendArr.size() == 0)
+					return; //no need to send
+
+				//generate send str
+				Json::Value root;
+				Json::Value arraylist;//消息列表
+				Json::FastWriter fw;//写入对象
+				for (int i = 0; i < sendArr.size(); i++)
+					arraylist[i] = convertBeidouPrivateToStr(sendArr[i]);
+				root["message_list"] = arraylist;
+				root["topic"] = t_beidou_topic;
+				auto sendStr = fw.write(root);
+				//send
+				HttpClientResponseResult result;
+				HttpClientRequestConfig config(HttpRequestMethod::POST, t_endpoint, skyeye_GetToken);
+				config.SetTimeout(60);
+
+				config.SetJsonBody(sendStr.c_str());
+
+				RestfulClient client = RestfulClient::getInstance();
+				config.PreDo();
+				client.Do(&config, &result, NULL);
+				if (result.ResponseOK()) {
+					struct CommResponse
+					{
+						bool success;
+						std::string errorCode;
+						std::string returnCode;
+						std::string errorMsg;
+						std::string message;
+
+						JSONCONVERT2OBJECT_MEMEBER_REGISTER(success, errorCode, returnCode, errorMsg, message)
+							JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("success", "code", "return_code", "error_msg", "message")
+					} responseIns;
+
+					Json::Value rawRoot;
+					if (GetJsonRootObject(rawRoot, result.content) && Json2Object(responseIns, rawRoot)) {
+						if (!responseIns.success)
+						{
+							if (responseIns.errorCode.find("RTI1002") != -1)
+							{
+								m_mtx.lock();
+
+								for (auto it : sendArr)
+									m_beidouMsgArr.emplace_back(it);
+								m_mtx.unlock();
+								std::this_thread::sleep_for(std::chrono::seconds(30));//upload logs failed
+							}
+						}
+					}
+				}
+				else
+				{
+					m_mtx.lock();
+
+					for (auto it : sendArr)
+						m_beidouMsgArr.emplace_back(it);
+					m_mtx.unlock();
+				}
+			};
+
+
+
+			auto skyeye_send = [&]{
 				std::vector<Link_private> sendArr;
 				m_mtx.lock();
 				//max send 12 logs
@@ -389,23 +528,20 @@ public:
 				{
 					if (m_msgArr.size() == 0)
 						break;
-
 					sendArr.emplace_back(m_msgArr.front());
 					m_msgArr.pop_front();
 				}
 				m_mtx.unlock();
 
 				if (sendArr.size() == 0)
-					continue; //no need to send
+					return; //no need to send
 
 				//generate send str
 				Json::Value root;
 				Json::Value arraylist;//消息列表
 				Json::FastWriter fw;//写入对象
 				for (int i = 0; i < sendArr.size(); i++)
-				{
 					arraylist[i] = convertLinkPrivateToStr(sendArr[i]);
-				}
 				root["message_list"] = arraylist;
 				root["topic"] = t_topic;
 				auto sendStr = fw.write(root);
@@ -458,11 +594,25 @@ public:
 						m_msgArr.emplace_back(it);
 					m_mtx.unlock();
 				}
+			};
+
+
+			while (true)
+			{
+				if (m_msgArr.empty() && m_beidouMsgArr.empty())
+				{
+					std::this_thread::sleep_for(std::chrono::seconds(1));//every 1 seconds
+					continue;
+				}
+				if (!m_msgArr.empty())
+					skyeye_send();
+				if (!m_beidouMsgArr.empty())
+					beidou_send();
 			}
 			
 		};
 		
-		std::thread(skyeye_sender, t_endpoint, t_topic).detach();
+		std::thread(skyeye_sender, t_endpoint, t_topic, t_beidou_topic).detach();
 	}
 
 	skyeye_sender()
@@ -649,7 +799,8 @@ void CreateDefaultLogger(std::string logEndpoint, std::string logTopic,std::stri
 {
 	skyeye_logger::getDefaultLogger().m_constParam = constParam;
 	TokenManager::getInstance().InitTokenManager(30, tokenEndpoint, channelId, tokenSecret, constParam.m_terminalNo, constParam.m_version);
-	skyeye_sender::getInstance().InitSender(logEndpoint, logTopic);
+	std::string beidouTopic = SpDefine::topicBeidou;
+	skyeye_sender::getInstance().InitSender(logEndpoint, logTopic, beidouTopic);
 }
 
 void CreateDefaultLogger(std::string terminalno, std::string version, std::string entityName, std::string itemId)
@@ -669,8 +820,9 @@ void CreateDefaultLogger(std::string terminalno, std::string version, std::strin
 	//skyeye_sender::getInstance();
 	std::string endpoint = SpDefine::endpoint_env;
 	std::string topic = SpDefine::topicSys;
-	
-	skyeye_sender::getInstance().InitSender(endpoint, topic);
+	std::string beidouTopic = SpDefine::topicBeidou;
+
+	skyeye_sender::getInstance().InitSender(endpoint, topic, beidouTopic);
 
 }
 
@@ -709,20 +861,7 @@ beidou_logger& beidou_logger::setResponseTime(std::string t_ResponseTime) {
     return *this;
 }
 
-beidou_logger& beidou_logger::setCallStack(std::string t_CallStack) {
-    m_msg->CallStack = t_CallStack;
-    return *this;
-}
 
-beidou_logger& beidou_logger::setDbStack(std::string t_DbStack) {
-    m_msg->DbStack = t_DbStack;
-    return *this;
-}
-
-beidou_logger& beidou_logger::setTags(std::string t_Tags) {
-    m_msg->Tags = t_Tags;
-    return *this;
-}
 
 beidou_logger& beidou_logger::set_deployUnitId(std::string t_deployUnitId) {
     m_msg->deployUnitId = t_deployUnitId;
@@ -733,29 +872,12 @@ beidou_logger& beidou_logger::set_serviceUnitId(std::string t_serviceUnitId) {
     return *this;
 }
 
-void beidou_logger::operator () ()  const {
-    beidou_log_item beidou_log = { 0 };
-    beidou_log.BussID = (char*)m_msg->BussID.c_str();
-    beidou_log.TraceID = (char*)m_msg->TraceID.c_str();
-
-    beidou_log.ParentSpanId = (char*)m_msg->ParentSpanId.c_str();
-    beidou_log.SpanId = (char*)m_msg->SpanId.c_str();
-    beidou_log.Host = (char*)m_msg->Host.c_str();
-    beidou_log.Api = (char*)m_msg->Api.c_str();
-    beidou_log.ReturnCode = (char*)m_msg->ReturnCode.c_str();
-    beidou_log.ResponseTime = (char*)m_msg->ResponseTime.c_str();
-    beidou_log.CallStack = (char*)m_msg->CallStack.c_str();
-    beidou_log.DbStack = (char*)m_msg->DbStack.c_str();
-    beidou_log.Tags = (char*)m_msg->Tags.c_str();
-    if (m_msg->deployUnitId.length() > 0)
-        beidou_log.deployUnitId = (char*)m_msg->deployUnitId.c_str();
-    else
-        beidou_log.deployUnitId = NULL;
-
-    if (m_msg->serviceUnitId.length() > 0)
-        beidou_log.serviceUnitId = (char*)m_msg->serviceUnitId.c_str();
-    else
-        beidou_log.serviceUnitId = NULL;
-
+void beidou_logger::operator () () {
+	if(m_msg)
+	{
+		skyeye_sender::getInstance().LogToBeidouSender(m_msg);
 
+		delete m_msg;
+		m_msg = nullptr;
+	}	
 }

+ 3 - 11
Other/libpublicFun/exLog/skyeyeLog.h

@@ -27,11 +27,9 @@ public:
     std::string Api;
     std::string ReturnCode;
     std::string ResponseTime;
-    std::string CallStack;
-    std::string DbStack;
-    std::string Tags;
     std::string deployUnitId;
     std::string serviceUnitId;
+	std::string t_timeStamp;
     void* logProducer;
 
     Beidou_private(const HttpClientTraceLink& t_context, const std::string& t_Api)
@@ -41,13 +39,11 @@ public:
         TraceID = t_context.X_B3_TraceId;
         ParentSpanId = t_context.X_B3_ParentSpanId;
         SpanId = t_context.X_B3_SpanId;
+		t_timeStamp = t_context.X_B3_Timestamp;
         Api = t_Api;
         ReturnCode = "SUC0000";
         ResponseTime = "0";
         Host = "vtmtlog.paas.cmbchina.cn:8080";
-        CallStack = "{\"callStack\":[]}";
-        DbStack = "{\"dbStack\":[]}";
-        Tags = "{\"tags\":{}}";
         deployUnitId = "";
         serviceUnitId = "";
     }
@@ -167,19 +163,15 @@ public:
 	beidou_logger& setHost(std::string t_host);//请求链接地址
 	beidou_logger& setReturnCode(std::string t_returncode);
 	beidou_logger& setResponseTime(std::string t_responseTime);
-	beidou_logger& setCallStack(std::string t_callStack);//调用堆栈,格式("callStack":[{“xxx”:"xx","xxxx":sdfs}])
-	beidou_logger& setDbStack(std::string t_dbStack);//格式("dbStack":[{“xxxxx”:"xxdfsdfs","xxddddxx":sdsddsdfs}])
-	beidou_logger& setTags(std::string t_Tags);
 	beidou_logger& set_deployUnitId(std::string t_deployUnitId);
 	beidou_logger& set_serviceUnitId(std::string t_serviceUnitId);
 	beidou_logger& withLogProducer(void* t_logProducer);
-	void operator () ()  const;
+	void operator () ();
 private:
 	Beidou_private* m_msg;
 private:
 	beidou_logger() {};
 	beidou_logger(const beidou_logger&) = delete;
-	~beidou_logger();
 };
 
 //CreateDefaultLogger with all params