浏览代码

!2 add trace to downloadFile and uploadFile

chenliangyu 6 月之前
父节点
当前提交
a2d868a3db

+ 2 - 2
Framework/Common/IHttpFunc.h

@@ -92,9 +92,9 @@ public:
 	//post请求
 	virtual const bool Post(CHTTPReq& req, CHTTPRet& ret, HttpClientTraceLink* pTrace) = 0;
 	//文件块上传
-	virtual const bool UploadFileBlock(CHTTPUploadReq& req, CHTTPUploadRet& ret) = 0;
+	virtual const bool UploadFileBlock(CHTTPUploadReq& req, CHTTPUploadRet& ret, HttpClientTraceLink* pTrace) = 0;
 	//文件块下载
-	virtual const bool DownloadFileBlock(const char* url, const char* jsonReqStr, fileContentArray &content, long& httpCode, unordered_map<string, string>& responseHeaders, long timeout) = 0;
+	virtual const bool DownloadFileBlock(const char* url, const char* jsonReqStr, fileContentArray &content, long& httpCode, unordered_map<string, string>& responseHeaders, long timeout, HttpClientTraceLink* pTrace) = 0;
 };
 
 typedef void (*LogFnCallback)(const char*);

+ 48 - 2
Framework/RVCComm/HTTPClient.cpp

