MCPcopy Index your code
hub / github.com/coder/coder / TestInTxMetrics

Function TestInTxMetrics

coderd/database/dbmetrics/dbmetrics_test.go:20–110  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestInTxMetrics(t *testing.T) {
21 t.Parallel()
22
23 successLabels := prometheus.Labels{
24 "success": "true",
25 "tx_id": "unlabeled",
26 }
27 const inTxHistMetricName = "coderd_db_tx_duration_seconds"
28 const inTxCountMetricName = "coderd_db_tx_executions_count"
29 t.Run("QueryMetrics", func(t *testing.T) {
30 t.Parallel()
31
32 db, _ := dbtestutil.NewDB(t)
33 reg := prometheus.NewRegistry()
34 db = dbmetrics.NewQueryMetrics(db, testutil.Logger(t), reg)
35
36 err := db.InTx(func(s database.Store) error {
37 return nil
38 }, nil)
39 require.NoError(t, err)
40
41 // Check that the metrics are registered
42 inTxMetric := promhelp.HistogramValue(t, reg, inTxHistMetricName, successLabels)
43 require.NotNil(t, inTxMetric)
44 require.Equal(t, uint64(1), inTxMetric.GetSampleCount())
45 })
46
47 t.Run("DBMetrics", func(t *testing.T) {
48 t.Parallel()
49
50 db, _ := dbtestutil.NewDB(t)
51 reg := prometheus.NewRegistry()
52 db = dbmetrics.NewDBMetrics(db, testutil.Logger(t), reg)
53
54 err := db.InTx(func(s database.Store) error {
55 return nil
56 }, nil)
57 require.NoError(t, err)
58
59 // Check that the metrics are registered
60 inTxMetric := promhelp.HistogramValue(t, reg, inTxHistMetricName, successLabels)
61 require.NotNil(t, inTxMetric)
62 require.Equal(t, uint64(1), inTxMetric.GetSampleCount())
63 })
64
65 // Test log output and metrics on failures
66 // Log example:
67 // [erro] database transaction hit serialization error and had to retry success=false executions=2 id=foobar_factory
68 t.Run("SerializationError", func(t *testing.T) {
69 t.Parallel()
70
71 var output bytes.Buffer
72 logger := slog.Make(sloghuman.Sink(&output))
73
74 reg := prometheus.NewRegistry()
75 db, _ := dbtestutil.NewDB(t)
76 db = dbmetrics.NewDBMetrics(db, logger, reg)
77 const id = "foobar_factory"

Callers

nothing calls this directly

Calls 15

NewDBFunction · 0.92
NewQueryMetricsFunction · 0.92
LoggerFunction · 0.92
HistogramValueFunction · 0.92
NewDBMetricsFunction · 0.92
DefaultTXOptionsFunction · 0.92
IncrementExecutionCountFunction · 0.92
CounterValueFunction · 0.92
RunMethod · 0.65
InTxMethod · 0.65
EqualMethod · 0.45
WithIDMethod · 0.45

Tested by

no test coverage detected