Skip to content

Commit c0c591c

Browse files
committed
migrate collector to go chassis
go-chassis/go-chassis#419
1 parent b331c3f commit c0c591c

File tree

4 files changed

+3
-219
lines changed

4 files changed

+3
-219
lines changed

cse_collector.go

Lines changed: 0 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,9 @@ import (
88
"net/http"
99
"time"
1010

11-
"github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix/metric_collector"
1211
"github.com/rcrowley/go-metrics"
1312
)
1413

15-
// CseCollector is a struct to keeps metric information of Http requests
16-
type CseCollector struct {
17-
attempts string
18-
errors string
19-
successes string
20-
failures string
21-
rejects string
22-
shortCircuits string
23-
timeouts string
24-
fallbackSuccesses string
25-
fallbackFailures string
26-
totalDuration string
27-
runDuration string
28-
}
29-
3014
// CseCollectorConfig is a struct to keep monitoring information
3115
type CseCollectorConfig struct {
3216
// CseMonitorAddr is the http address of the csemonitor server
@@ -43,127 +27,3 @@ type CseCollectorConfig struct {
4327
func InitializeCseCollector(config *CseCollectorConfig, r metrics.Registry, app, version, service, env string) {
4428
go NewReporter(r, config.CseMonitorAddr, config.Header, config.TimeInterval, config.TLSConfig, app, version, service, env).Run()
4529
}
46-
47-
// NewCseCollector creates a new Collector Object
48-
func NewCseCollector(name string) metricCollector.MetricCollector {
49-
return &CseCollector{
50-
attempts: name + ".attempts",
51-
errors: name + ".errors",
52-
successes: name + ".successes",
53-
failures: name + ".failures",
54-
rejects: name + ".rejects",
55-
shortCircuits: name + ".shortCircuits",
56-
timeouts: name + ".timeouts",
57-
fallbackSuccesses: name + ".fallbackSuccesses",
58-
fallbackFailures: name + ".fallbackFailures",
59-
totalDuration: name + ".totalDuration",
60-
runDuration: name + ".runDuration",
61-
}
62-
}
63-
64-
func (c *CseCollector) incrementCounterMetric(prefix string) {
65-
count, ok := metrics.GetOrRegister(prefix, metrics.NewCounter).(metrics.Counter)
66-
if !ok {
67-
return
68-
}
69-
count.Inc(1)
70-
}
71-
72-
func (c *CseCollector) updateTimerMetric(prefix string, dur time.Duration) {
73-
count, ok := metrics.GetOrRegister(prefix, metrics.NewTimer).(metrics.Timer)
74-
if !ok {
75-
return
76-
}
77-
count.Update(dur)
78-
}
79-
80-
func (c *CseCollector) cleanMetric(prefix string) {
81-
count, ok := metrics.GetOrRegister(prefix, metrics.NewCounter).(metrics.Counter)
82-
if !ok {
83-
return
84-
}
85-
count.Clear()
86-
}
87-
88-
// IncrementAttempts function increments the number of calls to this circuit.
89-
// This registers as a counter
90-
func (c *CseCollector) IncrementAttempts() {
91-
c.incrementCounterMetric(c.attempts)
92-
}
93-
94-
// IncrementErrors function increments the number of unsuccessful attempts.
95-
// Attempts minus Errors will equal successes.
96-
// Errors are result from an attempt that is not a success.
97-
// This registers as a counter
98-
func (c *CseCollector) IncrementErrors() {
99-
c.incrementCounterMetric(c.errors)
100-
101-
}
102-
103-
// IncrementSuccesses function increments the number of requests that succeed.
104-
// This registers as a counter
105-
func (c *CseCollector) IncrementSuccesses() {
106-
c.incrementCounterMetric(c.successes)
107-
108-
}
109-
110-
// IncrementFailures function increments the number of requests that fail.
111-
// This registers as a counter
112-
func (c *CseCollector) IncrementFailures() {
113-
c.incrementCounterMetric(c.failures)
114-
}
115-
116-
// IncrementRejects function increments the number of requests that are rejected.
117-
// This registers as a counter
118-
func (c *CseCollector) IncrementRejects() {
119-
c.incrementCounterMetric(c.rejects)
120-
}
121-
122-
// IncrementShortCircuits function increments the number of requests that short circuited due to the circuit being open.
123-
// This registers as a counter
124-
func (c *CseCollector) IncrementShortCircuits() {
125-
c.incrementCounterMetric(c.shortCircuits)
126-
}
127-
128-
// IncrementTimeouts function increments the number of timeouts that occurred in the circuit breaker.
129-
// This registers as a counter
130-
func (c *CseCollector) IncrementTimeouts() {
131-
c.incrementCounterMetric(c.timeouts)
132-
}
133-
134-
// IncrementFallbackSuccesses function increments the number of successes that occurred during the execution of the fallback function.
135-
// This registers as a counter
136-
func (c *CseCollector) IncrementFallbackSuccesses() {
137-
c.incrementCounterMetric(c.fallbackSuccesses)
138-
}
139-
140-
// IncrementFallbackFailures function increments the number of failures that occurred during the execution of the fallback function.
141-
// This registers as a counter
142-
func (c *CseCollector) IncrementFallbackFailures() {
143-
c.incrementCounterMetric(c.fallbackFailures)
144-
}
145-
146-
// UpdateTotalDuration function updates the internal counter of how long we've run for.
147-
// This registers as a timer
148-
func (c *CseCollector) UpdateTotalDuration(timeSinceStart time.Duration) {
149-
c.updateTimerMetric(c.totalDuration, timeSinceStart)
150-
}
151-
152-
// UpdateRunDuration function updates the internal counter of how long the last run took.
153-
// This registers as a timer
154-
func (c *CseCollector) UpdateRunDuration(runDuration time.Duration) {
155-
c.updateTimerMetric(c.runDuration, runDuration)
156-
}
157-
158-
// Reset function is a noop operation in this collector.
159-
func (c *CseCollector) Reset() {
160-
c.cleanMetric(c.attempts)
161-
c.cleanMetric(c.failures)
162-
c.cleanMetric(c.successes)
163-
c.cleanMetric(c.shortCircuits)
164-
c.cleanMetric(c.errors)
165-
c.cleanMetric(c.rejects)
166-
c.cleanMetric(c.timeouts)
167-
c.cleanMetric(c.fallbackSuccesses)
168-
c.cleanMetric(c.fallbackFailures)
169-
}

cse_collector_test.go

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

csemonitor_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/go-chassis/go-chassis/core/lager"
1515
"github.com/rcrowley/go-metrics"
1616
"github.com/stretchr/testify/assert"
17+
18+
chassisMetrics "github.com/go-chassis/go-chassis/metrics"
1719
)
1820

1921
var globalConf = `
@@ -69,7 +71,7 @@ func TestCseMonitor2(t *testing.T) {
6971
initEnv()
7072
assert := assert.New(t)
7173
reporter := NewReporter(metrics.DefaultRegistry, "127.0.0.1:8080", http.Header{"Content-Type": []string{"application/json"}}, time.Second, &tls.Config{}, "default", "0.0.1", "Server", "")
72-
metricCollector := NewCseCollector("source.Provider.Microservice.SchemaID.OperationId")
74+
metricCollector := chassisMetrics.NewCseCollector("source.Provider.Microservice.SchemaID.OperationId")
7375
config.SelfServiceName = "testService"
7476

7577
metricCollector.IncrementAttempts()

register.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/go-chassis/go-chassis/core/config"
88
"github.com/go-chassis/go-chassis/core/lager"
99
chassisMetrics "github.com/go-chassis/go-chassis/metrics"
10-
"github.com/go-chassis/go-chassis/third_party/forked/afex/hystrix-go/hystrix/metric_collector"
1110
"github.com/rcrowley/go-metrics"
1211
)
1312

@@ -17,7 +16,6 @@ func init() {
1716

1817
//reportMetricsToCSEDashboard use go-metrics to send metrics to cse dashboard
1918
func reportMetricsToCSEDashboard(r metrics.Registry) error {
20-
metricCollector.Registry.Register(NewCseCollector)
2119

2220
monitorServerURL, err := getMonitorEndpoint()
2321
if err != nil {

0 commit comments

Comments
 (0)