github.com/GuanceCloud/cliutils@v1.1.21/diskcache/metric.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package diskcache
     7  
     8  import (
     9  	"github.com/GuanceCloud/cliutils/metrics"
    10  	"github.com/prometheus/client_golang/prometheus"
    11  )
    12  
    13  var (
    14  	droppedBatchVec,
    15  	droppedBytesVec,
    16  	rotateVec,
    17  	removeVec,
    18  	putVec,
    19  	getVec,
    20  	putBytesVec,
    21  	wakeupVec,
    22  	seekBackVec,
    23  	getBytesVec *prometheus.CounterVec
    24  
    25  	sizeVec,
    26  	openTimeVec,
    27  	lastCloseTimeVec,
    28  	capVec,
    29  	maxDataVec,
    30  	batchSizeVec,
    31  	datafilesVec *prometheus.GaugeVec
    32  
    33  	getLatencyVec,
    34  	putLatencyVec *prometheus.SummaryVec
    35  
    36  	ns = "diskcache"
    37  )
    38  
    39  func setupMetrics() {
    40  	getLatencyVec = prometheus.NewSummaryVec(
    41  		prometheus.SummaryOpts{
    42  			Namespace: ns,
    43  			Name:      "get_latency",
    44  			Help:      "Get() time cost(micro-second)",
    45  		},
    46  		[]string{"path"},
    47  	)
    48  
    49  	putLatencyVec = prometheus.NewSummaryVec(
    50  		prometheus.SummaryOpts{
    51  			Namespace: ns,
    52  			Name:      "put_latency",
    53  			Help:      "Put() time cost(micro-second)",
    54  		},
    55  		[]string{"path"},
    56  	)
    57  
    58  	droppedBytesVec = prometheus.NewCounterVec(
    59  		prometheus.CounterOpts{
    60  			Namespace: ns,
    61  			Name:      "dropped_bytes_total",
    62  			Help:      "Dropped bytes during Put() when capacity reached.",
    63  		},
    64  		[]string{"path"},
    65  	)
    66  
    67  	droppedBatchVec = prometheus.NewCounterVec(
    68  		prometheus.CounterOpts{
    69  			Namespace: ns,
    70  			Name:      "dropped_total",
    71  			Help:      "Dropped files during Put() when capacity reached.",
    72  		},
    73  		[]string{"path", "reason"},
    74  	)
    75  
    76  	rotateVec = prometheus.NewCounterVec(
    77  		prometheus.CounterOpts{
    78  			Namespace: ns,
    79  			Name:      "rotate_total",
    80  			Help:      "Cache rotate count, mean file rotate from data to data.0000xxx",
    81  		},
    82  		[]string{"path"},
    83  	)
    84  
    85  	removeVec = prometheus.NewCounterVec(
    86  		prometheus.CounterOpts{
    87  			Namespace: ns,
    88  			Name:      "remove_total",
    89  			Help:      "Removed file count, if some file read EOF, remove it from un-read list",
    90  		},
    91  		[]string{"path"},
    92  	)
    93  
    94  	putVec = prometheus.NewCounterVec(
    95  		prometheus.CounterOpts{
    96  			Namespace: ns,
    97  			Name:      "put_total",
    98  			Help:      "Cache Put() count",
    99  		},
   100  		[]string{"path"},
   101  	)
   102  
   103  	putBytesVec = prometheus.NewCounterVec(
   104  		prometheus.CounterOpts{
   105  			Namespace: ns,
   106  			Name:      "put_bytes_total",
   107  			Help:      "Cache Put() bytes count",
   108  		},
   109  		[]string{"path"},
   110  	)
   111  
   112  	getVec = prometheus.NewCounterVec(
   113  		prometheus.CounterOpts{
   114  			Namespace: ns,
   115  			Name:      "get_total",
   116  			Help:      "Cache Get() count",
   117  		},
   118  		[]string{"path"},
   119  	)
   120  
   121  	wakeupVec = prometheus.NewCounterVec(
   122  		prometheus.CounterOpts{
   123  			Namespace: ns,
   124  			Name:      "wakeup_total",
   125  			Help:      "Wakeup count on sleeping write file",
   126  		},
   127  		[]string{"path"},
   128  	)
   129  
   130  	seekBackVec = prometheus.NewCounterVec(
   131  		prometheus.CounterOpts{
   132  			Namespace: ns,
   133  			Name:      "seek_back_total",
   134  			Help:      "Seek back when Get() got any error",
   135  		},
   136  		[]string{"path"},
   137  	)
   138  
   139  	getBytesVec = prometheus.NewCounterVec(
   140  		prometheus.CounterOpts{
   141  			Namespace: ns,
   142  			Name:      "get_bytes_total",
   143  			Help:      "Cache Get() bytes count",
   144  		},
   145  		[]string{"path"},
   146  	)
   147  
   148  	capVec = prometheus.NewGaugeVec(
   149  		prometheus.GaugeOpts{
   150  			Namespace: ns,
   151  			Name:      "capacity",
   152  			Help:      "Current capacity(in bytes)",
   153  		},
   154  		[]string{"path"},
   155  	)
   156  
   157  	maxDataVec = prometheus.NewGaugeVec(
   158  		prometheus.GaugeOpts{
   159  			Namespace: ns,
   160  			Name:      "max_data",
   161  			Help:      "Max data to Put(in bytes), default 0",
   162  		},
   163  		[]string{"path"},
   164  	)
   165  
   166  	batchSizeVec = prometheus.NewGaugeVec(
   167  		prometheus.GaugeOpts{
   168  			Namespace: ns,
   169  			Name:      "batch_size",
   170  			Help:      "Data file size(in bytes)",
   171  		},
   172  		[]string{"path"},
   173  	)
   174  
   175  	sizeVec = prometheus.NewGaugeVec(
   176  		prometheus.GaugeOpts{
   177  			Namespace: ns,
   178  			Name:      "size",
   179  			Help:      "Current cache size(in bytes)",
   180  		},
   181  		[]string{"path"},
   182  	)
   183  
   184  	openTimeVec = prometheus.NewGaugeVec(
   185  		prometheus.GaugeOpts{
   186  			Namespace: ns,
   187  			Name:      "open_time",
   188  			Help:      "Current cache Open time in unix timestamp(second)",
   189  		},
   190  		[]string{
   191  			// NOTE: make them sorted.
   192  			"no_fallback_on_error",
   193  			"no_lock",
   194  			"no_pos",
   195  			"no_sync",
   196  			"path",
   197  		},
   198  	)
   199  
   200  	lastCloseTimeVec = prometheus.NewGaugeVec(
   201  		prometheus.GaugeOpts{
   202  			Namespace: ns,
   203  			Name:      "last_close_time",
   204  			Help:      "Current cache last Close time in unix timestamp(second)",
   205  		},
   206  		[]string{"path"},
   207  	)
   208  
   209  	datafilesVec = prometheus.NewGaugeVec(
   210  		prometheus.GaugeOpts{
   211  			Namespace: ns,
   212  			Name:      "datafiles",
   213  			Help:      "Current un-read data files",
   214  		},
   215  		[]string{"path"},
   216  	)
   217  
   218  	metrics.MustRegister(
   219  		droppedBatchVec,
   220  		droppedBytesVec,
   221  		rotateVec,
   222  		putVec,
   223  		getVec,
   224  		putBytesVec,
   225  		wakeupVec,
   226  		seekBackVec,
   227  		getBytesVec,
   228  
   229  		openTimeVec,
   230  		lastCloseTimeVec,
   231  		capVec,
   232  		batchSizeVec,
   233  		maxDataVec,
   234  		sizeVec,
   235  		datafilesVec,
   236  
   237  		getLatencyVec,
   238  		putLatencyVec)
   239  }
   240  
   241  // register to specified registry for testing.
   242  func register(reg *prometheus.Registry) {
   243  	reg.MustRegister(
   244  		droppedBatchVec,
   245  		droppedBytesVec,
   246  		rotateVec,
   247  		putVec,
   248  		getVec,
   249  		putBytesVec,
   250  		wakeupVec,
   251  		seekBackVec,
   252  		getBytesVec,
   253  
   254  		capVec,
   255  		batchSizeVec,
   256  		maxDataVec,
   257  		sizeVec,
   258  		datafilesVec,
   259  
   260  		getLatencyVec,
   261  		putLatencyVec)
   262  }
   263  
   264  // ResetMetrics used to cleanup exist metrics of diskcache.
   265  func ResetMetrics() {
   266  	droppedBatchVec.Reset()
   267  	droppedBytesVec.Reset()
   268  	rotateVec.Reset()
   269  	putVec.Reset()
   270  	getVec.Reset()
   271  	putBytesVec.Reset()
   272  	wakeupVec.Reset()
   273  	seekBackVec.Reset()
   274  	getBytesVec.Reset()
   275  	capVec.Reset()
   276  	batchSizeVec.Reset()
   277  	maxDataVec.Reset()
   278  	sizeVec.Reset()
   279  	datafilesVec.Reset()
   280  	getLatencyVec.Reset()
   281  	putLatencyVec.Reset()
   282  }
   283  
   284  // Labels export cache's labels used to query prometheus metrics.
   285  // func (c *DiskCache) Labels() []string {
   286  //	return c.labels
   287  //}
   288  
   289  func Metrics() []prometheus.Collector {
   290  	return []prometheus.Collector{
   291  		droppedBatchVec,
   292  		droppedBytesVec,
   293  		rotateVec,
   294  		removeVec,
   295  		putVec,
   296  		getVec,
   297  		putBytesVec,
   298  		wakeupVec,
   299  		seekBackVec,
   300  		getBytesVec,
   301  
   302  		sizeVec,
   303  		openTimeVec,
   304  		lastCloseTimeVec,
   305  		capVec,
   306  		maxDataVec,
   307  		batchSizeVec,
   308  		datafilesVec,
   309  
   310  		getLatencyVec,
   311  		putLatencyVec,
   312  	}
   313  }
   314  
   315  // nolint: gochecknoinits
   316  func init() {
   317  	setupMetrics()
   318  }