From 4d728934566b259aba167f86237b00143e0f7086 Mon Sep 17 00:00:00 2001 From: josh <josh@elixxir.io> Date: Mon, 22 Mar 2021 10:35:05 -0700 Subject: [PATCH] Implement Metric object --- connect/metrics.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 connect/metrics.go diff --git a/connect/metrics.go b/connect/metrics.go new file mode 100644 index 0000000..ef11a05 --- /dev/null +++ b/connect/metrics.go @@ -0,0 +1,46 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +// Contains functionality for metric tracking on sending + +package connect + +import "sync/atomic" + +type Metric struct { + // Active count of non-excluded errors + // (ie errors we deem important) + errorCounter *uint64 +} + +// Constructor for a Metric object +func NewMetric() *Metric { + errCounter := uint64(0) + return &Metric{ + errorCounter: &errCounter, + } +} + +// Returns a copy of Metric and resets internal state +func (m *Metric) Get() *Metric { + metricCopy := m.deepCopy() + atomic.StoreUint64(m.errorCounter, 0) + return metricCopy +} + +// Increments the error counter in a thread-safe manner +func (m *Metric) IncrementErrors() { + atomic.AddUint64(m.errorCounter, 1) +} + +// DeepCopy creates a copy of Metric. +func (m *Metric) deepCopy() *Metric { + newErrCounter := atomic.LoadUint64(m.errorCounter) + return &Metric{ + errorCounter: &newErrCounter, + } +} -- GitLab