|
| 1 | +// Scrape `information_schema.innodb_cmpmem`. |
| 2 | + |
| 3 | +package collector |
| 4 | + |
| 5 | +import ( |
| 6 | + "database/sql" |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/prometheus/common/log" |
| 12 | +) |
| 13 | + |
| 14 | +const innodbCmpMemQuery = ` |
| 15 | + SELECT |
| 16 | + page_size, buffer_pool_instance, pages_used, pages_free, relocation_ops, relocation_time |
| 17 | + FROM information_schema.INNODB_CMPMEM |
| 18 | + ` |
| 19 | + |
| 20 | +//Metric descriptors. |
| 21 | +var ( |
| 22 | + // Map known innodb_cmp values to types. Unknown types will be mapped as |
| 23 | + // untyped. |
| 24 | + informationSchemaInnodbCmpMemTypes = map[string]struct { |
| 25 | + vtype prometheus.ValueType |
| 26 | + desc *prometheus.Desc |
| 27 | + }{ |
| 28 | + "pages_used": {prometheus.CounterValue, |
| 29 | + prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "innodb_cmpmem_pages_used_total"), |
| 30 | + "Number of blocks of the size PAGE_SIZE that are currently in use.", |
| 31 | + []string{"page_size", "buffer"}, nil)}, |
| 32 | + "pages_free": {prometheus.CounterValue, |
| 33 | + prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "innodb_cmpmem_pages_free_total"), |
| 34 | + "Number of blocks of the size PAGE_SIZE that are currently available for allocation.", |
| 35 | + []string{"page_size", "buffer"}, nil)}, |
| 36 | + "relocation_ops": {prometheus.CounterValue, |
| 37 | + prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "innodb_cmpmem_relocation_ops_total"), |
| 38 | + "Number of times a block of the size PAGE_SIZE has been relocated.", |
| 39 | + []string{"page_size", "buffer"}, nil)}, |
| 40 | + "relocation_time": {prometheus.CounterValue, |
| 41 | + prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, "innodb_cmpmem_relocation_time_seconds_total"), |
| 42 | + "Total time in microseconds spent in relocating blocks of the size PAGE_SIZE.", |
| 43 | + []string{"page_size", "buffer"}, nil)}, |
| 44 | + } |
| 45 | +) |
| 46 | + |
| 47 | +// ScrapeInnodbCmpMem collects from `information_schema.innodb_cmpmem`. |
| 48 | +type ScrapeInnodbCmpMem struct{} |
| 49 | + |
| 50 | +// Name of the Scraper. |
| 51 | +func (ScrapeInnodbCmpMem) Name() string { |
| 52 | + return "info_schema.innodb_cmpmem" |
| 53 | +} |
| 54 | + |
| 55 | +// Help returns additional information about Scraper. |
| 56 | +func (ScrapeInnodbCmpMem) Help() string { |
| 57 | + return "Please set next variables SET GLOBAL innodb_file_per_table=1;SET GLOBAL innodb_file_format=Barracuda;" |
| 58 | +} |
| 59 | + |
| 60 | +// Version of MySQL from which scraper is available. |
| 61 | +func (ScrapeInnodbCmpMem) Version() float64 { |
| 62 | + return 5.5 |
| 63 | +} |
| 64 | + |
| 65 | +// Scrape collects data. |
| 66 | +func (ScrapeInnodbCmpMem) Scrape(db *sql.DB, ch chan<- prometheus.Metric) error { |
| 67 | + informationSchemaInnodbCmpMemRows, err := db.Query(innodbCmpMemQuery) |
| 68 | + if err != nil { |
| 69 | + log.Debugln("INNODB_CMPMEM stats are not available.") |
| 70 | + return err |
| 71 | + } |
| 72 | + defer informationSchemaInnodbCmpMemRows.Close() |
| 73 | + |
| 74 | + // The client column is assumed to be column[0], while all other data is assumed to be coerceable to float64. |
| 75 | + // Because of the client column, clientStatData[0] maps to columnNames[1] when reading off the metrics |
| 76 | + // (because clientStatScanArgs is mapped as [ &client, &buffer, &clientData[0], &clientData[1] ... &clientdata[n] ] |
| 77 | + // To map metrics to names therefore we always range over columnNames[1:] |
| 78 | + columnNames, err := informationSchemaInnodbCmpMemRows.Columns() |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + log.Debugln("INNODB_CMPMEM stats are not available.") |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + var ( |
| 86 | + client string // Holds the client name, which should be in column 0. |
| 87 | + buffer string // Holds the buffer number, which should be in column 1. |
| 88 | + clientStatData = make([]float64, len(columnNames)-2) // 2 less because of the client column. |
| 89 | + clientStatScanArgs = make([]interface{}, len(columnNames)) |
| 90 | + ) |
| 91 | + |
| 92 | + clientStatScanArgs[0] = &client |
| 93 | + clientStatScanArgs[1] = &buffer |
| 94 | + for i := range clientStatData { |
| 95 | + clientStatScanArgs[i+2] = &clientStatData[i] |
| 96 | + } |
| 97 | + |
| 98 | + for informationSchemaInnodbCmpMemRows.Next() { |
| 99 | + if err := informationSchemaInnodbCmpMemRows.Scan(clientStatScanArgs...); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + // Loop over column names, and match to scan data. Unknown columns |
| 103 | + // will be filled with an untyped metric number. We assume other then |
| 104 | + // cient, that we'll only get numbers. |
| 105 | + for idx, columnName := range columnNames[2:] { |
| 106 | + if metricType, ok := informationSchemaInnodbCmpMemTypes[columnName]; ok { |
| 107 | + if columnName == "relocation_time" { |
| 108 | + ch <- prometheus.MustNewConstMetric(metricType.desc, metricType.vtype, float64(clientStatData[idx]/1000), client, buffer) |
| 109 | + } else { |
| 110 | + ch <- prometheus.MustNewConstMetric(metricType.desc, metricType.vtype, float64(clientStatData[idx]), client, buffer) |
| 111 | + } |
| 112 | + } else { |
| 113 | + // Unknown metric. Report as untyped. |
| 114 | + desc := prometheus.NewDesc(prometheus.BuildFQName(namespace, informationSchema, fmt.Sprintf("innodb_cmpmem_%s", strings.ToLower(columnName))), fmt.Sprintf("Unsupported metric from column %s", columnName), []string{"page_size", "buffer"}, nil) |
| 115 | + ch <- prometheus.MustNewConstMetric(desc, prometheus.UntypedValue, float64(clientStatData[idx]), client, buffer) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
0 commit comments