@@ -5,10 +5,10 @@ import (
55 "net/http"
66 "os"
77 "runtime"
8- "strings"
98 "time"
109
1110 "github.com/ServiceComb/go-chassis/core/archaius"
11+ "github.com/ServiceComb/go-chassis/core/config"
1212 "github.com/ServiceComb/go-chassis/core/lager"
1313 "github.com/ServiceComb/go-chassis/core/registry"
1414 "github.com/rcrowley/go-metrics"
@@ -33,7 +33,7 @@ type Reporter struct {
3333}
3434
3535// NewReporter creates a new monitoring object for CSE type collections
36- func NewReporter (r metrics.Registry , addr string , header http.Header , interval time.Duration , tls * tls.Config , app , version , service , env , serviceID string ) * Reporter {
36+ func NewReporter (r metrics.Registry , addr string , header http.Header , interval time.Duration , tls * tls.Config , app , version , service , env string ) * Reporter {
3737 reporter := & Reporter {
3838 Registry : r ,
3939 CseMonitorAddr : addr ,
@@ -45,7 +45,6 @@ func NewReporter(r metrics.Registry, addr string, header http.Header, interval t
4545 version : version ,
4646 service : service ,
4747 environment : env ,
48- serviceID : serviceID ,
4948 }
5049 return reporter
5150}
@@ -61,25 +60,21 @@ func (reporter *Reporter) Run() {
6160
6261 //If monitoring is enabled then only try to connect to Monitoring Server
6362 if archaius .GetBool ("cse.monitor.client.enable" , true ) {
64- instances , err := registry .DefaultServiceDiscoveryService .GetMicroServiceInstances (reporter .serviceID , reporter .serviceID )
65- if err != nil {
66- if strings .Contains (err .Error (), "Micro-service does not exist" ) {
67- var serviceID string
68- for {
69- serviceID , err = registry .DefaultServiceDiscoveryService .GetMicroServiceID (reporter .app ,
70- reporter .service , reporter .version , reporter .environment )
71- if serviceID != "" {
72- break
73- }
74- }
75- reporter .serviceID = serviceID
76- } else {
77- lager .Logger .Errorf (err , "failed to get the instance list" )
78- }
63+ reporter .serviceID = config .SelfServiceID
64+
65+ instances , ok := registry .SelfInstancesCache .Get (reporter .serviceID )
66+
67+ if ! ok {
68+ lager .Logger .Warnf ("get SelfInstancesCache failed for, sid: %s" , reporter .serviceID )
69+ }
70+
71+ instanceIDs , ok := instances .([]string )
72+ if ! ok {
73+ lager .Logger .Warnf ("type asserts failed, sid: %s" , reporter .serviceID )
7974 }
8075
81- for _ , instance := range instances {
82- monitorData := reporter .getData (reporter .app , reporter .version , reporter .service , reporter .environment , reporter .serviceID , instance . InstanceID )
76+ for _ , instance := range instanceIDs {
77+ monitorData := reporter .getData (reporter .app , reporter .version , reporter .service , reporter .environment , reporter .serviceID , instance )
8378 err := metricsAPI .PostMetrics (monitorData )
8479 if err != nil {
8580 //If the connection fails for the first time then print Warn Logs
0 commit comments