|
| 1 | +package indexer |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/metrics" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + indexerNamespace = "indexer" |
| 12 | +) |
| 13 | + |
| 14 | +// Build a fully qualified metric name |
| 15 | +func metricName(subsystem, name string) string { |
| 16 | + if name == "" { |
| 17 | + return "" |
| 18 | + } |
| 19 | + parts := []string{indexerNamespace, name} |
| 20 | + if subsystem != "" { |
| 21 | + parts = []string{indexerNamespace, subsystem, name} |
| 22 | + } |
| 23 | + // Prometheus uses _ but geth metrics uses / and replaces |
| 24 | + return strings.Join(parts, "/") |
| 25 | +} |
| 26 | + |
| 27 | +type indexerMetricsHandles struct { |
| 28 | + // The total number of processed blocks |
| 29 | + blocks metrics.Counter |
| 30 | + // The total number of processed transactions |
| 31 | + transactions metrics.Counter |
| 32 | + // The total number of processed receipts |
| 33 | + receipts metrics.Counter |
| 34 | + // Time spent waiting for free postgres tx |
| 35 | + tFreePostgres metrics.Timer |
| 36 | + // Postgres transaction commit duration |
| 37 | + tPostgresCommit metrics.Timer |
| 38 | + // Header processing time |
| 39 | + tHeaderProcessing metrics.Timer |
| 40 | + // Uncle processing time |
| 41 | + tUncleProcessing metrics.Timer |
| 42 | + // Tx and receipt processing time |
| 43 | + tTxAndRecProcessing metrics.Timer |
| 44 | + // State, storage, and code combined processing time |
| 45 | + tStateStoreCodeProcessing metrics.Timer |
| 46 | +} |
| 47 | + |
| 48 | +func RegisterIndexerMetrics(reg metrics.Registry) indexerMetricsHandles { |
| 49 | + ctx := indexerMetricsHandles{ |
| 50 | + blocks: metrics.NewCounter(), |
| 51 | + transactions: metrics.NewCounter(), |
| 52 | + receipts: metrics.NewCounter(), |
| 53 | + tFreePostgres: metrics.NewTimer(), |
| 54 | + tPostgresCommit: metrics.NewTimer(), |
| 55 | + tHeaderProcessing: metrics.NewTimer(), |
| 56 | + tUncleProcessing: metrics.NewTimer(), |
| 57 | + tTxAndRecProcessing: metrics.NewTimer(), |
| 58 | + tStateStoreCodeProcessing: metrics.NewTimer(), |
| 59 | + } |
| 60 | + subsys := "" // todo |
| 61 | + reg.Register(metricName(subsys, "blocks"), ctx.blocks) |
| 62 | + reg.Register(metricName(subsys, "transactions"), ctx.transactions) |
| 63 | + reg.Register(metricName(subsys, "receipts"), ctx.receipts) |
| 64 | + reg.Register(metricName(subsys, "t_free_postgres"), ctx.tFreePostgres) |
| 65 | + reg.Register(metricName(subsys, "t_postgres_commit"), ctx.tPostgresCommit) |
| 66 | + reg.Register(metricName(subsys, "t_header_processing"), ctx.tHeaderProcessing) |
| 67 | + reg.Register(metricName(subsys, "t_uncle_processing"), ctx.tUncleProcessing) |
| 68 | + reg.Register(metricName(subsys, "t_tx_receipt_processing"), ctx.tTxAndRecProcessing) |
| 69 | + reg.Register(metricName(subsys, "t_state_store_code_processing"), ctx.tStateStoreCodeProcessing) |
| 70 | + return ctx |
| 71 | +} |
| 72 | + |
| 73 | +type dbMetricsHandles struct { |
| 74 | + // Maximum number of open connections to the database |
| 75 | + maxOpen metrics.Gauge |
| 76 | + // The number of established connections both in use and idle |
| 77 | + open metrics.Gauge |
| 78 | + // The number of connections currently in use |
| 79 | + inUse metrics.Gauge |
| 80 | + // The number of idle connections |
| 81 | + idle metrics.Gauge |
| 82 | + // The total number of connections waited for |
| 83 | + waitedFor metrics.Counter |
| 84 | + // The total time blocked waiting for a new connection |
| 85 | + blockedMilliseconds metrics.Counter |
| 86 | + // The total number of connections closed due to SetMaxIdleConns |
| 87 | + closedMaxIdle metrics.Counter |
| 88 | + // The total number of connections closed due to SetConnMaxLifetime |
| 89 | + closedMaxLifetime metrics.Counter |
| 90 | +} |
| 91 | + |
| 92 | +func RegisterDBMetrics(reg metrics.Registry) dbMetricsHandles { |
| 93 | + ctx := dbMetricsHandles{ |
| 94 | + maxOpen: metrics.NewGauge(), |
| 95 | + open: metrics.NewGauge(), |
| 96 | + inUse: metrics.NewGauge(), |
| 97 | + idle: metrics.NewGauge(), |
| 98 | + waitedFor: metrics.NewCounter(), |
| 99 | + blockedMilliseconds: metrics.NewCounter(), |
| 100 | + closedMaxIdle: metrics.NewCounter(), |
| 101 | + closedMaxLifetime: metrics.NewCounter(), |
| 102 | + } |
| 103 | + subsys := "connections" |
| 104 | + reg.Register(metricName(subsys, "max_open"), ctx.maxOpen) |
| 105 | + reg.Register(metricName(subsys, "open"), ctx.open) |
| 106 | + reg.Register(metricName(subsys, "in_use"), ctx.inUse) |
| 107 | + reg.Register(metricName(subsys, "idle"), ctx.idle) |
| 108 | + reg.Register(metricName(subsys, "waited_for"), ctx.waitedFor) |
| 109 | + reg.Register(metricName(subsys, "blocked_milliseconds"), ctx.blockedMilliseconds) |
| 110 | + reg.Register(metricName(subsys, "closed_max_idle"), ctx.closedMaxIdle) |
| 111 | + reg.Register(metricName(subsys, "closed_max_lifetime"), ctx.closedMaxLifetime) |
| 112 | + return ctx |
| 113 | +} |
| 114 | + |
| 115 | +func (met *dbMetricsHandles) Update(stats sql.DBStats) { |
| 116 | + met.maxOpen.Update(int64(stats.MaxOpenConnections)) |
| 117 | + met.open.Update(int64(stats.OpenConnections)) |
| 118 | + met.inUse.Update(int64(stats.InUse)) |
| 119 | + met.idle.Update(int64(stats.Idle)) |
| 120 | + met.waitedFor.Inc(int64(stats.WaitCount)) |
| 121 | + met.blockedMilliseconds.Inc(int64(stats.WaitDuration.Milliseconds())) |
| 122 | + met.closedMaxIdle.Inc(int64(stats.MaxIdleClosed)) |
| 123 | + met.closedMaxLifetime.Inc(int64(stats.MaxLifetimeClosed)) |
| 124 | +} |
0 commit comments