github.com/cayleygraph/cayley@v0.7.7/graph/kv/metrics.go (about)

     1  package kv
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/hidal-go/hidalgo/kv"
     7  	"github.com/prometheus/client_golang/prometheus"
     8  	"github.com/prometheus/client_golang/prometheus/promauto"
     9  )
    10  
    11  var (
    12  	mApplyBatch = promauto.NewHistogram(prometheus.HistogramOpts{
    13  		Name: "cayley_apply_deltas_batch",
    14  		Help: "Number of quads in a buffer for ApplyDeltas or WriteQuads.",
    15  	})
    16  	mApplySeconds = promauto.NewHistogram(prometheus.HistogramOpts{
    17  		Name: "cayley_apply_deltas_seconds",
    18  		Help: "Time to write a buffer in ApplyDeltas or WriteQuads.",
    19  	})
    20  
    21  	mNodesNew = promauto.NewCounter(prometheus.CounterOpts{
    22  		Name: "cayley_kv_nodes_new_count",
    23  		Help: "Number new nodes created.",
    24  	})
    25  	mNodesUpd = promauto.NewCounter(prometheus.CounterOpts{
    26  		Name: "cayley_kv_nodes_upd_count",
    27  		Help: "Number of node refcount updates.",
    28  	})
    29  	mNodesDel = promauto.NewCounter(prometheus.CounterOpts{
    30  		Name: "cayley_kv_nodes_del_count",
    31  		Help: "Number of node deleted.",
    32  	})
    33  
    34  	mQuadsBloomHit = promauto.NewCounter(prometheus.CounterOpts{
    35  		Name: "cayley_kv_quads_bloom_hits",
    36  		Help: "Number of times the quad bloom filter returned a negative result.",
    37  	})
    38  	mQuadsBloomMiss = promauto.NewCounter(prometheus.CounterOpts{
    39  		Name: "cayley_kv_quads_bloom_miss",
    40  		Help: "Number of times the quad bloom filter returned a positive result.",
    41  	})
    42  
    43  	mPrimitiveFetch = promauto.NewCounter(prometheus.CounterOpts{
    44  		Name: "cayley_kv_primitive_fetch",
    45  		Help: "Number of primitives fetched from KV.",
    46  	})
    47  	mPrimitiveFetchMiss = promauto.NewCounter(prometheus.CounterOpts{
    48  		Name: "cayley_kv_primitive_fetch_miss",
    49  		Help: "Number of primitives that were not found in KV.",
    50  	})
    51  	mPrimitiveAppend = promauto.NewCounter(prometheus.CounterOpts{
    52  		Name: "cayley_kv_primitive_append",
    53  		Help: "Number of primitives appended to log.",
    54  	})
    55  
    56  	mIndexWriteBufferEntries = promauto.NewGaugeVec(prometheus.GaugeOpts{
    57  		Name: "cayley_kv_index_buffer_entries",
    58  		Help: "Number of entries in the index write buffer.",
    59  	}, []string{"index"})
    60  	mIndexWriteBufferFlushBatch = promauto.NewHistogramVec(prometheus.HistogramOpts{
    61  		Name: "cayley_kv_index_buffer_flush_batch",
    62  		Help: "Number of entries in the batch for flushing index entries.",
    63  	}, []string{"index"})
    64  	mIndexEntrySizeBytes = promauto.NewHistogramVec(prometheus.HistogramOpts{
    65  		Name: "cayley_kv_index_entry_size_bytes",
    66  		Help: "Size of a single index entry.",
    67  	}, []string{"index"})
    68  
    69  	mKVGet = promauto.NewCounter(prometheus.CounterOpts{
    70  		Name: "cayley_kv_get_count",
    71  		Help: "Number of get KV calls.",
    72  	})
    73  	mKVGetMiss = promauto.NewCounter(prometheus.CounterOpts{
    74  		Name: "cayley_kv_get_miss",
    75  		Help: "Number of get KV calls that found no value.",
    76  	})
    77  	mKVGetSize = promauto.NewHistogram(prometheus.HistogramOpts{
    78  		Name: "cayley_kv_get_size",
    79  		Help: "Size of values returned from KV.",
    80  	})
    81  	mKVPut = promauto.NewCounter(prometheus.CounterOpts{
    82  		Name: "cayley_kv_put_count",
    83  		Help: "Number of put KV calls.",
    84  	})
    85  	mKVPutSize = promauto.NewHistogram(prometheus.HistogramOpts{
    86  		Name: "cayley_kv_put_size",
    87  		Help: "Size of values put to KV.",
    88  	})
    89  	mKVDel = promauto.NewCounter(prometheus.CounterOpts{
    90  		Name: "cayley_kv_del_count",
    91  		Help: "Number of del KV calls.",
    92  	})
    93  	mKVScan = promauto.NewCounter(prometheus.CounterOpts{
    94  		Name: "cayley_kv_scan_count",
    95  		Help: "Number of scan KV calls.",
    96  	})
    97  	mKVCommit = promauto.NewCounter(prometheus.CounterOpts{
    98  		Name: "cayley_kv_commit",
    99  		Help: "Number of KV commits.",
   100  	})
   101  	mKVCommitSeconds = promauto.NewHistogram(prometheus.HistogramOpts{
   102  		Name: "cayley_kv_commit_seconds",
   103  		Help: "Time to commit to KV.",
   104  	})
   105  	mKVRollback = promauto.NewCounter(prometheus.CounterOpts{
   106  		Name: "cayley_kv_rollback",
   107  		Help: "Number of KV rollbacks.",
   108  	})
   109  )
   110  
   111  func wrapTx(tx kv.Tx) kv.Tx {
   112  	return &mTx{tx: tx}
   113  }
   114  
   115  type mTx struct {
   116  	tx   kv.Tx
   117  	done bool
   118  }
   119  
   120  func (tx *mTx) Commit(ctx context.Context) error {
   121  	if !tx.done {
   122  		tx.done = true
   123  		mKVCommit.Inc()
   124  		defer prometheus.NewTimer(mKVCommitSeconds).ObserveDuration()
   125  	}
   126  	return tx.tx.Commit(ctx)
   127  }
   128  
   129  func (tx *mTx) Close() error {
   130  	if !tx.done {
   131  		tx.done = true
   132  		mKVRollback.Inc()
   133  	}
   134  	return tx.tx.Close()
   135  }
   136  
   137  func (tx *mTx) Get(ctx context.Context, key kv.Key) (kv.Value, error) {
   138  	mKVGet.Inc()
   139  	val, err := tx.tx.Get(ctx, key)
   140  	if err == kv.ErrNotFound {
   141  		mKVGetMiss.Inc()
   142  	} else if err == nil {
   143  		mKVGetSize.Observe(float64(len(val)))
   144  	}
   145  	return val, err
   146  }
   147  
   148  func (tx *mTx) GetBatch(ctx context.Context, keys []kv.Key) ([]kv.Value, error) {
   149  	mKVGet.Add(float64(len(keys)))
   150  	vals, err := tx.tx.GetBatch(ctx, keys)
   151  	for _, v := range vals {
   152  		if v == nil {
   153  			mKVGetMiss.Inc()
   154  		} else {
   155  			mKVGetSize.Observe(float64(len(v)))
   156  		}
   157  	}
   158  	return vals, err
   159  }
   160  
   161  func (tx *mTx) Put(k kv.Key, v kv.Value) error {
   162  	mKVPut.Inc()
   163  	mKVPutSize.Observe(float64(len(v)))
   164  	return tx.tx.Put(k, v)
   165  }
   166  
   167  func (tx *mTx) Del(k kv.Key) error {
   168  	mKVDel.Inc()
   169  	return tx.tx.Del(k)
   170  }
   171  
   172  func (tx *mTx) Scan(pref kv.Key) kv.Iterator {
   173  	mKVScan.Inc()
   174  	return tx.tx.Scan(pref)
   175  }