Преглед изворни кода

rpc_module : update state when error happens.

liyingxin пре 1 година
родитељ
комит
65e57353d5

+ 1 - 1
docs/docs-01-idl.md

@@ -1,6 +1,6 @@
 [English version](/docs/en/docs-01-idl.md)
 
-## RPC IDL
+##  01 - RPC IDL
 - 描述文件
 - 前后兼容
 - Protobuf/Thrift

+ 1 - 1
docs/docs-02-service.md

@@ -1,6 +1,6 @@
 [English version](/docs/en/docs-02-service.md)
 
-## RPC Service
+## 02 - RPC Service
 - 组成SRPC服务的基本单元
 - 每一个Service一定由某一种IDL生成
 - Service只与IDL有关,与网络通信具体协议无关

+ 1 - 1
docs/docs-03-server.md

@@ -1,6 +1,6 @@
 [English version](/docs/en/docs-03-server.md)
 
-## RPC Server
+## 03 - RPC Server
 - 每一个Server对应一个端口
 - 每一个Server对应一个确定的网络通信协议
 - 每一个Service可以添加到任意的Server里

+ 1 - 1
docs/docs-04-client.md

@@ -1,6 +1,6 @@
 [English version](/docs/en/docs-04-client.md)
 
-## RPC Client
+## 04 - RPC Client
 - 每一个Client对应着一个确定的目标/一个确定的集群
 - 每一个Client对应着一个确定的网络通信协议
 - 每一个Client对应着一个确定的IDL

+ 1 - 1
docs/docs-05-context.md

@@ -1,6 +1,6 @@
 [English version](/docs/en/docs-05-context.md)
 
-## RPC Context
+## 05 - RPC Context
 - RPCContext专门用来辅助异步接口,Service和Client通用
 - 每一个异步接口都会提供Context,用来给用户提供更高级的功能,比如获取对方ip、获取连接seqid等
 - Context上一些功能是Server或Client独有的,比如Server可以设置回复数据的压缩方式,Client可以获取请求成功或失败

+ 1 - 1
docs/docs-06-workflow.md

@@ -1,6 +1,6 @@
 [English version](/docs/en/docs-06-workflow.md)
 
-## 与workflow异步框架的结合
+## 06 - 与workflow异步框架的结合
 ### 1. Server
 下面我们通过一个具体例子来呈现
 - Echo RPC在接收到请求时,向下游发起一次http请求

+ 1 - 1
docs/docs-08-tracing.md

@@ -1,6 +1,6 @@
 [English version](/docs/en/docs-08-tracing.md)
 
