diff --git a/client/ehttp/component.go b/client/ehttp/component.go index deb08656..5ceb840a 100644 --- a/client/ehttp/component.go +++ b/client/ehttp/component.go @@ -27,9 +27,7 @@ type Component struct { func newComponent(name string, config *Config, logger *elog.Component) *Component { // resty的默认方法,无法设置长连接个数,和是否开启长连接,这里重新构造http client。 cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) - var interceptors []func(name string, cfg *Config, logger *elog.Component) (resty.RequestMiddleware, resty.ResponseMiddleware, resty.ErrorHook) - interceptors = append(interceptors, fixedInterceptor, logInterceptor, metricInterceptor, traceInterceptor) - + interceptors := []interceptor{fixedInterceptor, logInterceptor, metricInterceptor} cli := resty.NewWithClient(&http.Client{Transport: createTransport(config), Jar: cookieJar}). SetDebug(config.RawDebug). SetTimeout(config.ReadTimeout). diff --git a/client/ehttp/interceptor.go b/client/ehttp/interceptor.go index 5523ad25..a8900948 100644 --- a/client/ehttp/interceptor.go +++ b/client/ehttp/interceptor.go @@ -19,12 +19,11 @@ import ( "github.com/gotomicro/ego/core/util/xdebug" ) +type interceptor func(name string, cfg *Config, logger *elog.Component) (resty.RequestMiddleware, resty.ResponseMiddleware, resty.ErrorHook) + func logAccess(name string, config *Config, logger *elog.Component, req *resty.Request, res *resty.Response, err error) { rr := req.RawRequest - var ( - url string - host string - ) + var url, host string // 修复err 不是 *resty.ResponseError错误的时候,可能为nil if rr != nil { url = rr.URL.RequestURI() diff --git a/client/ehttp/options.go b/client/ehttp/options.go index 9ed68207..93fd12c5 100644 --- a/client/ehttp/options.go +++ b/client/ehttp/options.go @@ -37,17 +37,10 @@ func WithSlowLogThreshold(slowLogThreshold time.Duration) Option { } } -// WithEnableAccessInterceptor 设置开启请求日志 -func WithEnableAccessInterceptor(enableAccessInterceptor bool) Option { - return func(c *Container) { - c.config.EnableAccessInterceptor = enableAccessInterceptor - } -} - -// WithEnableAccessInterceptorRes 设置开启请求日志响应参数 -func WithEnableAccessInterceptorRes(enableAccessInterceptorRes bool) Option { +// WithIdleConnTimeout 设置空闲连接时间 +func WithIdleConnTimeout(idleConnTimeout time.Duration) Option { return func(c *Container) { - c.config.EnableAccessInterceptorRes = enableAccessInterceptorRes + c.config.IdleConnTimeout = idleConnTimeout } } @@ -65,9 +58,30 @@ func WithMaxIdleConnsPerHost(maxIdleConnsPerHost int) Option { } } +// WithEnableTraceInterceptor 设置开启Trace拦截器 +func WithEnableTraceInterceptor(enableTraceInterceptor bool) Option { + return func(c *Container) { + c.config.EnableTraceInterceptor = enableTraceInterceptor + } +} + // WithEnableKeepAlives 设置是否开启长连接,默认打开 func WithEnableKeepAlives(enableKeepAlives bool) Option { return func(c *Container) { c.config.EnableKeepAlives = enableKeepAlives } } + +// WithEnableAccessInterceptor 设置开启请求日志 +func WithEnableAccessInterceptor(enableAccessInterceptor bool) Option { + return func(c *Container) { + c.config.EnableAccessInterceptor = enableAccessInterceptor + } +} + +// WithEnableAccessInterceptorRes 设置开启请求日志响应参数 +func WithEnableAccessInterceptorRes(enableAccessInterceptorRes bool) Option { + return func(c *Container) { + c.config.EnableAccessInterceptorRes = enableAccessInterceptorRes + } +} diff --git a/core/util/xdebug/print.go b/core/util/xdebug/print.go index ae63abb0..a57455f5 100644 --- a/core/util/xdebug/print.go +++ b/core/util/xdebug/print.go @@ -10,13 +10,13 @@ import ( ) // MakeReqResInfo ... -// Deprecated: MakeReqResInfo ... +// Deprecated: MakeReqResInfo will be removed in v0.10 func MakeReqResInfo(compName string, addr string, cost time.Duration, req interface{}, reply interface{}) string { return fmt.Sprintf("%s %s %s %s => %s\n", xcolor.Green(compName), xcolor.Green(addr), xcolor.Yellow(fmt.Sprintf("[%vms]", float64(cost.Microseconds())/1000)), xcolor.Blue(fmt.Sprintf("%v", req)), xcolor.Blue(fmt.Sprintf("%v", reply))) } // MakeReqResError ... -// Deprecated: MakeReqResError 以error级别打印配置名、目标地址、耗时、请求数据、响应数据 +// Deprecated: MakeReqResError will be removed in v0.10 func MakeReqResError(compName string, addr string, cost time.Duration, req string, err string) string { return fmt.Sprintf("%s %s %s %s => %s\n", xcolor.Red(compName), xcolor.Red(addr), xcolor.Yellow(fmt.Sprintf("[%vms]", float64(cost.Microseconds())/1000)), xcolor.Blue(fmt.Sprintf("%v", req)), xcolor.Red(err)) }