@@ -665,7 +665,7 @@ const bool CHTTPClient::UploadForm(const std::string& strURL,
 /************************************************************************/
 /* add by cfq 添加文件块上传                                            */
 /************************************************************************/
-const bool CHTTPClient:: UploadFileBlock(CHTTPUploadReq &req, CHTTPUploadRet &ret)
+const bool CHTTPClient:: UploadFileBlock(CHTTPUploadReq &req, CHTTPUploadRet &ret, HttpClientTraceLink* pTrace)
 {	
 	if(req.url.empty()){
 		if (m_eSettingsFlags & ENABLE_LOG)
@@ -696,6 +696,28 @@ const bool CHTTPClient:: UploadFileBlock(CHTTPUploadReq &req, CHTTPUploadRet &re
 	/** Now specify we want to POST data */
 	curl_easy_setopt(m_pCurlSession, CURLOPT_POST, 1L);
 
+   unordered_map<string, string> m_headers;
+   if (pTrace != NULL
+		&& strlen(pTrace->X_B3_BusinessId) > 0
+		&& strlen(pTrace->X_B3_TraceId) > 0
+		&& strlen(pTrace->X_B3_SpanId) > 0
+		&& strlen(pTrace->X_B3_ParentSpanId) > 0
+		&& strlen(pTrace->X_B3_Timestamp) > 0)
+	{
+		m_headers.emplace(std::make_pair("X_B3_BusinessId", pTrace->X_B3_BusinessId));
+		m_headers.emplace(std::make_pair("X_B3_TraceId", pTrace->X_B3_TraceId));
+		m_headers.emplace(std::make_pair("X_B3_SpanId", pTrace->X_B3_SpanId));
+		m_headers.emplace(std::make_pair("X_B3_ParentSpanId", pTrace->X_B3_ParentSpanId));
+		m_headers.emplace(std::make_pair("X_B3_Timestamp", pTrace->X_B3_Timestamp));
+	}
+   std::string strHeader;
+   for (HeadersMap::const_iterator it = m_headers.cbegin();
+      it != m_headers.cend();
+      ++it)
+   {
+      strHeader = it->first + ": " + it->second; // build header string
+      AddHeader(strHeader);
+   }
 	/* stating that Expect: 100-continue is not wanted */
 	AddHeader("Expect:");
 	curl_httppost* pFormPost = NULL;
@@ -804,7 +826,7 @@ const bool CHTTPClient:: UploadFileBlock(CHTTPUploadReq &req, CHTTPUploadRet &re
 /************************************************************************/
 /* add by cfq 添加文件块下载                                            */
 /************************************************************************/
-const bool CHTTPClient:: DownloadFileBlock(const char* url, const char* jsonReqStr, fileContentArray& content, long &httpCode, unordered_map<string, string>& responseHeaders, long timeout)
+const bool CHTTPClient:: DownloadFileBlock(const char* url, const char* jsonReqStr, fileContentArray& content, long &httpCode, unordered_map<string, string>& responseHeaders, long timeout, HttpClientTraceLink* pTrace)
 {
 	if (url == nullptr)
 		return false;
@@ -822,6 +844,30 @@ const bool CHTTPClient:: DownloadFileBlock(const char* url, const char* jsonReqS
 	CheckURL(url);
 
 	if(content.copyPtr != NULL){
+      // Add headers
+      unordered_map<string, string> m_headers;
+      if (pTrace != NULL
+         && strlen(pTrace->X_B3_BusinessId) > 0
+         && strlen(pTrace->X_B3_TraceId) > 0
+         && strlen(pTrace->X_B3_SpanId) > 0
+         && strlen(pTrace->X_B3_ParentSpanId) > 0
+         && strlen(pTrace->X_B3_Timestamp) > 0)
+      {
+         m_headers.emplace(std::make_pair("X_B3_BusinessId", pTrace->X_B3_BusinessId));
+         m_headers.emplace(std::make_pair("X_B3_TraceId", pTrace->X_B3_TraceId));
+         m_headers.emplace(std::make_pair("X_B3_SpanId", pTrace->X_B3_SpanId));
+         m_headers.emplace(std::make_pair("X_B3_ParentSpanId", pTrace->X_B3_ParentSpanId));
+         m_headers.emplace(std::make_pair("X_B3_Timestamp", pTrace->X_B3_Timestamp));
+      }
+      std::string strHeader;
+      for (HeadersMap::const_iterator it = m_headers.cbegin();
+         it != m_headers.cend();
+         ++it)
+      {
+         strHeader = it->first + ": " + it->second; // build header string
+         AddHeader(strHeader);
+      }
+
 		curl_easy_setopt(m_pCurlSession, CURLOPT_HTTPPOST, 1);
 		curl_easy_setopt(m_pCurlSession, CURLOPT_POSTFIELDS, jsonReqStr);
 		//接受响应头

+ 2 - 2
Framework/RVCComm/HTTPClient.h

@@ -150,9 +150,9 @@ public:
    //add by cfq  override IHttpFunc
    virtual const bool Get(CHTTPReq &req, CHTTPRet &ret, HttpClientTraceLink* pTrace);
    virtual const bool Post(CHTTPReq &req, CHTTPRet &ret, HttpClientTraceLink* pTrace);
-   virtual const bool UploadFileBlock(CHTTPUploadReq &req, CHTTPUploadRet &ret);
+   virtual const bool UploadFileBlock(CHTTPUploadReq &req, CHTTPUploadRet &ret, HttpClientTraceLink* pTrace);
    virtual const bool DownloadFileBlock(const char* url, const char* jsonReqStr, fileContentArray& content,
-	   long &httpCode, unordered_map<string, string>& responseHeaders, long timeout);
+	   long &httpCode, unordered_map<string, string>& responseHeaders, long timeout, HttpClientTraceLink* pTrace);
    
 private:
    // SSL certs

+ 4 - 1
Module/mod_UpgradeMgr/UpgradeTaskFSM.cpp

@@ -1505,7 +1505,10 @@ namespace Task
 			content.maxLen = m_fsm->m_newEachDownloadLen;
 			int retlen = 0;
 			content.curLen = &retlen;
-			if(!client->DownloadFileBlock(downloadUrl.GetData(),jsonReq.c_str(), content,httpCode,responseHeaders,time_out)){
+			PROCESS_LINK_CONTEXT("LR0402DownloadFile")
+
+
+			if(!client->DownloadFileBlock(downloadUrl.GetData(),jsonReq.c_str(), content,httpCode,responseHeaders,time_out, &nextLink)){
 				if (retlen > m_fsm->m_newEachDownloadLen) {
 					CSimpleStringA errMsg = CSimpleStringA::Format("retlen is over limit_length ,retlen= %d", retlen).GetData();
 					LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL, CSimpleStringA::Format("DownloadPackTask http req fail, %s, url=%s", errMsg.GetData(), downloadUrl.GetData()).GetData());

+ 2 - 1
Module/mod_upload/UploadFSM.cpp

@@ -345,7 +345,8 @@ namespace Task
 			if (m_fsm->m_currUploadFile->lastMD5Str.length() != 0) {
 				LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_FILE_INFO, CSimpleStringA::Format("upload %s file size is %s byte,sm3 digest is %s.", m_fsm->m_currUploadFile->fileName.c_str(), lastFileLength.c_str(), m_fsm->m_currUploadFile->lastMD5Str.c_str()).GetData());
 			}
-			if (!client->UploadFileBlock(qTempReq, qTempRet)) {
+			PROCESS_LINK_CONTEXT("LR0402UploadFile")
+			if (!client->UploadFileBlock(qTempReq, qTempRet, &nextLink)) {
 				DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("uploadFile http req fail ,url =%s ,fileName = %s",httpUrl.c_str(), m_fsm->m_currUploadFile->fileName.c_str());
 				return false;
 			}