|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | +) |
| 9 | + |
| 10 | +type ScrapeSysUserSummaryByStatementType struct{} |
| 11 | + |
| 12 | +func (ScrapeSysUserSummaryByStatementType) Name() string { return "sys.user_summary_by_statement_type" } |
| 13 | +func (ScrapeSysUserSummaryByStatementType) Help() string { |
| 14 | + return "Collect metrics from sys.x$user_summary_by_statement_type." |
| 15 | +} |
| 16 | +func (ScrapeSysUserSummaryByStatementType) Version() float64 { return 5.7 } |
| 17 | + |
| 18 | +// Metric name stem to match sys_user_summary.go style. |
| 19 | +const userSummaryByStmtTypeStem = "user_summary_by_statement_type" |
| 20 | + |
| 21 | +// Descriptors. |
| 22 | +var ( |
| 23 | + sysUSSTStatementsTotal = prometheus.NewDesc( |
| 24 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_total"), |
| 25 | + "The total number of occurrences of the statement type for the user.", |
| 26 | + []string{"user", "statement"}, nil, |
| 27 | + ) |
| 28 | + sysUSSTTotalLatency = prometheus.NewDesc( |
| 29 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_latency"), |
| 30 | + "The total wait time of timed occurrences for the user and statement type (seconds).", |
| 31 | + []string{"user", "statement"}, nil, |
| 32 | + ) |
| 33 | + sysUSSTMaxLatency = prometheus.NewDesc( |
| 34 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_max_latency"), |
| 35 | + "The maximum single-statement latency for the user and statement type (seconds).", |
| 36 | + []string{"user", "statement"}, nil, |
| 37 | + ) |
| 38 | + sysUSSTLockLatency = prometheus.NewDesc( |
| 39 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_lock_latency"), |
| 40 | + "The total time spent waiting for locks for the user and statement type (seconds).", |
| 41 | + []string{"user", "statement"}, nil, |
| 42 | + ) |
| 43 | + sysUSSTCpuLatency = prometheus.NewDesc( |
| 44 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_cpu_latency"), |
| 45 | + "The total CPU time for the user and statement type (seconds).", |
| 46 | + []string{"user", "statement"}, nil, |
| 47 | + ) |
| 48 | + sysUSSTRowsSent = prometheus.NewDesc( |
| 49 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_rows_sent_total"), |
| 50 | + "The total number of rows sent for the user and statement type.", |
| 51 | + []string{"user", "statement"}, nil, |
| 52 | + ) |
| 53 | + sysUSSTRowsExamined = prometheus.NewDesc( |
| 54 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_rows_examined_total"), |
| 55 | + "The total number of rows examined for the user and statement type.", |
| 56 | + []string{"user", "statement"}, nil, |
| 57 | + ) |
| 58 | + sysUSSTRowsAffected = prometheus.NewDesc( |
| 59 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_rows_affected_total"), |
| 60 | + "The total number of rows affected for the user and statement type.", |
| 61 | + []string{"user", "statement"}, nil, |
| 62 | + ) |
| 63 | + sysUSSTFullScans = prometheus.NewDesc( |
| 64 | + prometheus.BuildFQName(namespace, sysSchema, userSummaryByStmtTypeStem+"_full_scans_total"), |
| 65 | + "The total number of full table scans for the user and statement type.", |
| 66 | + []string{"user", "statement"}, nil, |
| 67 | + ) |
| 68 | +) |
| 69 | + |
| 70 | +func (ScrapeSysUserSummaryByStatementType) Scrape( |
| 71 | + ctx context.Context, |
| 72 | + inst *instance, |
| 73 | + ch chan<- prometheus.Metric, |
| 74 | + _ *slog.Logger, |
| 75 | +) error { |
| 76 | + const q = ` |
| 77 | +SELECT |
| 78 | + user, |
| 79 | + statement, |
| 80 | + total, |
| 81 | + total_latency, |
| 82 | + max_latency, |
| 83 | + lock_latency, |
| 84 | + cpu_latency, |
| 85 | + rows_sent, |
| 86 | + rows_examined, |
| 87 | + rows_affected, |
| 88 | + full_scans |
| 89 | +FROM sys.x$user_summary_by_statement_type` |
| 90 | + |
| 91 | + rows, err := inst.db.QueryContext(ctx, q) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + defer rows.Close() |
| 96 | + |
| 97 | + for rows.Next() { |
| 98 | + var ( |
| 99 | + user, stmt string |
| 100 | + total uint64 |
| 101 | + totalPs, maxPs, lockPs, cpuPs uint64 |
| 102 | + rowsSent, rowsExam, rowsAff, fscs uint64 |
| 103 | + ) |
| 104 | + if err := rows.Scan(&user, &stmt, &total, &totalPs, &maxPs, &lockPs, &cpuPs, &rowsSent, &rowsExam, &rowsAff, &fscs); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + ch <- prometheus.MustNewConstMetric(sysUSSTStatementsTotal, prometheus.GaugeValue, float64(total), user, stmt) |
| 109 | + ch <- prometheus.MustNewConstMetric(sysUSSTTotalLatency, prometheus.GaugeValue, float64(totalPs)/picoSeconds, user, stmt) |
| 110 | + ch <- prometheus.MustNewConstMetric(sysUSSTMaxLatency, prometheus.GaugeValue, float64(maxPs)/picoSeconds, user, stmt) |
| 111 | + ch <- prometheus.MustNewConstMetric(sysUSSTLockLatency, prometheus.GaugeValue, float64(lockPs)/picoSeconds, user, stmt) |
| 112 | + ch <- prometheus.MustNewConstMetric(sysUSSTCpuLatency, prometheus.GaugeValue, float64(cpuPs)/picoSeconds, user, stmt) |
| 113 | + ch <- prometheus.MustNewConstMetric(sysUSSTRowsSent, prometheus.GaugeValue, float64(rowsSent), user, stmt) |
| 114 | + ch <- prometheus.MustNewConstMetric(sysUSSTRowsExamined, prometheus.GaugeValue, float64(rowsExam), user, stmt) |
| 115 | + ch <- prometheus.MustNewConstMetric(sysUSSTRowsAffected, prometheus.GaugeValue, float64(rowsAff), user, stmt) |
| 116 | + ch <- prometheus.MustNewConstMetric(sysUSSTFullScans, prometheus.GaugeValue, float64(fscs), user, stmt) |
| 117 | + } |
| 118 | + return rows.Err() |
| 119 | +} |
0 commit comments