github.com/coocood/badger@v1.5.1-0.20200528065104-c02ac3616d04/y/metrics.go (about)

     1  /*
     2   * Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *    http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package y
    18  
    19  import (
    20  	"github.com/prometheus/client_golang/prometheus"
    21  )
    22  
    23  const (
    24  	namespace  = "badger"
    25  	labelPath  = "path"
    26  	labelLevel = "target_level"
    27  )
    28  
    29  var (
    30  	// LSMSize has size of the LSM in bytes
    31  	LSMSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    32  		Namespace: namespace,
    33  		Name:      "lsm_size",
    34  	}, []string{labelPath})
    35  	// VlogSize has size of the value log in bytes
    36  	VlogSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    37  		Namespace: namespace,
    38  		Name:      "vlog_size",
    39  	}, []string{labelPath})
    40  
    41  	// These are cumulative
    42  
    43  	// NumReads has cumulative number of reads
    44  	NumReads = prometheus.NewCounterVec(prometheus.CounterOpts{
    45  		Namespace: namespace,
    46  		Name:      "num_reads",
    47  	}, []string{labelPath})
    48  	// NumWrites has cumulative number of writes
    49  	NumWrites = prometheus.NewCounterVec(prometheus.CounterOpts{
    50  		Namespace: namespace,
    51  		Name:      "num_writes",
    52  	}, []string{labelPath})
    53  	// NumBytesRead has cumulative number of bytes read
    54  	NumBytesRead = prometheus.NewCounterVec(prometheus.CounterOpts{
    55  		Namespace: namespace,
    56  		Name:      "num_bytes_read",
    57  	}, []string{labelPath})
    58  	// NumVLogBytesWritten has cumulative number of bytes written
    59  	NumVLogBytesWritten = prometheus.NewCounterVec(prometheus.CounterOpts{
    60  		Namespace: namespace,
    61  		Name:      "num_bytes_written",
    62  	}, []string{labelPath})
    63  	// NumGets is number of gets
    64  	NumGets = prometheus.NewCounterVec(prometheus.CounterOpts{
    65  		Namespace: namespace,
    66  		Name:      "num_gets",
    67  	}, []string{labelPath})
    68  	// NumPuts is number of puts
    69  	NumPuts = prometheus.NewCounterVec(prometheus.CounterOpts{
    70  		Namespace: namespace,
    71  		Name:      "num_puts",
    72  	}, []string{labelPath})
    73  	// NumMemtableGets is number of memtable gets
    74  	NumMemtableGets = prometheus.NewCounterVec(prometheus.CounterOpts{
    75  		Namespace: namespace,
    76  		Name:      "num_memtable_gets",
    77  	}, []string{labelPath})
    78  
    79  	// Level statistics
    80  
    81  	// NumCompactionBytesWrite has cumulative size of keys read during compaction.
    82  	NumCompactionBytesWrite = prometheus.NewCounterVec(prometheus.CounterOpts{
    83  		Namespace: namespace,
    84  		Name:      "num_compaction_bytes_write",
    85  	}, []string{labelPath, labelLevel})
    86  	// NumCompactionBytesRead has cumulative size of keys write during compaction.
    87  	NumCompactionBytesRead = prometheus.NewCounterVec(prometheus.CounterOpts{
    88  		Namespace: namespace,
    89  		Name:      "num_compaction_bytes_read",
    90  	}, []string{labelPath, labelLevel})
    91  	// NumCompactionBytesRead has cumulative size of discarded keys after compaction.
    92  	NumCompactionBytesDiscard = prometheus.NewCounterVec(prometheus.CounterOpts{
    93  		Namespace: namespace,
    94  		Name:      "num_compaction_bytes_discard",
    95  	}, []string{labelPath, labelLevel})
    96  	// NumCompactionKeysWrite has cumulative count of keys write during compaction.
    97  	NumCompactionKeysWrite = prometheus.NewCounterVec(prometheus.CounterOpts{
    98  		Namespace: namespace,
    99  		Name:      "num_compaction_keys_write",
   100  	}, []string{labelPath, labelLevel})
   101  	// NumCompactionKeysRead has cumulative count of keys read during compaction.
   102  	NumCompactionKeysRead = prometheus.NewCounterVec(prometheus.CounterOpts{
   103  		Namespace: namespace,
   104  		Name:      "num_compaction_keys_read",
   105  	}, []string{labelPath, labelLevel})
   106  	// NumCompactionKeysDiscard has cumulative count of discarded keys after compaction.
   107  	NumCompactionKeysDiscard = prometheus.NewCounterVec(prometheus.CounterOpts{
   108  		Namespace: namespace,
   109  		Name:      "num_compaction_keys_discard",
   110  	}, []string{labelPath, labelLevel})
   111  	// NumLSMGets is number of LMS gets
   112  	NumLSMGets = prometheus.NewCounterVec(prometheus.CounterOpts{
   113  		Namespace: namespace,
   114  		Name:      "num_lsm_gets",
   115  	}, []string{labelPath, labelLevel})
   116  	// NumLSMBloomFalsePositive is number of LMS bloom hits
   117  	NumLSMBloomFalsePositive = prometheus.NewCounterVec(prometheus.CounterOpts{
   118  		Namespace: namespace,
   119  		Name:      "num_lsm_bloom_false_positive",
   120  	}, []string{labelPath, labelLevel})
   121  
   122  	// Histograms
   123  
   124  	VlogSyncDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
   125  		Namespace: namespace,
   126  		Name:      "vlog_sync_duration",
   127  		Buckets:   prometheus.ExponentialBuckets(0.001, 1.5, 20),
   128  	}, []string{labelPath})
   129  
   130  	WriteLSMDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
   131  		Namespace: namespace,
   132  		Name:      "write_lsm_duration",
   133  		Buckets:   prometheus.ExponentialBuckets(0.0003, 1.5, 20),
   134  	}, []string{labelPath})
   135  
   136  	LSMGetDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
   137  		Namespace: namespace,
   138  		Name:      "lsm_get_duration",
   139  		Buckets:   prometheus.ExponentialBuckets(0.0003, 1.5, 20),
   140  	}, []string{labelPath})
   141  
   142  	LSMMultiGetDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
   143  		Namespace: namespace,
   144  		Name:      "lsm_multi_get_duration",
   145  		Buckets:   prometheus.ExponentialBuckets(0.0003, 1.5, 20),
   146  	}, []string{labelPath})
   147  )
   148  
   149  type MetricsSet struct {
   150  	path                string
   151  	LSMSize             prometheus.Gauge
   152  	VlogSize            prometheus.Gauge
   153  	NumReads            prometheus.Counter
   154  	NumWrites           prometheus.Counter
   155  	NumBytesRead        prometheus.Counter
   156  	NumVLogBytesWritten prometheus.Counter
   157  	NumGets             prometheus.Counter
   158  	NumPuts             prometheus.Counter
   159  	NumMemtableGets     prometheus.Counter
   160  	VlogSyncDuration    prometheus.Observer
   161  	WriteLSMDuration    prometheus.Observer
   162  	LSMGetDuration      prometheus.Observer
   163  	LSMMultiGetDuration prometheus.Observer
   164  }
   165  
   166  func NewMetricSet(path string) *MetricsSet {
   167  	return &MetricsSet{
   168  		path:                path,
   169  		LSMSize:             LSMSize.WithLabelValues(path),
   170  		VlogSize:            VlogSize.WithLabelValues(path),
   171  		NumReads:            NumReads.WithLabelValues(path),
   172  		NumWrites:           NumWrites.WithLabelValues(path),
   173  		NumBytesRead:        NumBytesRead.WithLabelValues(path),
   174  		NumVLogBytesWritten: NumVLogBytesWritten.WithLabelValues(path),
   175  
   176  		NumGets:             NumGets.WithLabelValues(path),
   177  		NumPuts:             NumPuts.WithLabelValues(path),
   178  		NumMemtableGets:     NumMemtableGets.WithLabelValues(path),
   179  		VlogSyncDuration:    VlogSyncDuration.WithLabelValues(path),
   180  		WriteLSMDuration:    WriteLSMDuration.WithLabelValues(path),
   181  		LSMGetDuration:      LSMGetDuration.WithLabelValues(path),
   182  		LSMMultiGetDuration: LSMMultiGetDuration.WithLabelValues(path),
   183  	}
   184  }
   185  
   186  func (m *MetricsSet) NewLevelMetricsSet(levelLabel string) *LevelMetricsSet {
   187  	return &LevelMetricsSet{
   188  		MetricsSet:                m,
   189  		NumLSMGets:                NumLSMGets.WithLabelValues(m.path, levelLabel),
   190  		NumLSMBloomFalsePositive:  NumLSMBloomFalsePositive.WithLabelValues(m.path, levelLabel),
   191  		NumCompactionKeysRead:     NumCompactionKeysRead.WithLabelValues(m.path, levelLabel),
   192  		NumCompactionBytesRead:    NumCompactionBytesRead.WithLabelValues(m.path, levelLabel),
   193  		NumCompactionKeysWrite:    NumCompactionKeysWrite.WithLabelValues(m.path, levelLabel),
   194  		NumCompactionBytesWrite:   NumCompactionBytesWrite.WithLabelValues(m.path, levelLabel),
   195  		NumCompactionKeysDiscard:  NumCompactionKeysDiscard.WithLabelValues(m.path, levelLabel),
   196  		NumCompactionBytesDiscard: NumCompactionBytesDiscard.WithLabelValues(m.path, levelLabel),
   197  	}
   198  }
   199  
   200  type LevelMetricsSet struct {
   201  	*MetricsSet
   202  	NumCompactionKeysRead     prometheus.Counter
   203  	NumCompactionBytesRead    prometheus.Counter
   204  	NumCompactionKeysWrite    prometheus.Counter
   205  	NumCompactionBytesWrite   prometheus.Counter
   206  	NumCompactionKeysDiscard  prometheus.Counter
   207  	NumCompactionBytesDiscard prometheus.Counter
   208  	NumLSMGets                prometheus.Counter
   209  	NumLSMBloomFalsePositive  prometheus.Counter
   210  }
   211  
   212  type CompactionStats struct {
   213  	KeysRead     int
   214  	BytesRead    int
   215  	KeysWrite    int
   216  	BytesWrite   int
   217  	KeysDiscard  int
   218  	BytesDiscard int
   219  }
   220  
   221  func (m *LevelMetricsSet) UpdateCompactionStats(stats *CompactionStats) {
   222  	m.NumCompactionKeysRead.Add(float64(stats.KeysRead))
   223  	m.NumCompactionBytesRead.Add(float64(stats.BytesRead))
   224  
   225  	m.NumCompactionKeysWrite.Add(float64(stats.KeysWrite))
   226  	m.NumCompactionBytesWrite.Add(float64(stats.BytesWrite))
   227  
   228  	m.NumCompactionKeysDiscard.Add(float64(stats.KeysDiscard))
   229  	m.NumCompactionBytesDiscard.Add(float64(stats.BytesDiscard))
   230  }
   231  
   232  // These variables are global and have cumulative values for all kv stores.
   233  func init() {
   234  	prometheus.MustRegister(LSMSize)
   235  	prometheus.MustRegister(VlogSize)
   236  	prometheus.MustRegister(NumReads)
   237  	prometheus.MustRegister(NumWrites)
   238  	prometheus.MustRegister(NumBytesRead)
   239  	prometheus.MustRegister(NumVLogBytesWritten)
   240  	prometheus.MustRegister(NumLSMGets)
   241  	prometheus.MustRegister(NumLSMBloomFalsePositive)
   242  	prometheus.MustRegister(NumGets)
   243  	prometheus.MustRegister(NumPuts)
   244  	prometheus.MustRegister(NumMemtableGets)
   245  	prometheus.MustRegister(VlogSyncDuration)
   246  	prometheus.MustRegister(WriteLSMDuration)
   247  	prometheus.MustRegister(LSMGetDuration)
   248  	prometheus.MustRegister(LSMMultiGetDuration)
   249  	prometheus.MustRegister(NumCompactionBytesWrite)
   250  	prometheus.MustRegister(NumCompactionBytesRead)
   251  	prometheus.MustRegister(NumCompactionBytesDiscard)
   252  	prometheus.MustRegister(NumCompactionKeysRead)
   253  	prometheus.MustRegister(NumCompactionKeysWrite)
   254  	prometheus.MustRegister(NumCompactionKeysDiscard)
   255  }