@@ -4,22 +4,20 @@ import (
44 "crypto/tls"
55 "net/http"
66 "os"
7- "runtime"
87 "time"
98
109 "github.com/go-chassis/go-archaius"
11- "github.com/go-chassis/go-chassis/core/lager "
12- "github.com/go-chassis/go-chassis/core/registry "
13- runtime2 "github.com/go-chassis/go-chassis/pkg/runtime "
14- "github.com/rcrowley/go-metrics "
10+ chassisRuntime "github.com/go-chassis/go-chassis/pkg/runtime "
11+ "github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix "
12+ "github.com/go-mesh/openlogging "
13+ "runtime "
1514)
1615
1716// IsMonitoringConnected is a boolean to keep an check if there exsist any succeful connection to monitoring Server
1817var IsMonitoringConnected bool
1918
2019// Reporter is a struct to store the registry address and different monitoring information
2120type Reporter struct {
22- Registry metrics.Registry
2321 CseMonitorAddr string
2422 Header http.Header
2523 Interval time.Duration
@@ -30,12 +28,12 @@ type Reporter struct {
3028 service string
3129 environment string
3230 serviceID string
31+ metricsAPI * CseMonitorClient
3332}
3433
3534// 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 string ) * Reporter {
35+ func NewReporter (addr string , header http.Header , interval time.Duration , tls * tls.Config , app , version , service , env string ) * Reporter {
3736 reporter := & Reporter {
38- Registry : r ,
3937 CseMonitorAddr : addr ,
4038 Header : header ,
4139 Interval : interval ,
@@ -46,78 +44,34 @@ func NewReporter(r metrics.Registry, addr string, header http.Header, interval t
4644 service : service ,
4745 environment : env ,
4846 }
47+ metricsAPI , err := NewCseMonitorClient (reporter .Header , reporter .CseMonitorAddr , reporter .TLSConfig , "v2" )
48+ if err != nil {
49+ openlogging .GetLogger ().Errorf ("Get cse monitor client failed:%s" , err )
50+ }
51+ reporter .metricsAPI = metricsAPI
52+ IsMonitoringConnected = true
4953 return reporter
5054}
5155
5256// Run creates a go_routine which runs continuously and capture the monitoring data
53- func (reporter * Reporter ) Run () {
54- var count int
57+ func (reporter * Reporter ) Run (cb * hystrix.CircuitBreaker ) {
5558 ticker := time .Tick (reporter .Interval )
56- metricsAPI , err := NewCseMonitorClient (reporter .Header , reporter .CseMonitorAddr , reporter .TLSConfig , "v2" )
57- if err != nil {
58- lager .Logger .Errorf ("Get cse monitor client failed:%s" , err )
59- }
60- IsMonitoringConnected = true
61- isConnctedForFirstTime := false
6259
6360 for range ticker {
64-
65- //If monitoring is enabled then only try to connect to Monitoring Server
6661 if archaius .GetBool ("cse.monitor.client.enable" , true ) {
67- reporter .serviceID = runtime2 .ServiceID
68-
69- instances , ok := registry .SelfInstancesCache .Get (reporter .serviceID )
70-
71- if ! ok {
72- lager .Logger .Warnf ("waiting for instance registration: [%s]" , reporter .serviceID )
73- continue
74- }
75-
76- instanceIDs , ok := instances .([]string )
77- if ! ok {
78- lager .Logger .Warnf ("type assert of instance failed, sid: %s" , reporter .serviceID )
79- continue
80- }
81-
82- for _ , instance := range instanceIDs {
83- monitorData := reporter .getData (reporter .app , reporter .version , reporter .service , reporter .environment , reporter .serviceID , instance )
84- err := metricsAPI .PostMetrics (monitorData )
85- if err != nil {
86- //If the connection fails for the first time then print Warn Logs
87- if IsMonitoringConnected {
88- lager .Logger .Warnf ("Unable to connect to monitoring server, err: %v" , err )
89- }
90- IsMonitoringConnected = false
91- } else {
92- //If Connection is established for first time then Print the Info logs
93- if ! isConnctedForFirstTime {
94- lager .Logger .Infof ("Connection to monitoring server established successfully" )
95- isConnctedForFirstTime = true
96- }
97- //If connection is recovered first time then print Info Logs
98- if ! IsMonitoringConnected {
99- lager .Logger .Infof ("Connection recovered successfully to monitoring server" )
100- }
101- IsMonitoringConnected = true
102- }
103-
104- if len (monitorData .Interfaces ) != 0 {
105- count ++
106- if count == 10 {
107- reporter .Registry .Each (func (s string , i interface {}) {
108- if c , ok := i .(metrics.Counter ); ok {
109- c .Clear ()
110- }
111- })
112- count = 0
113- }
114- }
62+ reporter .serviceID = chassisRuntime .ServiceID
63+ monitorData := reporter .getData (cb , reporter .app , reporter .version ,
64+ reporter .service , reporter .environment , reporter .serviceID , chassisRuntime .InstanceID )
65+ err := reporter .metricsAPI .PostMetrics (monitorData )
66+ if err != nil {
67+ openlogging .GetLogger ().Warnf ("Unable to report to monitoring server, err: %v" , err )
11568 }
11669 }
11770 }
11871}
11972
120- func (reporter * Reporter ) getData (app , version , service , env , serviceID , instanceID string ) MonitorData {
73+ func (reporter * Reporter ) getData (cb * hystrix.CircuitBreaker ,
74+ app , version , service , env , serviceID , instanceID string ) MonitorData {
12175 var monitorData = NewMonitorData ()
12276 monitorData .AppID = app
12377 monitorData .Version = version
@@ -129,8 +83,6 @@ func (reporter *Reporter) getData(app, version, service, env, serviceID, instanc
12983 monitorData .Memory = getProcessInfo ()
13084 monitorData .Thread = threadCreateProfile .Count ()
13185 monitorData .CPU = float64 (runtime .NumCPU ())
132- reporter .Registry .Each (func (name string , i interface {}) {
133- monitorData .appendInterfaceInfo (name , i )
134- })
86+ monitorData .appendInterfaceInfo (cb .Name , cb .Metrics .DefaultCollector ())
13587 return * monitorData
13688}
0 commit comments