This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(567): metrics collectors & trackers
- Loading branch information
1 parent
a3f13d1
commit 470ab1e
Showing
12 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package metrics | ||
|
||
// Collector is an interface that is used to register metrics | ||
type Collector interface { | ||
RegisterGauge(name string) GaugeInterface | ||
RegisterHistogramVec(name string, labels ...string) HistogramVecInterface | ||
} | ||
|
||
// GaugeInterface is an interface that is used to track gauges of values | ||
type GaugeInterface interface { | ||
Set(value float64) | ||
} | ||
|
||
// HistogramVecInterface is an interface that is used to register histograms with labels | ||
type HistogramVecInterface interface { | ||
WithLabelValues(lvs ...string) HistogramInterface | ||
} | ||
|
||
// HistogramInterface is an interface that is used to track histograms of values | ||
type HistogramInterface interface { | ||
Observe(value float64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Package metrics provides a way to track metrics in the application. Functionality is strictly tailored to the needs of the package and is not meant to be a general purpose metrics library. | ||
*/ | ||
package metrics | ||
|
||
import "time" | ||
|
||
// Metrics is a struct that contains all the metrics that are used to track in the package | ||
type Metrics struct { | ||
collector Collector | ||
Stats Stats | ||
verifyMerkleRoots HistogramVecInterface | ||
recordTransaction HistogramVecInterface | ||
} | ||
|
||
// NewMetrics is a constructor for the Metrics struct | ||
func NewMetrics(collector Collector) *Metrics { | ||
return &Metrics{ | ||
collector: collector, | ||
Stats: registerStats(collector), | ||
verifyMerkleRoots: collector.RegisterHistogramVec(verifyMerkleRootsHistogramName, "classification"), | ||
recordTransaction: collector.RegisterHistogramVec(recordTransactionHistogramName, "classification", "strategy"), | ||
} | ||
} | ||
|
||
// EndWithClassification is a function returned by Track* methods that should be called when the tracked operation is finished | ||
type EndWithClassification func(success bool) | ||
|
||
// TrackVerifyMerkleRoots is used to track the time it takes to verify merkle roots | ||
func (m *Metrics) TrackVerifyMerkleRoots() EndWithClassification { | ||
start := time.Now() | ||
return func(success bool) { | ||
m.verifyMerkleRoots.WithLabelValues(classify(success)).Observe(time.Since(start).Seconds()) | ||
} | ||
} | ||
|
||
// TrackRecordTransaction is used to track the time it takes to record a transaction | ||
func (m *Metrics) TrackRecordTransaction(strategyName string) EndWithClassification { | ||
start := time.Now() | ||
return func(success bool) { | ||
m.verifyMerkleRoots.WithLabelValues(classify(success), strategyName).Observe(time.Since(start).Seconds()) | ||
} | ||
} | ||
|
||
func classify(success bool) string { | ||
if success { | ||
return "success" | ||
} | ||
return "failure" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package metrics | ||
|
||
const domainPrefix = "bux_" | ||
|
||
const ( | ||
verifyMerkleRootsHistogramName = domainPrefix + "verify_merkle_roots_histogram" | ||
recordTransactionHistogramName = domainPrefix + "record_transaction_histogram" | ||
) | ||
|
||
const xpubGaugeName = domainPrefix + "xpub_gauge" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package metrics | ||
|
||
// Stats is a struct that contains all the gauges that are used to track the calculated stats of the application | ||
type Stats struct { | ||
XPub GaugeInterface | ||
} | ||
|
||
func registerStats(collector Collector) Stats { | ||
return Stats{ | ||
XPub: collector.RegisterGauge(xpubGaugeName), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters