Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
comms
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xx network
comms
Commits
2d0e07fb
Commit
2d0e07fb
authored
Mar 30, 2021
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Refactor metrics
parent
55f9bdf1
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
connect/host.go
+2
-2
2 additions, 2 deletions
connect/host.go
connect/host_test.go
+3
-3
3 additions, 3 deletions
connect/host_test.go
connect/metrics.go
+11
-9
11 additions, 9 deletions
connect/metrics.go
connect/metrics_test.go
+6
-6
6 additions, 6 deletions
connect/metrics_test.go
with
22 additions
and
20 deletions
connect/host.go
+
2
−
2
View file @
2d0e07fb
...
...
@@ -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
}
...
...
This diff is collapsed.
Click to expand it.
connect/host_test.go
+
3
−
3
View file @
2d0e07fb
...
...
@@ -202,14 +202,14 @@ func TestHost_GetMetrics(t *testing.T) {
// Check that the metricCopy has the expected error count
metricCopy
:=
host
.
GetMetrics
()
if
*
metricCopy
.
E
rrCounter
!=
uint64
(
expectedCount
)
{
if
*
metricCopy
.
e
rrCounter
!=
uint64
(
expectedCount
)
{
t
.
Errorf
(
"GetMetric() did not pull expected state."
+
"
\n\t
Expected: %v"
+
"
\n\t
Received: %v"
,
expectedCount
,
*
metricCopy
.
E
rrCounter
)
"
\n\t
Received: %v"
,
expectedCount
,
*
metricCopy
.
e
rrCounter
)
}
// Check that the original metric's state has been reset
if
*
host
.
metrics
.
E
rrCounter
!=
uint64
(
0
)
{
if
*
host
.
metrics
.
e
rrCounter
!=
uint64
(
0
)
{
t
.
Errorf
(
"get call should reset state for metric"
)
}
...
...
This diff is collapsed.
Click to expand it.
connect/metrics.go
+
11
−
9
View file @
2d0e07fb
...
...
@@ -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)
E
rrCounter
*
uint64
e
rrCounter
*
uint64
}
// Constructor for a Metric object
func
newMetric
()
*
Metric
{
errCounter
:=
uint64
(
0
)
return
&
Metric
{
E
rrCounter
:
&
errCounter
,
e
rrCounter
:
&
errCounter
,
}
}
// Getter for
E
rrCounter
// Getter for
e
rrCounter
func
(
m
*
Metric
)
GetErrorCounter
()
uint64
{
return
atomic
.
LoadUint64
(
m
.
E
rrCounter
)
return
atomic
.
LoadUint64
(
m
.
e
rrCounter
)
}
// Returns a copy of Metric and resets internal state
func
(
m
*
Metric
)
get
()
*
Metric
{
metricCopy
:=
m
.
deepCopy
()
atomic
.
StoreUint64
(
m
.
E
rrCounter
,
0
)
atomic
.
StoreUint64
(
m
.
e
rrCounter
,
0
)
return
metricCopy
}
// Increments the error counter in a thread-safe manner
func
(
m
*
Metric
)
incrementErrors
()
{
atomic
.
AddUint64
(
m
.
E
rrCounter
,
1
)
atomic
.
AddUint64
(
m
.
e
rrCounter
,
1
)
}
// deepCopy creates a copy of Metric.
func
(
m
*
Metric
)
deepCopy
()
*
Metric
{
newErrCounter
:=
atomic
.
LoadUint64
(
m
.
E
rrCounter
)
newErrCounter
:=
atomic
.
LoadUint64
(
m
.
e
rrCounter
)
return
&
Metric
{
E
rrCounter
:
&
newErrCounter
,
e
rrCounter
:
&
newErrCounter
,
}
}
This diff is collapsed.
Click to expand it.
connect/metrics_test.go
+
6
−
6
View file @
2d0e07fb
...
...
@@ -18,7 +18,7 @@ func TestNewMetric(t *testing.T) {
expectedErrCnt
:=
uint64
(
0
)
expectedMetric
:=
&
Metric
{
E
rrCounter
:
&
expectedErrCnt
,
e
rrCounter
:
&
expectedErrCnt
,
}
if
!
reflect
.
DeepEqual
(
expectedMetric
,
metric
)
{
...
...
@@ -67,11 +67,11 @@ func TestMetric_IncrementErrors(t *testing.T) {
metric
.
incrementErrors
()
}
if
*
metric
.
E
rrCounter
!=
uint64
(
expectedCount
)
{
if
*
metric
.
e
rrCounter
!=
uint64
(
expectedCount
)
{
t
.
Errorf
(
"incrementErrors did not "
+
"result in expected error count."
+
"
\n\t
Expected: %v"
+
"
\n\t
Received: %v"
,
expectedCount
,
*
metric
.
E
rrCounter
)
"
\n\t
Received: %v"
,
expectedCount
,
*
metric
.
e
rrCounter
)
}
}
...
...
@@ -86,14 +86,14 @@ func TestMetric_Get(t *testing.T) {
// Check that the metricCopy has the expected error count
metricCopy
:=
metric
.
get
()
if
*
metricCopy
.
E
rrCounter
!=
uint64
(
expectedCount
)
{
if
*
metricCopy
.
e
rrCounter
!=
uint64
(
expectedCount
)
{
t
.
Errorf
(
"get() did not pull expected state."
+
"
\n\t
Expected: %v"
+
"
\n\t
Received: %v"
,
expectedCount
,
*
metricCopy
.
E
rrCounter
)
"
\n\t
Received: %v"
,
expectedCount
,
*
metricCopy
.
e
rrCounter
)
}
// Check that the original metric's state has been reset
if
*
metric
.
E
rrCounter
!=
uint64
(
0
)
{
if
*
metric
.
e
rrCounter
!=
uint64
(
0
)
{
t
.
Errorf
(
"get call should reset state for metric"
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment