Repository: cloudwego/hertz
Pull request #58 (2022-06-15 07:01:47)

optimize: add remote address in the error log
#### What type of PR is this?
optimize
<!--
Add one of the following kinds:

build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs: Documentation only changes
feat: A new feature
optimize: A new optimization
fix: A bug fix
perf: A code change that improves performance
refactor: A code change that neither fixes a bug nor adds a feature
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
test: Adding missing tests or correcting existing tests
chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
-->

#### What this PR does / why we need it (English/Chinese):
en: add remote address in the error log when server processing the error.
zh: 服务端错误日志中添加对端地址。

<!--
The description will be attached in Release Notes, 
so please describe it from user-oriented.
-->

#### Which issue(s) this PR fixes:
[#55]
<!--
*Automatically closes linked issue when PR is merged.
Eg: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->



Patch:
diff --git a/README.md b/README.md
index 6b32f8f..5dc0d04 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,16 @@
 
 English | [中文](README_cn.md)
 
+[![Release](https://img.shields.io/github/v/release/cloudwego/hertz)](https://github.com/cloudwego/hertz/releases)
+[![WebSite](https://img.shields.io/website?up_message=cloudwego&url=https%3A%2F%2Fwww.cloudwego.io%2F)](https://www.cloudwego.io/)
+[![License](https://img.shields.io/github/license/cloudwego/hertz)](https://github.com/cloudwego/hertz/blob/main/LICENSE)
+[![Go Report Card](https://goreportcard.com/badge/github.com/cloudwego/hertz)](https://goreportcard.com/report/github.com/cloudwego/hertz)
+[![OpenIssue](https://img.shields.io/github/issues/cloudwego/hertz)](https://github.com/cloudwego/hertz/issues)
+[![ClosedIssue](https://img.shields.io/github/issues-closed/cloudwego/hertz)](https://github.com/cloudwego/hertz/issues?q=is%3Aissue+is%3Aclosed)
+![Stars](https://img.shields.io/github/stars/cloudwego/hertz)
+![Forks](https://img.shields.io/github/forks/cloudwego/hertz)
+
+
 Hertz [həːts] is a high-usability, high-performance and high-extensibility Golang HTTP framework that helps developers build microservices. It was designed with reference to other open-source frameworks like [fasthttp](https://github.com/valyala/fasthttp)、[gin](https://github.com/gin-gonic/gin)、[echo](https://github.com/labstack/echo) and combined with the internal requirements in ByteDance. At present, it has been widely used inside ByteDance. Nowadays, more and more microservices use Golang. If you have requirements for microservice performance and hope that the framework can fully meet the internal customizable requirements, Hertz will be a good choice.
 ## Basic Features
 - High usability
diff --git a/README_cn.md b/README_cn.md
index 61971c9..9079a48 100644
--- a/README_cn.md
+++ b/README_cn.md
@@ -2,6 +2,15 @@
 
 [English](README.md) | 中文
 
+[![Release](https://img.shields.io/github/v/release/cloudwego/hertz)](https://github.com/cloudwego/hertz/releases)
+[![WebSite](https://img.shields.io/website?up_message=cloudwego&url=https%3A%2F%2Fwww.cloudwego.io%2F)](https://www.cloudwego.io/)
+[![License](https://img.shields.io/github/license/cloudwego/hertz)](https://github.com/cloudwego/hertz/blob/main/LICENSE)
+[![Go Report Card](https://goreportcard.com/badge/github.com/cloudwego/hertz)](https://goreportcard.com/report/github.com/cloudwego/hertz)
+[![OpenIssue](https://img.shields.io/github/issues/cloudwego/hertz)](https://github.com/cloudwego/hertz/issues)
+[![ClosedIssue](https://img.shields.io/github/issues-closed/cloudwego/hertz)](https://github.com/cloudwego/hertz/issues?q=is%3Aissue+is%3Aclosed)
+![Stars](https://img.shields.io/github/stars/cloudwego/hertz)
+![Forks](https://img.shields.io/github/forks/cloudwego/hertz)
+
 Hertz[həːts] 是一个 Golang 微服务 HTTP 框架，在设计之初参考了其他开源框架 [fasthttp](https://github.com/valyala/fasthttp)、[gin](https://github.com/gin-gonic/gin)、[echo](https://github.com/labstack/echo) 的优势，并结合字节跳动内部的需求，使其具有高易用性、高性能、高扩展性等特点，目前在字节跳动内部已广泛使用。如今越来越多的微服务选择使用 Golang，如果对微服务性能有要求，又希望框架能够充分满足内部的可定制化需求，Hertz 会是一个不错的选择。
 ## 框架特点
 - 高易用性
diff --git a/pkg/route/engine.go b/pkg/route/engine.go
index b8a6269..035549f 100644
--- a/pkg/route/engine.go
+++ b/pkg/route/engine.go
@@ -375,18 +375,29 @@ func errProcess(conn io.Closer, err error) {
 		return
 	}
 
+	// Get remote address
+	rip := getRemoteAddrFromCloser(conn)
+
 	// Handle netpoll error
 	if errors.Is(err, netpoll.ErrConnClosed) {
-		hlog.Warnf("HERTZ: Netpoll error=%s", err.Error())
+		hlog.Warnf("HERTZ: Netpoll error=%s, remoteAddr=%s", err.Error(), rip)
 		return
 	}
 	if errors.Is(err, netpoll.ErrReadTimeout) {
-		hlog.Errorf("HERTZ: Netpoll error=%s", err.Error())
+		hlog.Errorf("HERTZ: Netpoll error=%s, remoteAddr=%s", err.Error(), rip)
 		return
 	}
-
 	// other errors
-	hlog.Errorf("HERTZ: Error=%s", err.Error())
+	hlog.Errorf("HERTZ: Error=%s, remoteAddr=%s", err.Error(), rip)
+}
+
+func getRemoteAddrFromCloser(conn io.Closer) string {
+	if c, ok := conn.(network.Conn); ok {
+		if addr := c.RemoteAddr(); addr != nil {
+			return addr.String()
+		}
+	}
+	return ""
 }
 
 func (engine *Engine) Close() error {


Test patch:
diff --git a/pkg/common/testdata/proto/test.pb.go b/pkg/common/testdata/proto/test.pb.go
index 5f947d8..382b673 100644
--- a/pkg/common/testdata/proto/test.pb.go
+++ b/pkg/common/testdata/proto/test.pb.go
@@ -280,14 +280,17 @@ func file_test_proto_rawDescGZIP() []byte {
 	return file_test_proto_rawDescData
 }
 
-var file_test_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
-var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
-var file_test_proto_goTypes = []interface{}{
-	(FOO)(0),                   // 0: proto.FOO
-	(*Test)(nil),               // 1: proto.Test
-	(*TestStruct)(nil),         // 2: proto.TestStruct
-	(*Test_OptionalGroup)(nil), // 3: proto.Test.OptionalGroup
-}
+var (
+	file_test_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
+	file_test_proto_msgTypes  = make([]protoimpl.MessageInfo, 3)
+	file_test_proto_goTypes   = []interface{}{
+		(FOO)(0),                   // 0: proto.FOO
+		(*Test)(nil),               // 1: proto.Test
+		(*TestStruct)(nil),         // 2: proto.TestStruct
+		(*Test_OptionalGroup)(nil), // 3: proto.Test.OptionalGroup
+	}
+)
+
 var file_test_proto_depIdxs = []int32{
 	3, // 0: proto.Test.optionalgroup:type_name -> proto.Test.OptionalGroup
 	1, // [1:1] is the sub-list for method output_type