chore: name unlabeled db transaction metrics (#15251)

This commit is contained in:
Steven Masley
2024-10-28 13:15:29 -04:00
committed by GitHub
parent ecb22461bb
commit 7cb20d7b26
2 changed files with 8 additions and 3 deletions

View File

@ -74,6 +74,11 @@ func (m metricsStore) InTx(f func(database.Store) error, options *database.TxOpt
options = database.DefaultTXOptions() options = database.DefaultTXOptions()
} }
if options.TxIdentifier == "" {
// empty strings are hard to deal with in grafana
options.TxIdentifier = "unlabeled"
}
start := time.Now() start := time.Now()
err := m.Store.InTx(f, options) err := m.Store.InTx(f, options)
dur := time.Since(start) dur := time.Since(start)
@ -82,13 +87,13 @@ func (m metricsStore) InTx(f func(database.Store) error, options *database.TxOpt
// So IDs should be used sparingly to prevent too much bloat. // So IDs should be used sparingly to prevent too much bloat.
m.txDuration.With(prometheus.Labels{ m.txDuration.With(prometheus.Labels{
"success": strconv.FormatBool(err == nil), "success": strconv.FormatBool(err == nil),
"tx_id": options.TxIdentifier, // Can be empty string for unlabeled "tx_id": options.TxIdentifier,
}).Observe(dur.Seconds()) }).Observe(dur.Seconds())
m.txRetries.With(prometheus.Labels{ m.txRetries.With(prometheus.Labels{
"success": strconv.FormatBool(err == nil), "success": strconv.FormatBool(err == nil),
"retries": strconv.FormatInt(int64(options.ExecutionCount()-1), 10), "retries": strconv.FormatInt(int64(options.ExecutionCount()-1), 10),
"tx_id": options.TxIdentifier, // Can be empty string for unlabeled "tx_id": options.TxIdentifier,
}).Inc() }).Inc()
// Log all serializable transactions that are retried. // Log all serializable transactions that are retried.

View File

@ -22,7 +22,7 @@ func TestInTxMetrics(t *testing.T) {
successLabels := prometheus.Labels{ successLabels := prometheus.Labels{
"success": "true", "success": "true",
"tx_id": "", "tx_id": "unlabeled",
} }
const inTxHistMetricName = "coderd_db_tx_duration_seconds" const inTxHistMetricName = "coderd_db_tx_duration_seconds"
const inTxCountMetricName = "coderd_db_tx_executions_count" const inTxCountMetricName = "coderd_db_tx_executions_count"