11package metricsink
22
33import (
4- "crypto/tls"
5- "net/http"
6- "os"
7- "time"
8-
94 "github.com/go-chassis/go-archaius"
105 chassisRuntime "github.com/go-chassis/go-chassis/pkg/runtime"
116 "github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix"
127 "github.com/go-mesh/openlogging"
8+ "github.com/huaweicse/cse-collector/pkg/monitoring"
9+ "os"
1310 "runtime"
1411)
1512
@@ -18,71 +15,51 @@ var IsMonitoringConnected bool
1815
1916// Reporter is a struct to store the registry address and different monitoring information
2017type Reporter struct {
21- CseMonitorAddr string
22- Header http.Header
23- Interval time.Duration
24- Percentiles []float64
25- TLSConfig * tls.Config
26- app string
27- version string
28- service string
29- environment string
30- serviceID string
31- metricsAPI * CseMonitorClient
18+ environment string
19+ c * monitoring.CseMonitorClient
3220}
3321
3422// NewReporter creates a new monitoring object for CSE type collections
35- func NewReporter (addr string , header http.Header , interval time.Duration , tls * tls.Config , app , version , service , env string ) * Reporter {
36- reporter := & Reporter {
37- CseMonitorAddr : addr ,
38- Header : header ,
39- Interval : interval ,
40- Percentiles : []float64 {0.5 , 0.75 , 0.95 , 0.99 , 0.999 },
41- TLSConfig : tls ,
42- app : app ,
43- version : version ,
44- service : service ,
45- environment : env ,
46- }
47- metricsAPI , err := NewCseMonitorClient (reporter .Header , reporter .CseMonitorAddr , reporter .TLSConfig , "v2" )
23+ func NewReporter (config * CseCollectorConfig ) (* Reporter , error ) {
24+ c , err := monitoring .NewCseMonitorClient (config .Header , config .CseMonitorAddr , config .TLSConfig )
4825 if err != nil {
4926 openlogging .GetLogger ().Errorf ("Get cse monitor client failed:%s" , err )
27+ return nil , err
5028 }
51- reporter .metricsAPI = metricsAPI
5229 IsMonitoringConnected = true
53- return reporter
30+ return & Reporter {
31+ environment : config .Env ,
32+ c : c ,
33+ }, nil
5434}
5535
56- // Run creates a go_routine which runs continuously and capture the monitoring data
57- func (reporter * Reporter ) Run (cb * hystrix.CircuitBreaker ) {
58- ticker := time .Tick (reporter .Interval )
59-
60- for range ticker {
61- if archaius .GetBool ("cse.monitor.client.enable" , true ) {
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 )
68- }
36+ //Send send metrics to monitoring service
37+ func (reporter * Reporter ) Send (cb * hystrix.CircuitBreaker ) {
38+ if archaius .GetBool ("cse.monitor.client.enable" , true ) {
39+ monitorData := reporter .getData (cb )
40+ openlogging .Debug ("send metrics" , openlogging .WithTags (openlogging.Tags {
41+ "data" : monitorData ,
42+ }))
43+ err := reporter .c .PostMetrics (monitorData )
44+ if err != nil {
45+ openlogging .GetLogger ().Warnf ("unable to report to monitoring server, err: %v" , err )
6946 }
7047 }
7148}
7249
73- func (reporter * Reporter ) getData (cb * hystrix.CircuitBreaker ,
74- app , version , service , env , serviceID , instanceID string ) MonitorData {
75- var monitorData = NewMonitorData ()
76- monitorData .AppID = app
77- monitorData .Version = version
78- monitorData .Name = service
79- monitorData .ServiceID = serviceID
80- monitorData . InstanceID = instanceID
81- monitorData .Environment = env
50+ func (reporter * Reporter ) getData (cb * hystrix.CircuitBreaker ) monitoring. MonitorData {
51+ var monitorData = monitoring . NewMonitorData ()
52+ monitorData . AppID = chassisRuntime . App
53+ monitorData .Version = chassisRuntime . Version
54+ monitorData .Name = chassisRuntime . ServiceName
55+ monitorData .ServiceID = chassisRuntime . ServiceID
56+ monitorData .InstanceID = chassisRuntime . InstanceID
57+
58+ monitorData .Environment = reporter . environment
8259 monitorData .Instance , _ = os .Hostname ()
8360 monitorData .Memory = getProcessInfo ()
8461 monitorData .Thread = threadCreateProfile .Count ()
8562 monitorData .CPU = float64 (runtime .NumCPU ())
86- monitorData .appendInterfaceInfo (cb . Name , cb . Metrics . DefaultCollector () )
63+ monitorData .AppendInterfaceInfo (cb )
8764 return * monitorData
8865}
0 commit comments