c++上报URL
·
代码示例如下:
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <json/json.h>
void CallingChainTracer::report(reportStruct& reportData)
{
CURL *curl;
CURLcode res;
struct curl_slist *headers = NULL;
// 构建上报的url
const std::string url = "http://192.168.12.34:30660/call_chain/buried-points/trace/push";
// 填充JSON结构体
Json::Value root;
root["trace_id"] = (std::string)span_->trace_id;
root["workflow_id"] = (int)reportData.workflow_id;
root["case_id"] = (int)reportData.case_id;
root["source_span_id"] = (std::string)span_->source_span_id;
root["device_name"] = (std::string)reportData.device_name;
root["span_id"] = (std::string)span_->span_id;
root["service_id"] = (std::string)span_->service_id;
root["service_name"] = (std::string)reportData.service_name;
root["method_id"] = (std::string)span_->method_id;
root["method_name"] = (std::string)reportData.method_name;
root["method_type"] = (std::string)reportData.method_type;
root["current_time"] = Json::Int64(span_->current_time);
root["status"] = (int)span_->status;
root["result"] = 0;
Json::StreamWriterBuilder builder;
builder["indentation"] = ""; // 格式化输出
std::string json_str = Json::writeString(builder, root);
curl = curl_easy_init();
if (curl) {
headers = curl_slist_append(headers, "accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_str.c_str());
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
if (res != CURLE_OK)
std::cout << "Error: " << curl_easy_strerror(res) << std::endl;
else
std::cout << "Request sent successfully" << std::endl;
}
}
更多推荐
所有评论(0)