@@ -33,6 +33,7 @@ import (
3333 "time"
3434
3535 "github.com/polarismesh/polaris-go"
36+ "github.com/polarismesh/polaris-go/api"
3637 "github.com/polarismesh/polaris-go/pkg/config"
3738 "github.com/polarismesh/polaris-go/pkg/model"
3839)
4647 port int
4748 token string
4849 configPath string
50+ debug bool
4951)
5052
5153func initArgs () {
@@ -57,6 +59,7 @@ func initArgs() {
5759 flag .IntVar (& port , "port" , 18080 , "port" )
5860 flag .StringVar (& token , "token" , "" , "token" )
5961 flag .StringVar (& configPath , "config" , "./polaris.yaml" , "path for config file" )
62+ flag .BoolVar (& debug , "debug" , false , "debug" )
6063}
6164
6265// PolarisClient is a consumer of the circuit breaker calleeService.
@@ -69,6 +72,26 @@ type PolarisClient struct {
6972 webSvr * http.Server
7073}
7174
75+ // reportServiceCallResult 上报服务调用结果的辅助方法
76+ func (svr * PolarisClient ) reportServiceCallResult (instance model.Instance , retStatus model.RetStatus , statusCode int , delay time.Duration ) {
77+ ret := & polaris.ServiceCallResult {
78+ ServiceCallResult : model.ServiceCallResult {
79+ EmptyInstanceGauge : model.EmptyInstanceGauge {},
80+ CalledInstance : instance ,
81+ Method : "/echo" ,
82+ RetStatus : retStatus ,
83+ },
84+ }
85+ ret .SetDelay (delay )
86+ ret .SetRetCode (int32 (statusCode ))
87+ if err := svr .consumer .UpdateServiceCallResult (ret ); err != nil {
88+ log .Printf ("do report service call result : %+v" , err )
89+ } else {
90+ log .Printf ("report service call result success: instance=%s:%d, status=%v, retCode=%d, delay=%v" ,
91+ instance .GetHost (), instance .GetPort (), ret .RetStatus , ret .GetRetCode (), delay )
92+ }
93+ }
94+
7295func (svr * PolarisClient ) discoverInstance () (model.Instance , error ) {
7396 svr .printAllInstances ()
7497 getOneRequest := & polaris.GetOneInstanceRequest {}
@@ -111,6 +134,11 @@ func (svr *PolarisClient) runWebServer() {
111134 instance .GetHost (), instance .GetPort (), err )))
112135
113136 time .Sleep (time .Millisecond * time .Duration (rand .Intn (10 )))
137+
138+ // 上报服务调用结果
139+ delay := time .Since (start )
140+ svr .reportServiceCallResult (instance , model .RetFail , http .StatusInternalServerError , delay )
141+
114142 // 上报熔断结果,用于熔断计算
115143 svr .reportCircuitBreak (instance , model .RetFail , strconv .Itoa (http .StatusInternalServerError ), start )
116144 return
@@ -119,6 +147,16 @@ func (svr *PolarisClient) runWebServer() {
119147
120148 defer resp .Body .Close ()
121149
150+ // 上报服务调用结果
151+ delay := time .Since (start )
152+ retStatus := model .RetSuccess
153+ if resp .StatusCode == http .StatusTooManyRequests {
154+ retStatus = model .RetFlowControl
155+ } else if resp .StatusCode != http .StatusOK {
156+ retStatus = model .RetFail
157+ }
158+ svr .reportServiceCallResult (instance , retStatus , resp .StatusCode , delay )
159+
122160 // 上报熔断结果,用于熔断计算
123161 if resp .StatusCode != http .StatusOK {
124162 svr .reportCircuitBreak (instance , model .RetFail ,
@@ -271,6 +309,14 @@ func main() {
271309 log .Print ("calleeNamespace and calleeService are required" )
272310 return
273311 }
312+ if debug {
313+ // 设置日志级别为DEBUG
314+ if err := api .SetLoggersLevel (api .DebugLog ); err != nil {
315+ log .Printf ("fail to set log level to DEBUG, err is %v" , err )
316+ } else {
317+ log .Printf ("successfully set log level to DEBUG" )
318+ }
319+ }
274320 cfg , err := config .LoadConfigurationByFile (configPath )
275321 if err != nil {
276322 log .Fatalf ("load configuration by file %s failed: %v" , configPath , err )
0 commit comments