Skip to content

Commit 1cf3c68

Browse files
Merge pull request #37 from tianxiaoliang/master
adapt to latest go chassis,remove go-metrics
2 parents 098e36d + 030aaf1 commit 1cf3c68

File tree

10 files changed

+91
-303
lines changed

10 files changed

+91
-303
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
### Metric Collector for Go-Chassis
22
[![Build Status](https://travis-ci.org/ServiceComb/cse-collector.svg?branch=master)](https://travis-ci.org/ServiceComb/cse-collector)
3-
This a metric collector for Go-Chassis which collects metrics of the microservices.
4-
It can collect metrics for each api's exposed by the micro-services. The same data can be
5-
exposed to Huawei CSE Governance Dashboard.
3+
This is a reporter plugin for go-chassis
4+
which report circuit breaker metrics to Huaweicloud.
65

76
# How to use
87

@@ -12,7 +11,7 @@ import _ "github.com/huaweicse/cse-collector"
1211
```
1312

1413
# Introdction
15-
The metrics collected by this collector is listed below:
14+
The metrics reported by this collector is listed below:
1615
```
1716
attempts
1817
errors
@@ -50,8 +49,9 @@ CountTimeout int64 `json:"countTimeout"`
5049
```
5150

5251
#### Configurations
53-
You need to configure your microservice to send the data at regular interval to
54-
Huawei CSE Dashboard.
52+
in chassis.yaml file.
53+
You need to configure your micro service to send the data to
54+
Huaweicloud ServiceStage.
5555

5656
```
5757
cse:
@@ -157,5 +157,3 @@ Every 10sec data will be flushed
157157
]
158158
]
159159
```
160-
161-
If perticular micro service has more than one instance and if request has been sent to perticular instance then only for that instance all the value should change.

cse_collector.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"crypto/tls"
88
"net/http"
99
"time"
10-
11-
"github.com/rcrowley/go-metrics"
1210
)
1311

1412
// CseCollectorConfig is a struct to keep monitoring information
@@ -24,6 +22,6 @@ type CseCollectorConfig struct {
2422
}
2523

2624
// InitializeCseCollector starts the CSE collector in a new Thread
27-
func InitializeCseCollector(config *CseCollectorConfig, r metrics.Registry, app, version, service, env string) {
28-
go NewReporter(r, config.CseMonitorAddr, config.Header, config.TimeInterval, config.TLSConfig, app, version, service, env).Run()
25+
func InitializeCseCollector(config *CseCollectorConfig, app, version, service, env string) {
26+
NewReporter(config.CseMonitorAddr, config.Header, config.TimeInterval, config.TLSConfig, app, version, service, env).Run()
2927
}

csemonitor.go

Lines changed: 22 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1817
var IsMonitoringConnected bool
1918

2019
// Reporter is a struct to store the registry address and different monitoring information
2120
type 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
}

csemonitor_test.go

Lines changed: 0 additions & 135 deletions
This file was deleted.

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/huaweicse/cse-collector
2+
3+
require (
4+
github.com/go-chassis/foundation v0.0.0-20190621030543-c3b63f787f4c
5+
github.com/go-chassis/go-archaius v0.19.0
6+
github.com/go-chassis/go-chassis v1.5.2-0.20190713094014-10193b3a09b8
7+
github.com/go-mesh/openlogging v1.0.0
8+
)

0 commit comments

Comments
 (0)