-## 上报Tracing到OpenTelemetry
+## 08 - 上报Tracing到OpenTelemetry
 **SRPC**支持产生和上报链路信息trace和span,并且可以通过多种途径进行上报,其中包括本地导出数据和上报到[OpenTelemetry](https://opentelemetry.io).
 
 **SRPC**遵循**OpenTelemetry**的[数据规范(data specification)](https://github.com/open-telemetry/opentelemetry-specification)以及[w3c的trace context](https://www.w3.org/TR/trace-context/),因此可以使用插件**RPCTraceOpenTelemetry**进行上报。

+ 1 - 2
docs/docs-09-metrics.md

@@ -1,7 +1,6 @@
 [English version](/docs/en/docs-09-metrics.md)
 
-## 上报Metrics
-
+## 09 - 上报Metrics
 
 **Metrics**(指标)是常用的监控需求,**SRPC**支持产生与统计Metrics,并通过多种途径上报,其中包括上报到[Prometheus](https://prometheus.io/)和[OpenTelemetry](https://opentelemetry.io)。
 

+ 4 - 4
docs/docs-10-http-with-modules.md

@@ -1,5 +1,5 @@
 
-## 带生态插件的HttpServer和HttpClient
+## 10 - 带生态插件的HttpServer和HttpClient
 
 **srpc**提供带有插件功能的**HttpServer**和**HttpClient**,可以上报**trace**和**metrcis**,用法和功能完全兼容**Workflow**,添加插件的用法也和srpc目前的Server和Client一样,用于使用**Http功能**同时需**要采集trace、metrics等信息并上报**的场景。
 
@@ -89,8 +89,8 @@ SRPC框架的trace模块已经采集以下内容,并会通过各自的filter
 
 | 指标名 | 含义 | 类型 | 例子 | 备注 |
 |-------|-----|-----|------|-----|
-|srpc.state|  框架状态码  | int | 0 | |
-|srpc.error|  框架错误码  | int | 1 |state!=0时才有 |
+|task.state|  框架状态码  | int | 0 |(以下简称state) |
+|task.error|  框架错误码  | int | 1 |state!=0时才有 |
 |http.status_code|  Http返回码  | string | 200 | state=0时才有 |
 |http.method|  Http请求方法  | string | GET |  |
 |http.scheme|  scheme  | string | https |  |
@@ -104,7 +104,6 @@ SRPC框架的trace模块已经采集以下内容,并会通过各自的filter
 
 | 指标名 | 含义 | 类型 | 例子 | 备注 |
 |-------|-----|-----|------|-----|
-|srpc.error|  框架错误码  | int | 1 | state不为0时才有 |
 |srpc.timeout_reason|  超时原因  | int | 2 | state=WFT_STATE_SYS_ERROR(1)和error=ETIMEDOUT(116)时才有 |
 |http.resend_count|  框架重试次数  | int | 0 | state=0时才有 |
 |net.peer.addr| uri请求的地址 | string | 10.xx.xx.xx | |
@@ -274,6 +273,7 @@ resource_spans {
 - 用法不同:本次新增的Http模块是延续Workflow风格的用法,比如WFHttpTask;而SRPCHttp/ThriftHttp/TRPCHttp的用法是RPC模式,且包括了同步/异步/半同步的使用方式;
 - 接口不同:前者用url直接定位要发的请求,而server也是一个process函数作为处理请求的统一入口;而原先的模块对Http只是网络层面的收发,url中的路由信息是通过${service}和${method}进行拼接的,然后把Protobuf或者Thrift这个结构体作为Http协议的body发出;
 - 开发者接触的请求/回复不同:前者从task上拿出HttpRequest和HttpResponse,后者是Protobuf/Thrift里定义的Message;
+- 框架级state和error略有不同:前者是task.state和task.error,使用workflow的状态码,比如0表示成功;而后者是srpc.state和srpc.error,使用SRPC的[状态码](/src/rpc_basic.h),比如1表示成功;
 
 **Q2: 和Workflow原生的Http协议是什么关系?**
 

+ 1 - 1
docs/en/docs-01-idl.md

@@ -1,6 +1,6 @@
 [中文版](/docs/docs-01-idl.md)
 
-## RPC IDL
+## 01 - RPC IDL
 
 - Interface Description Languaue file
 - Backward and forward compatibility

+ 1 - 1
docs/en/docs-02-service.md

@@ -1,6 +1,6 @@
 [中文版](/docs/docs-02-service.md)
 
-## RPC Service
+## 02 - RPC Service
 
 - It is the basic unit for SRPC services.
 - Each service must be generated by one type of IDLs.

+ 1 - 1
docs/en/docs-03-server.md

@@ -1,6 +1,6 @@
 [中文版](/docs/docs-03-server.md)
 
-## RPC Server
+## 03 - RPC Server
 
 - Each server corresponds to one port
 - Each server corresponds to one specific network communication protocol

+ 1 - 1
docs/en/docs-04-client.md

@@ -1,6 +1,6 @@
 [中文版](/docs/docs-04-client.md)
 
-## RPC Client
+## 04 - RPC Client
 
 - Each Client corresponds to one specific target/one specific cluster
 - Each Client corresponds to one specific network communication protocol

+ 1 - 1
docs/en/docs-05-context.md

@@ -1,6 +1,6 @@
 [中文版](/docs/docs-05-context.md)
 
-## RPC Context
+## 05 - RPC Context
 
 - RPCContext is used specially to assist asynchronous interfaces, and can be used in both Service and Client.
 - Each asynchronous interface will provide a Context, which offers higher-level functions, such as obtaining the remote IP, the connection seqid, and so on.

+ 1 - 1
docs/en/docs-06-workflow.md

@@ -1,6 +1,6 @@
 [中文版](/docs/docs-06-workflow.md)
 
-## Integrating with the asynchronous Workflow framework
+## 06 - Integrating with the asynchronous Workflow framework
 
 ### 1. Server
 

+ 1 - 1
docs/en/docs-08-tracing.md

@@ -1,6 +1,6 @@
 [中文版](/docs/docs-08-tracing.md)
 
-## Report Tracing to OpenTelemetry
+## 08 - Report Tracing to OpenTelemetry
 **SRPC** supports generating and reporting tracing and spans, which can be reported in multiple ways, including exporting data locally or to [OpenTelemetry](https://opentelemetry.io).
 
 Since **SRPC** follows the [data specification](https://github.com/open-telemetry/opentelemetry-specification) of **OpenTelemetry** and the specification of [w3c trace context](https://www.w3.org/TR/trace-context/), now we can use **RPCTraceOpenTelemetry** as the reporting plugin.

+ 1 - 1
docs/en/docs-09-metrics.md

@@ -1,6 +1,6 @@
 [中文版](/docs/docs-09-metrics.md)
 
-## Report Metrics to OpenTelemetry / Prometheus
+## 09 - Report Metrics to OpenTelemetry / Prometheus
 
 **Metrics** are common monitoring requirements. **SRPC** supports the generation and statistics of Metrics, and reports through various way, including reporting to [Prometheus](https://prometheus.io/) and [OpenTelemetry](https://opentelemetry.io).
 

+ 2 - 2
src/http/http_module.cc

@@ -49,10 +49,10 @@ bool HttpTraceModule::client_end(SubTask *task, RPCModuleData& data) const
 
 	auto *client_task = static_cast<HttpClientTask *>(task);
 
-	data[SRPC_STATE] = std::to_string(client_task->get_state());
+	data[WF_TASK_STATE] = std::to_string(client_task->get_state());
 	if (client_task->get_state() != WFT_STATE_SUCCESS)
 	{
-		data[SRPC_ERROR] = std::to_string(client_task->get_error());
+		data[WF_TASK_ERROR] = std::to_string(client_task->get_error());
 		if (client_task->get_error() == ETIMEDOUT)
 		{
 			data[SRPC_TIMEOUT_REASON] =

+ 8 - 0
src/module/rpc_trace_filter.cc

@@ -170,6 +170,12 @@ static size_t rpc_span_log_format(RPCModuleData& data, char *str, size_t len)
 							data[SRPC_FINISH_TIMESTAMP].c_str(),
 							data[SRPC_DURATION].c_str());
 		}
+		else if (strcmp(iter.first.c_str(), SRPC_STATE) == 0 ||
+				 strcmp(iter.first.c_str(), SRPC_ERROR) == 0)
+		{
+			ret += snprintf(str + ret, len - ret, " %s: %s",
+							iter.first.c_str(), iter.second.c_str());
+		}
 /*
 		else if (strcmp(it.first.c_str(), SRPC_SPAN_LOG) == 0)
 		{
@@ -323,6 +329,8 @@ SubTask *RPCTraceOpenTelemetry::create(RPCModuleData& span)
 		next = WFTaskFactory::create_empty_task();
 	else
 	{
+//		fprintf(stderr, "[Trace info to report] :\n%s\n\n",
+//				req->DebugString().c_str());
 		req->SerializeToString(output);
 		this->report_status = false;
 		this->report_span_count = 0;

+ 2 - 0
src/module/rpc_trace_module.h

@@ -49,6 +49,8 @@ static constexpr char const *SRPC_SPAN_KIND_CLIENT	= "srpc.client";
 static constexpr char const *SRPC_SPAN_KIND_SERVER	= "srpc.server";
 static constexpr char const *SRPC_STATE				= "srpc.state";
 static constexpr char const *SRPC_ERROR				= "srpc.error";
+static constexpr char const *WF_TASK_STATE			= "task.state";
+static constexpr char const *WF_TASK_ERROR			= "task.error";
 static constexpr char const *SRPC_REMOTE_IP			= "srpc.peer.ip";
 static constexpr char const *SRPC_REMOTE_PORT		= "srpc.peer.port";
 static constexpr char const *SRPC_SAMPLING_PRIO		= "srpc.sampling.priority";

+ 31 - 30
src/rpc_task.inl

@@ -288,22 +288,24 @@ CommMessageOut *RPCServerTask<RPCREQ, RPCRESP>::message_out()
 		status_code = this->resp.compress();
 		if (status_code == RPCStatusOK)
 		{
-			// for server, this is the where series->module_data stored
-			RPCModuleData *data = this->mutable_module_data();
+			if (!this->resp.serialize_meta())
+				status_code = RPCStatusMetaError;
+		}
+	}
 
-			for (auto *module : modules_)
-				module->server_task_end(this, *data);
+	this->resp.set_status_code(status_code);
 
-			this->resp.set_meta_module_data(*data);
+	// for server, this is the where series->module_data stored
+	RPCModuleData *data = this->mutable_module_data();
 
-			if (this->resp.serialize_meta())
-				return this->WFServerTask<RPCREQ, RPCRESP>::message_out();
+	for (auto *module : modules_)
+		module->server_task_end(this, *data);
 
-			status_code = RPCStatusMetaError;
-		}
-	}
+	this->resp.set_meta_module_data(*data);
+
+	if (status_code == RPCStatusOK)
+		return this->WFServerTask<RPCREQ, RPCRESP>::message_out();
 
-	this->resp.set_status_code(status_code);
 	errno = EBADMSG;
 	return NULL;
 }
@@ -444,31 +446,31 @@ CommMessageOut *RPCClientTask<RPCREQ, RPCRESP>::message_out()
 
 	if (status_code == RPCStatusOK)
 	{
-		RPCModuleData *data = NULL;
-		SERIES *series = dynamic_cast<SERIES *>(series_of(this));
-
-		if (series)
-		{
-			data = series->get_module_data();
-			if (data != NULL)
-				this->set_module_data(*data);
-		}
+		if (!this->req.serialize_meta())
+			status_code = RPCStatusMetaError;
+	}
 
-		data = this->mutable_module_data();
+	this->resp.set_status_code(status_code);
 
-		for (auto *module : modules_)
-			module->client_task_begin(this, *data);
+	RPCModuleData *data = NULL;
+	SERIES *series = dynamic_cast<SERIES *>(series_of(this));
+	if (series)
+	{
+		data = series->get_module_data();
+		if (data != NULL)
+			this->set_module_data(*data);
+	}
+	data = this->mutable_module_data();
 
-		this->req.set_meta_module_data(*data);
+	for (auto *module : modules_)
+		module->client_task_begin(this, *data);
 
-		if (this->req.serialize_meta())
-			return this->WFClientTask<RPCREQ, RPCRESP>::message_out();
+	this->req.set_meta_module_data(*data);
 
-		status_code = RPCStatusMetaError;
-	}
+	if (status_code == RPCStatusOK)
+		return this->WFClientTask<RPCREQ, RPCRESP>::message_out();
 
 	this->disable_retry();
-	this->resp.set_status_code(status_code);
 	errno = EBADMSG;
 	return NULL;
 }
@@ -575,7 +577,6 @@ void RPCClientTask<RPCREQ, RPCRESP>::rpc_callback(WFNetworkTask<RPCREQ, RPCRESP>
 			module->client_task_end(this, *resp_data);
 	}
 
-
 	user_done_(status_code, worker);
 }
 

+ 1 - 0
tools/templates/common/GNUmakefile

@@ -12,6 +12,7 @@ base:
 
 clean:
 ifeq ($(BUILD_DIR), $(wildcard $(BUILD_DIR)))
+	make -C $(BUILD_DIR) clean
 	rm -rf $(BUILD_DIR)
 endif
 

+ 1 - 1
tutorial/tutorial-17-http_server.cc

@@ -25,7 +25,7 @@ static WFFacilities::WaitGroup wait_group(1);
 
 srpc::RPCMetricsPull  exporter;
 srpc::RPCTraceDefault trace_log;
-//srpc::RPCTraceOpenTelemetry otel("http://127.0.0.1:8081");
+//srpc::RPCTraceOpenTelemetry otel("http://127.0.0.1:4318");
 
 static void sig_handler(int signo)
 {