From c8023a9c8d2c45ac03a53a62daaf218e44e399fc Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Tue, 13 Jun 2023 16:15:59 +0300 Subject: [PATCH] [#424] morphcache: Use labels for method duration Signed-off-by: Dmitrii Stepanov --- pkg/metrics/morphcache.go | 58 +++++++++----------------------------- pkg/morph/client/nns.go | 4 +-- pkg/morph/client/notary.go | 2 +- 3 files changed, 16 insertions(+), 48 deletions(-) diff --git a/pkg/metrics/morphcache.go b/pkg/metrics/morphcache.go index 000ec0d802..9cf2c8a0c4 100644 --- a/pkg/metrics/morphcache.go +++ b/pkg/metrics/morphcache.go @@ -1,7 +1,6 @@ package metrics import ( - "fmt" "strconv" "time" @@ -12,66 +11,35 @@ import ( const ( mcSubsystem = "morphcache" mcSuccess = "success" + mcMethod = "method" ) type MorphCacheMetrics interface { - AddNNSContractHashDuration(success bool, d time.Duration) - - AddGroupKeyDuration(success bool, d time.Duration) - - AddTxHeightDuration(success bool, d time.Duration) + AddMethodDuration(method string, success bool, d time.Duration) } type morphCacheMetrics struct { - // Duration of processing get nns contract hash request - nnsContractHashDuration *prometheus.HistogramVec - - // Duration of processing get group key request - groupKeyDuration *prometheus.HistogramVec - - // Duration of processing get tx height request - txHeightDuration *prometheus.HistogramVec + methodDuration *prometheus.HistogramVec } var _ MorphCacheMetrics = (*morphCacheMetrics)(nil) func newMorphCacheMetrics() *morphCacheMetrics { return &morphCacheMetrics{ - nnsContractHashDuration: newMCMethodDurationCounter("nns_contract_hash"), - groupKeyDuration: newMCMethodDurationCounter("group_key"), - txHeightDuration: newMCMethodDurationCounter("tx_height"), + methodDuration: metrics.NewHistogramVec(prometheus.HistogramOpts{ + Namespace: namespace, + Subsystem: mcSubsystem, + Name: "request_duration_seconds", + Help: "Morph cache request process duration", + }, []string{mcSuccess, mcMethod}), } } -func (m *morphCacheMetrics) AddNNSContractHashDuration(success bool, d time.Duration) { - m.nnsContractHashDuration.With( +func (m *morphCacheMetrics) AddMethodDuration(method string, success bool, d time.Duration) { + m.methodDuration.With( prometheus.Labels{ mcSuccess: strconv.FormatBool(success), + mcMethod: method, }, - ).Observe(float64(d)) -} - -func (m *morphCacheMetrics) AddGroupKeyDuration(success bool, d time.Duration) { - m.groupKeyDuration.With( - prometheus.Labels{ - mcSuccess: strconv.FormatBool(success), - }, - ).Observe(float64(d)) -} - -func (m *morphCacheMetrics) AddTxHeightDuration(success bool, d time.Duration) { - m.txHeightDuration.With( - prometheus.Labels{ - mcSuccess: strconv.FormatBool(success), - }, - ).Observe(float64(d)) -} - -func newMCMethodDurationCounter(method string) *prometheus.HistogramVec { - return metrics.NewHistogramVec(prometheus.HistogramOpts{ - Namespace: namespace, - Subsystem: mcSubsystem, - Name: fmt.Sprintf("%s_req_duration_seconds", method), - Help: fmt.Sprintf("Accumulated %s request process duration", method), - }, []string{mcSuccess}) + ).Observe(d.Seconds()) } diff --git a/pkg/morph/client/nns.go b/pkg/morph/client/nns.go index d8d798b076..53dbe180e7 100644 --- a/pkg/morph/client/nns.go +++ b/pkg/morph/client/nns.go @@ -86,7 +86,7 @@ func (c *Client) NNSHash() (util.Uint160, error) { startedAt := time.Now() defer func() { - c.cache.metrics.AddNNSContractHashDuration(success, time.Since(startedAt)) + c.cache.metrics.AddMethodDuration("NNSContractHash", success, time.Since(startedAt)) }() nnsHash := c.cache.nns() @@ -233,7 +233,7 @@ func (c *Client) contractGroupKey() (*keys.PublicKey, error) { success := false startedAt := time.Now() defer func() { - c.cache.metrics.AddGroupKeyDuration(success, time.Since(startedAt)) + c.cache.metrics.AddMethodDuration("GroupKey", success, time.Since(startedAt)) }() if gKey := c.cache.groupKey(); gKey != nil { diff --git a/pkg/morph/client/notary.go b/pkg/morph/client/notary.go index e567b6df0b..17644361a0 100644 --- a/pkg/morph/client/notary.go +++ b/pkg/morph/client/notary.go @@ -800,7 +800,7 @@ func (c *Client) getTransactionHeight(h util.Uint256) (uint32, error) { success := false startedAt := time.Now() defer func() { - c.cache.metrics.AddTxHeightDuration(success, time.Since(startedAt)) + c.cache.metrics.AddMethodDuration("TxHeight", success, time.Since(startedAt)) }() if rh, ok := c.cache.txHeights.Get(h); ok {