github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/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 //nolint:gochecknoinits // TODO: refactor 201 func init() { 202 prometheus.MustRegister(IdleWorkersGauge) 203 prometheus.MustRegister(ImporterEngineCounter) 204 prometheus.MustRegister(KvEncoderCounter) 205 prometheus.MustRegister(TableCounter) 206 prometheus.MustRegister(ProcessedEngineCounter) 207 prometheus.MustRegister(ChunkCounter) 208 prometheus.MustRegister(BytesCounter) 209 prometheus.MustRegister(ImportSecondsHistogram) 210 prometheus.MustRegister(RowReadSecondsHistogram) 211 prometheus.MustRegister(RowReadBytesHistogram) 212 prometheus.MustRegister(RowEncodeSecondsHistogram) 213 prometheus.MustRegister(RowKVDeliverSecondsHistogram) 214 prometheus.MustRegister(BlockDeliverSecondsHistogram) 215 prometheus.MustRegister(BlockDeliverBytesHistogram) 216 prometheus.MustRegister(BlockDeliverKVPairsHistogram) 217 prometheus.MustRegister(ChecksumSecondsHistogram) 218 prometheus.MustRegister(ChunkParserReadBlockSecondsHistogram) 219 prometheus.MustRegister(ApplyWorkerSecondsHistogram) 220 prometheus.MustRegister(LocalStorageUsageBytesGauge) 221 } 222 223 func RecordTableCount(status string, err error) { 224 var result string 225 if err != nil { 226 result = TableResultFailure 227 } else { 228 result = TableResultSuccess 229 } 230 TableCounter.WithLabelValues(status, result).Inc() 231 } 232 233 func RecordEngineCount(status string, err error) { 234 var result string 235 if err != nil { 236 result = TableResultFailure 237 } else { 238 result = TableResultSuccess 239 } 240 ProcessedEngineCounter.WithLabelValues(status, result).Inc() 241 } 242 243 // ReadCounter reports the current value of the counter. 244 func ReadCounter(counter prometheus.Counter) float64 { 245 var metric dto.Metric 246 if err := counter.Write(&metric); err != nil { 247 return math.NaN() 248 } 249 return metric.Counter.GetValue() 250 } 251 252 // ReadHistogramSum reports the sum of all observed values in the histogram. 253 func ReadHistogramSum(histogram prometheus.Histogram) float64 { 254 var metric dto.Metric 255 if err := histogram.Write(&metric); err != nil { 256 return math.NaN() 257 } 258 return metric.Histogram.GetSampleSum() 259 }