Skip to content
Snippets Groups Projects
Commit 2d0e07fb authored by Josh Brooks's avatar Josh Brooks
Browse files

Refactor metrics

parent 55f9bdf1
No related branches found
No related tags found
No related merge requests found
......@@ -206,7 +206,7 @@ func (h *Host) isExcludedMetricError(err string) bool {
// Sets the host metrics to an arbitrary value. Used for testing
// purposes only
func (h *Host) SetMetricsTesting(m *Metric, face interface{}) {
func (h *Host) SetMetricsTesting(errCounter uint64, face interface{}) {
// Ensure that this function is only run in testing environments
switch face.(type) {
case *testing.T, *testing.M, *testing.B:
......@@ -215,7 +215,7 @@ func (h *Host) SetMetricsTesting(m *Metric, face interface{}) {
panic("SetMetricsTesting() can only be used for testing.")
}
h.metrics = m
h.metrics.errCounter = &errCounter
}
......
......@@ -202,14 +202,14 @@ func TestHost_GetMetrics(t *testing.T) {
// Check that the metricCopy has the expected error count
metricCopy := host.GetMetrics()
if *metricCopy.ErrCounter != uint64(expectedCount) {
if *metricCopy.errCounter != uint64(expectedCount) {
t.Errorf("GetMetric() did not pull expected state."+
"\n\tExpected: %v"+
"\n\tReceived: %v", expectedCount, *metricCopy.ErrCounter)
"\n\tReceived: %v", expectedCount, *metricCopy.errCounter)
}
// Check that the original metric's state has been reset
if *host.metrics.ErrCounter != uint64(0) {
if *host.metrics.errCounter != uint64(0) {
t.Errorf("get call should reset state for metric")
}
......
......@@ -9,43 +9,45 @@
package connect
import "sync/atomic"
import (
"sync/atomic"
)
type Metric struct {
// Active count of non-excluded errors
// (ie errors we deem important)
ErrCounter *uint64
errCounter *uint64
}
// Constructor for a Metric object
func newMetric() *Metric {
errCounter := uint64(0)
return &Metric{
ErrCounter: &errCounter,
errCounter: &errCounter,
}
}
// Getter for ErrCounter
// Getter for errCounter
func (m *Metric) GetErrorCounter() uint64 {
return atomic.LoadUint64(m.ErrCounter)
return atomic.LoadUint64(m.errCounter)
}
// Returns a copy of Metric and resets internal state
func (m *Metric) get() *Metric {
metricCopy := m.deepCopy()
atomic.StoreUint64(m.ErrCounter, 0)
atomic.StoreUint64(m.errCounter, 0)
return metricCopy
}
// Increments the error counter in a thread-safe manner
func (m *Metric) incrementErrors() {
atomic.AddUint64(m.ErrCounter, 1)
atomic.AddUint64(m.errCounter, 1)
}
// deepCopy creates a copy of Metric.
func (m *Metric) deepCopy() *Metric {
newErrCounter := atomic.LoadUint64(m.ErrCounter)
newErrCounter := atomic.LoadUint64(m.errCounter)
return &Metric{
ErrCounter: &newErrCounter,
errCounter: &newErrCounter,
}
}
......@@ -18,7 +18,7 @@ func TestNewMetric(t *testing.T) {
expectedErrCnt := uint64(0)
expectedMetric := &Metric{
ErrCounter: &expectedErrCnt,
errCounter: &expectedErrCnt,
}
if !reflect.DeepEqual(expectedMetric, metric) {
......@@ -67,11 +67,11 @@ func TestMetric_IncrementErrors(t *testing.T) {
metric.incrementErrors()
}
if *metric.ErrCounter != uint64(expectedCount) {
if *metric.errCounter != uint64(expectedCount) {
t.Errorf("incrementErrors did not "+
"result in expected error count."+
"\n\tExpected: %v"+
"\n\tReceived: %v", expectedCount, *metric.ErrCounter)
"\n\tReceived: %v", expectedCount, *metric.errCounter)
}
}
......@@ -86,14 +86,14 @@ func TestMetric_Get(t *testing.T) {
// Check that the metricCopy has the expected error count
metricCopy := metric.get()
if *metricCopy.ErrCounter != uint64(expectedCount) {
if *metricCopy.errCounter != uint64(expectedCount) {
t.Errorf("get() did not pull expected state."+
"\n\tExpected: %v"+
"\n\tReceived: %v", expectedCount, *metricCopy.ErrCounter)
"\n\tReceived: %v", expectedCount, *metricCopy.errCounter)
}
// Check that the original metric's state has been reset
if *metric.ErrCounter != uint64(0) {
if *metric.errCounter != uint64(0) {
t.Errorf("get call should reset state for metric")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment