@@ -16,42 +16,51 @@ package collector
16
16
import (
17
17
"context"
18
18
"database/sql"
19
+ "strconv"
19
20
20
21
"github.com/go-kit/kit/log"
21
22
"github.com/prometheus/client_golang/prometheus"
22
23
)
23
24
24
- const perfReplicationGroupMemeberStatsQuery = `
25
- SELECT MEMBER_ID,COUNT_TRANSACTIONS_IN_QUEUE,COUNT_TRANSACTIONS_CHECKED,COUNT_CONFLICTS_DETECTED,COUNT_TRANSACTIONS_ROWS_VALIDATING
26
- FROM performance_schema.replication_group_member_stats
27
- `
25
+ const perfReplicationGroupMemberStatsQuery = `
26
+ SELECT * FROM performance_schema.replication_group_member_stats WHERE MEMBER_ID=@@server_uuid
27
+ `
28
28
29
- // Metric descriptors.
30
29
var (
31
- performanceSchemaReplicationGroupMemberStatsTransInQueueDesc = prometheus .NewDesc (
32
- prometheus .BuildFQName (namespace , performanceSchema , "transaction_in_queue" ),
33
- "The number of transactions in the queue pending conflict detection checks. Once the " +
34
- "transactions have been checked for conflicts, if they pass the check, they are queued to be applied as well." ,
35
- []string {"member_id" }, nil ,
36
- )
37
- performanceSchemaReplicationGroupMemberStatsTransCheckedDesc = prometheus .NewDesc (
38
- prometheus .BuildFQName (namespace , performanceSchema , "transaction_checked" ),
39
- "The number of transactions that have been checked for conflicts." ,
40
- []string {"member_id" }, nil ,
41
- )
42
- performanceSchemaReplicationGroupMemberStatsConflictsDetectedDesc = prometheus .NewDesc (
43
- prometheus .BuildFQName (namespace , performanceSchema , "conflicts_detected" ),
44
- "The number of transactions that did not pass the conflict detection check." ,
45
- []string {"member_id" }, nil ,
46
- )
47
- performanceSchemaReplicationGroupMemberStatsTransRowValidatingDesc = prometheus .NewDesc (
48
- prometheus .BuildFQName (namespace , performanceSchema , "transaction_rows_validating" ),
49
- "The current size of the conflict detection database (against which each transaction is certified)." ,
50
- []string {"member_id" }, nil ,
51
- )
30
+ // The list of columns we are interesting in.
31
+ // In MySQL 5.7 these are the 4 first columns available. In MySQL 8.x all 8.
32
+ perfReplicationGroupMemberStats = map [string ]struct {
33
+ vtype prometheus.ValueType
34
+ desc * prometheus.Desc
35
+ }{
36
+ "COUNT_TRANSACTIONS_IN_QUEUE" : {prometheus .GaugeValue ,
37
+ prometheus .NewDesc (prometheus .BuildFQName (namespace , performanceSchema , "transactions_in_queue" ),
38
+ "The number of transactions in the queue pending conflict detection checks." , nil , nil )},
39
+ "COUNT_TRANSACTIONS_CHECKED" : {prometheus .CounterValue ,
40
+ prometheus .NewDesc (prometheus .BuildFQName (namespace , performanceSchema , "transactions_checked_total" ),
41
+ "The number of transactions that have been checked for conflicts." , nil , nil )},
42
+ "COUNT_CONFLICTS_DETECTED" : {prometheus .CounterValue ,
43
+ prometheus .NewDesc (prometheus .BuildFQName (namespace , performanceSchema , "conflicts_detected_total" ),
44
+ "The number of transactions that have not passed the conflict detection check." , nil , nil )},
45
+ "COUNT_TRANSACTIONS_ROWS_VALIDATING" : {prometheus .CounterValue ,
46
+ prometheus .NewDesc (prometheus .BuildFQName (namespace , performanceSchema , "transactions_rows_validating_total" ),
47
+ "Number of transaction rows which can be used for certification, but have not been garbage collected." , nil , nil )},
48
+ "COUNT_TRANSACTIONS_REMOTE_IN_APPLIER_QUEUE" : {prometheus .GaugeValue ,
49
+ prometheus .NewDesc (prometheus .BuildFQName (namespace , performanceSchema , "transactions_remote_in_applier_queue" ),
50
+ "The number of transactions that this member has received from the replication group which are waiting to be applied." , nil , nil )},
51
+ "COUNT_TRANSACTIONS_REMOTE_APPLIED" : {prometheus .CounterValue ,
52
+ prometheus .NewDesc (prometheus .BuildFQName (namespace , performanceSchema , "transactions_remote_applied_total" ),
53
+ "Number of transactions this member has received from the group and applied." , nil , nil )},
54
+ "COUNT_TRANSACTIONS_LOCAL_PROPOSED" : {prometheus .CounterValue ,
55
+ prometheus .NewDesc (prometheus .BuildFQName (namespace , performanceSchema , "transactions_local_proposed_total" ),
56
+ "Number of transactions which originated on this member and were sent to the group." , nil , nil )},
57
+ "COUNT_TRANSACTIONS_LOCAL_ROLLBACK" : {prometheus .CounterValue ,
58
+ prometheus .NewDesc (prometheus .BuildFQName (namespace , performanceSchema , "transactions_local_rollback_total" ),
59
+ "Number of transactions which originated on this member and were rolled back by the group." , nil , nil )},
60
+ }
52
61
)
53
62
54
- // ScrapeReplicationGroupMemberStats collects from `performance_schema.replication_group_member_stats`.
63
+ // ScrapePerfReplicationGroupMemberStats collects from `performance_schema.replication_group_member_stats`.
55
64
type ScrapePerfReplicationGroupMemberStats struct {}
56
65
57
66
// Name of the Scraper. Should be unique.
@@ -71,41 +80,36 @@ func (ScrapePerfReplicationGroupMemberStats) Version() float64 {
71
80
72
81
// Scrape collects data from database connection and sends it over channel as prometheus metric.
73
82
func (ScrapePerfReplicationGroupMemberStats ) Scrape (ctx context.Context , db * sql.DB , ch chan <- prometheus.Metric , logger log.Logger ) error {
74
- perfReplicationGroupMemeberStatsRows , err := db .QueryContext (ctx , perfReplicationGroupMemeberStatsQuery )
83
+ rows , err := db .QueryContext (ctx , perfReplicationGroupMemberStatsQuery )
75
84
if err != nil {
76
85
return err
77
86
}
78
- defer perfReplicationGroupMemeberStatsRows .Close ()
87
+ defer rows .Close ()
79
88
80
- var (
81
- memberId string
82
- countTransactionsInQueue , countTransactionsChecked uint64
83
- countConflictsDetected , countTransactionsRowsValidating uint64
84
- )
89
+ var columnNames []string
90
+ if columnNames , err = rows .Columns (); err != nil {
91
+ return err
92
+ }
85
93
86
- for perfReplicationGroupMemeberStatsRows .Next () {
87
- if err := perfReplicationGroupMemeberStatsRows .Scan (
88
- & memberId , & countTransactionsInQueue , & countTransactionsChecked ,
89
- & countConflictsDetected , & countTransactionsRowsValidating ,
90
- ); err != nil {
94
+ var scanArgs = make ([]interface {}, len (columnNames ))
95
+ for i := range scanArgs {
96
+ scanArgs [i ] = & sql.RawBytes {}
97
+ }
98
+
99
+ for rows .Next () {
100
+ if err := rows .Scan (scanArgs ... ); err != nil {
91
101
return err
92
102
}
93
- ch <- prometheus .MustNewConstMetric (
94
- performanceSchemaReplicationGroupMemberStatsTransInQueueDesc , prometheus .CounterValue , float64 (countTransactionsInQueue ),
95
- memberId ,
96
- )
97
- ch <- prometheus .MustNewConstMetric (
98
- performanceSchemaReplicationGroupMemberStatsTransCheckedDesc , prometheus .CounterValue , float64 (countTransactionsChecked ),
99
- memberId ,
100
- )
101
- ch <- prometheus .MustNewConstMetric (
102
- performanceSchemaReplicationGroupMemberStatsConflictsDetectedDesc , prometheus .CounterValue , float64 (countConflictsDetected ),
103
- memberId ,
104
- )
105
- ch <- prometheus .MustNewConstMetric (
106
- performanceSchemaReplicationGroupMemberStatsTransRowValidatingDesc , prometheus .CounterValue , float64 (countTransactionsRowsValidating ),
107
- memberId ,
108
- )
103
+
104
+ for i , columnName := range columnNames {
105
+ if metric , ok := perfReplicationGroupMemberStats [columnName ]; ok {
106
+ value , err := strconv .ParseFloat (string (* scanArgs [i ].(* sql.RawBytes )), 64 )
107
+ if err != nil {
108
+ return err
109
+ }
110
+ ch <- prometheus .MustNewConstMetric (metric .desc , metric .vtype , value )
111
+ }
112
+ }
109
113
}
110
114
return nil
111
115
}
0 commit comments