-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcsemonitor.go
executable file
·66 lines (59 loc) · 2.15 KB
/
csemonitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package metricsink
import (
"fmt"
"github.com/go-chassis/go-archaius"
chassisRuntime "github.com/go-chassis/go-chassis/v2/pkg/runtime"
"github.com/go-chassis/go-chassis/v2/third_party/forked/afex/hystrix-go/hystrix"
"github.com/go-chassis/openlog"
"github.com/huaweicse/cse-collector/pkg/monitoring"
"os"
"runtime"
)
// IsMonitoringConnected is a boolean to keep an check if there exsist any succeful connection to monitoring Server
var IsMonitoringConnected bool
// Reporter is a struct to store the registry address and different monitoring information
type Reporter struct {
environment string
c *monitoring.CseMonitorClient
}
// NewReporter creates a new monitoring object for CSE type collections
func NewReporter(config *CseCollectorConfig) (*Reporter, error) {
c, err := monitoring.NewCseMonitorClient(config.Header, config.CseMonitorAddr, config.TLSConfig)
if err != nil {
openlog.Error(fmt.Sprintf("Get cse monitor client failed:%s", err))
return nil, err
}
IsMonitoringConnected = true
return &Reporter{
environment: config.Env,
c: c,
}, nil
}
//Send send metrics to monitoring service
func (reporter *Reporter) Send(cb *hystrix.CircuitBreaker) {
if archaius.GetBool("cse.monitor.client.enable", true) {
monitorData := reporter.getData(cb)
openlog.Debug("send metrics", openlog.WithTags(openlog.Tags{
"data": monitorData,
}))
err := reporter.c.PostMetrics(monitorData)
if err != nil {
openlog.Warn(fmt.Sprintf("unable to report to monitoring server, err: %v", err))
}
}
}
func (reporter *Reporter) getData(cb *hystrix.CircuitBreaker) monitoring.MonitorData {
var monitorData = monitoring.NewMonitorData()
monitorData.AppID = chassisRuntime.App
monitorData.Version = chassisRuntime.Version
monitorData.Name = chassisRuntime.ServiceName
monitorData.ServiceID = chassisRuntime.ServiceID
monitorData.InstanceID = chassisRuntime.InstanceID
monitorData.Environment = reporter.environment
monitorData.Instance, _ = os.Hostname()
monitorData.Memory = getProcessInfo()
monitorData.Thread = threadCreateProfile.Count()
monitorData.CPU = float64(runtime.NumCPU())
monitorData.AppendInterfaceInfo(cb)
return *monitorData
}