NewDBMetrics returns a database.Store that registers metrics for the database but does not handle individual queries. metricsStore is intended to always be used, because queryMetrics are a bit too verbose for many use cases.
(s database.Store, logger slog.Logger, reg prometheus.Registerer)
| 26 | // metricsStore is intended to always be used, because queryMetrics are a bit |
| 27 | // too verbose for many use cases. |
| 28 | func NewDBMetrics(s database.Store, logger slog.Logger, reg prometheus.Registerer) database.Store { |
| 29 | // Don't double-wrap. |
| 30 | if slices.Contains(s.Wrappers(), wrapname) { |
| 31 | return s |
| 32 | } |
| 33 | txRetries := prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 34 | Namespace: "coderd", |
| 35 | Subsystem: "db", |
| 36 | Name: "tx_executions_count", |
| 37 | Help: "Total count of transactions executed. 'retries' is expected to be 0 for a successful transaction.", |
| 38 | }, []string{ |
| 39 | "success", // Did the InTx function return an error? |
| 40 | // Number of executions, since we have retry logic on serialization errors. |
| 41 | // retries = Executions - 1 (as 1 execute is expected) |
| 42 | "retries", |
| 43 | // Uniquely naming some transactions can help debug reoccurring errors. |
| 44 | "tx_id", |
| 45 | }) |
| 46 | reg.MustRegister(txRetries) |
| 47 | |
| 48 | txDuration := prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 49 | Namespace: "coderd", |
| 50 | Subsystem: "db", |
| 51 | Name: "tx_duration_seconds", |
| 52 | Help: "Duration of transactions in seconds.", |
| 53 | Buckets: prometheus.DefBuckets, |
| 54 | }, []string{ |
| 55 | "success", // Did the InTx function return an error? |
| 56 | // Uniquely naming some transactions can help debug reoccurring errors. |
| 57 | "tx_id", |
| 58 | }) |
| 59 | reg.MustRegister(txDuration) |
| 60 | return &metricsStore{ |
| 61 | Store: s, |
| 62 | txDuration: txDuration, |
| 63 | txRetries: txRetries, |
| 64 | logger: logger, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func (m metricsStore) Wrappers() []string { |
| 69 | return append(m.Store.Wrappers(), wrapname) |