package appview import ( "context" "time" "github.com/prometheus/client_golang/prometheus" "provenance/internal/storage" ) const metricsDBTimeout = time.Second type databaseMetrics struct { store storage.Store bytes, wal, connections, inUse, waits, error *prometheus.Desc } func newDatabaseMetrics(store storage.Store) *databaseMetrics { desc := func(name, help string) *prometheus.Desc { return prometheus.NewDesc(name, help, nil, nil) } return &databaseMetrics{store: store, bytes: desc("provenance_database_allocated_bytes", "SQLite allocated page bytes, excluding WAL and caches."), wal: desc("provenance_database_wal_bytes", "Current SQLite WAL file size."), connections: desc("provenance_database_connections", "Open database connections."), inUse: desc("provenance_database_connections_in_use", "Database connections currently in use."), waits: desc("provenance_database_connection_waits_total", "Database connection waits since process start."), error: desc("provenance_database_scrape_error", "One when database allocation statistics cannot be read."), } } func (m *databaseMetrics) Describe(ch chan<- *prometheus.Desc) { for _, desc := range []*prometheus.Desc{m.bytes, m.wal, m.connections, m.inUse, m.waits, m.error} { ch <- desc } } func (m *databaseMetrics) Collect(ch chan<- prometheus.Metric) { ctx, cancel := context.WithTimeout(context.Background(), metricsDBTimeout) defer cancel() stats, err := m.store.DatabaseStats(ctx) if err != nil { ch <- prometheus.MustNewConstMetric(m.error, prometheus.GaugeValue, 1) return } ch <- prometheus.MustNewConstMetric(m.error, prometheus.GaugeValue, 0) ch <- prometheus.MustNewConstMetric(m.bytes, prometheus.GaugeValue, float64(stats.AllocatedBytes)) ch <- prometheus.MustNewConstMetric(m.wal, prometheus.GaugeValue, float64(stats.WALBytes)) ch <- prometheus.MustNewConstMetric(m.connections, prometheus.GaugeValue, float64(stats.Connections)) ch <- prometheus.MustNewConstMetric(m.inUse, prometheus.GaugeValue, float64(stats.InUse)) ch <- prometheus.MustNewConstMetric(m.waits, prometheus.CounterValue, float64(stats.Waits)) }