[#424] morphcache: Use labels for method duration

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-06-13 16:15:59 +03:00
parent 85deb12f4d
commit c8023a9c8d
3 changed files with 16 additions and 48 deletions

View file

@ -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())
}

View file

@ -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 {

View file

@ -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 {