github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/metric/metric.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package metric
    15  
    16  import (
    17  	"math"
    18  
    19  	"github.com/prometheus/client_golang/prometheus"
    20  	dto "github.com/prometheus/client_model/go"
    21  )
    22  
    23  const (
    24  	// states used for the TableCounter labels
    25  	TableStatePending        = "pending"
    26  	TableStateWritten        = "written"
    27  	TableStateClosed         = "closed"
    28  	TableStateImported       = "imported"
    29  	TableStateAlteredAutoInc = "altered_auto_inc"
    30  	TableStateChecksum       = "checksum"
    31  	TableStateCompleted      = "completed"
    32  
    33  	// results used for the TableCounter labels
    34  	TableResultSuccess = "success"
    35  	TableResultFailure = "failure"
    36  
    37  	// states used for the ChunkCounter labels
    38  	ChunkStateEstimated = "estimated"
    39  	ChunkStatePending   = "pending"
    40  	ChunkStateRunning   = "running"
    41  	ChunkStateFinished  = "finished"
    42  	ChunkStateFailed    = "failed"
    43  
    44  	BlockDeliverKindIndex = "index"
    45  	BlockDeliverKindData  = "data"
    46  )
    47  
    48  var (
    49  	ImporterEngineCounter = prometheus.NewCounterVec(
    50  		prometheus.CounterOpts{
    51  			Namespace: "lightning",
    52  			Name:      "importer_engine",
    53  			Help:      "counting open and closed importer engines",
    54  		}, []string{"type"})
    55  
    56  	IdleWorkersGauge = prometheus.NewGaugeVec(
    57  		prometheus.GaugeOpts{
    58  			Namespace: "lightning",
    59  			Name:      "idle_workers",
    60  			Help:      "counting idle workers",
    61  		}, []string{"name"})
    62  
    63  	KvEncoderCounter = prometheus.NewCounterVec(
    64  		prometheus.CounterOpts{
    65  			Namespace: "lightning",
    66  			Name:      "kv_encoder",
    67  			Help:      "counting kv open and closed kv encoder",
    68  		}, []string{"type"},
    69  	)
    70  
    71  	TableCounter = prometheus.NewCounterVec(
    72  		prometheus.CounterOpts{
    73  			Namespace: "lightning",
    74  			Name:      "tables",
    75  			Help:      "count number of tables processed",
    76  		}, []string{"state", "result"})
    77  	ProcessedEngineCounter = prometheus.NewCounterVec(
    78  		prometheus.CounterOpts{
    79  			Namespace: "lightning",
    80  			Name:      "engines",
    81  			Help:      "count number of engines processed",
    82  		}, []string{"state", "result"})
    83  	ChunkCounter = prometheus.NewCounterVec(
    84  		prometheus.CounterOpts{
    85  			Namespace: "lightning",
    86  			Name:      "chunks",
    87  			Help:      "count number of chunks processed",
    88  		}, []string{"state"})
    89  	BytesCounter = prometheus.NewCounterVec(
    90  		prometheus.CounterOpts{
    91  			Namespace: "lightning",
    92  			Name:      "bytes",
    93  			Help:      "count of total bytes",
    94  		}, []string{"state"})
    95  	// state can be one of:
    96  	//  - estimated (an estimation derived from the file size)
    97  	//  - pending
    98  	//  - running
    99  	//  - finished
   100  	//  - failed
   101  
   102  	ImportSecondsHistogram = prometheus.NewHistogram(
   103  		prometheus.HistogramOpts{
   104  			Namespace: "lightning",
   105  			Name:      "import_seconds",
   106  			Help:      "time needed to import a table",
   107  			Buckets:   prometheus.ExponentialBuckets(0.125, 2, 6),
   108  		},
   109  	)
   110  	ChunkParserReadBlockSecondsHistogram = prometheus.NewHistogram(
   111  		prometheus.HistogramOpts{
   112  			Namespace: "lightning",
   113  			Name:      "chunk_parser_read_block_seconds",
   114  			Help:      "time needed for chunk parser read a block",
   115  			Buckets:   prometheus.ExponentialBuckets(0.001, 3.1622776601683795, 10),
   116  		},
   117  	)
   118  	ApplyWorkerSecondsHistogram = prometheus.NewHistogramVec(
   119  		prometheus.HistogramOpts{
   120  			Namespace: "lightning",
   121  			Name:      "apply_worker_seconds",
   122  			Help:      "time needed to apply a worker",
   123  			Buckets:   prometheus.ExponentialBuckets(0.001, 3.1622776601683795, 10),
   124  		}, []string{"name"},
   125  	)
   126  	RowReadSecondsHistogram = prometheus.NewHistogram(
   127  		prometheus.HistogramOpts{
   128  			Namespace: "lightning",
   129  			Name:      "row_read_seconds",
   130  			Help:      "time needed to parse a row",
   131  			Buckets:   prometheus.ExponentialBuckets(0.001, 3.1622776601683795, 7),
   132  		},
   133  	)
   134  	RowReadBytesHistogram = prometheus.NewHistogram(
   135  		prometheus.HistogramOpts{
   136  			Namespace: "lightning",
   137  			Name:      "row_read_bytes",
   138  			Help:      "number of bytes being read out from data source",
   139  			Buckets:   prometheus.ExponentialBuckets(1024, 2, 8),
   140  		},
   141  	)
   142  	RowEncodeSecondsHistogram = prometheus.NewHistogram(
   143  		prometheus.HistogramOpts{
   144  			Namespace: "lightning",
   145  			Name:      "row_encode_seconds",
   146  			Help:      "time needed to encode a row",
   147  			Buckets:   prometheus.ExponentialBuckets(0.001, 3.1622776601683795, 10),
   148  		},
   149  	)
   150  	RowKVDeliverSecondsHistogram = prometheus.NewHistogram(
   151  		prometheus.HistogramOpts{
   152  			Namespace: "lightning",
   153  			Name:      "row_kv_deliver_seconds",
   154  			Help:      "time needed to deliver kvs of a single row",
   155  			Buckets:   prometheus.ExponentialBuckets(0.001, 3.1622776601683795, 10),
   156  		},
   157  	)
   158  	BlockDeliverSecondsHistogram = prometheus.NewHistogram(
   159  		prometheus.HistogramOpts{
   160  			Namespace: "lightning",
   161  			Name:      "block_deliver_seconds",
   162  			Help:      "time needed to deliver a block",
   163  			Buckets:   prometheus.ExponentialBuckets(0.001, 3.1622776601683795, 10),
   164  		},
   165  	)
   166  	BlockDeliverBytesHistogram = prometheus.NewHistogramVec(
   167  		prometheus.HistogramOpts{
   168  			Namespace: "lightning",
   169  			Name:      "block_deliver_bytes",
   170  			Help:      "number of bytes being sent out to importer",
   171  			Buckets:   prometheus.ExponentialBuckets(512, 2, 10),
   172  		}, []string{"kind"},
   173  	)
   174  	BlockDeliverKVPairsHistogram = prometheus.NewHistogramVec(
   175  		prometheus.HistogramOpts{
   176  			Namespace: "lightning",
   177  			Name:      "block_deliver_kv_pairs",
   178  			Help:      "number of KV pairs being sent out to importer",
   179  			Buckets:   prometheus.ExponentialBuckets(1, 2, 10),
   180  		}, []string{"kind"},
   181  	)
   182  	ChecksumSecondsHistogram = prometheus.NewHistogram(
   183  		prometheus.HistogramOpts{
   184  			Namespace: "lightning",
   185  			Name:      "checksum_seconds",
   186  			Help:      "time needed to complete the checksum stage",
   187  			Buckets:   prometheus.ExponentialBuckets(1, 2.2679331552660544, 10),
   188  		},
   189  	)
   190  
   191  	LocalStorageUsageBytesGauge = prometheus.NewGaugeVec(
   192  		prometheus.GaugeOpts{
   193  			Namespace: "lightning",
   194  			Name:      "local_storage_usage_bytes",
   195  			Help:      "disk/memory size currently occupied by intermediate files in local backend",
   196  		}, []string{"medium"},
   197  	)
   198  )
   199  
   200  func init() {
   201  	prometheus.MustRegister(IdleWorkersGauge)
   202  	prometheus.MustRegister(ImporterEngineCounter)
   203  	prometheus.MustRegister(KvEncoderCounter)
   204  	prometheus.MustRegister(TableCounter)
   205  	prometheus.MustRegister(ProcessedEngineCounter)
   206  	prometheus.MustRegister(ChunkCounter)
   207  	prometheus.MustRegister(BytesCounter)
   208  	prometheus.MustRegister(ImportSecondsHistogram)
   209  	prometheus.MustRegister(RowReadSecondsHistogram)
   210  	prometheus.MustRegister(RowReadBytesHistogram)
   211  	prometheus.MustRegister(RowEncodeSecondsHistogram)
   212  	prometheus.MustRegister(RowKVDeliverSecondsHistogram)
   213  	prometheus.MustRegister(BlockDeliverSecondsHistogram)
   214  	prometheus.MustRegister(BlockDeliverBytesHistogram)
   215  	prometheus.MustRegister(BlockDeliverKVPairsHistogram)
   216  	prometheus.MustRegister(ChecksumSecondsHistogram)
   217  	prometheus.MustRegister(ChunkParserReadBlockSecondsHistogram)
   218  	prometheus.MustRegister(ApplyWorkerSecondsHistogram)
   219  	prometheus.MustRegister(LocalStorageUsageBytesGauge)
   220  }
   221  
   222  func RecordTableCount(status string, err error) {
   223  	var result string
   224  	if err != nil {
   225  		result = TableResultFailure
   226  	} else {
   227  		result = TableResultSuccess
   228  	}
   229  	TableCounter.WithLabelValues(status, result).Inc()
   230  }
   231  
   232  func RecordEngineCount(status string, err error) {
   233  	var result string
   234  	if err != nil {
   235  		result = TableResultFailure
   236  	} else {
   237  		result = TableResultSuccess
   238  	}
   239  	ProcessedEngineCounter.WithLabelValues(status, result).Inc()
   240  }
   241  
   242  // ReadCounter reports the current value of the counter.
   243  func ReadCounter(counter prometheus.Counter) float64 {
   244  	var metric dto.Metric
   245  	if err := counter.Write(&metric); err != nil {
   246  		return math.NaN()
   247  	}
   248  	return metric.Counter.GetValue()
   249  }
   250  
   251  // ReadHistogramSum reports the sum of all observed values in the histogram.
   252  func ReadHistogramSum(histogram prometheus.Histogram) float64 {
   253  	var metric dto.Metric
   254  	if err := histogram.Write(&metric); err != nil {
   255  		return math.NaN()
   256  	}
   257  	return metric.Histogram.GetSampleSum()
   258  }