-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathmetrics_test.go
More file actions
70 lines (54 loc) · 2.05 KB
/
Copy pathmetrics_test.go
File metadata and controls
70 lines (54 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2026 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package logdb
import (
"database/sql"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
dto "github.com/prometheus/client_model/go"
"github.com/vechain/thor/v2/metrics"
)
func TestReadDBStatsSamples(t *testing.T) {
samples := readDBStatsSamples(sql.DBStats{
InUse: 3,
WaitCount: 7,
WaitDuration: 250 * time.Millisecond,
})
require.Len(t, samples, 3)
byName := make(map[string]metrics.Sample)
for _, s := range samples {
byName[s.Name] = s
}
require.Equal(t, metrics.KindGauge, byName["readdb_in_use_connections"].Kind)
require.Equal(t, float64(3), byName["readdb_in_use_connections"].Value)
require.Equal(t, metrics.KindCounter, byName["readdb_wait_count"].Kind)
require.Equal(t, float64(7), byName["readdb_wait_count"].Value)
require.Equal(t, metrics.KindCounter, byName["readdb_wait_duration_ms"].Kind)
require.Equal(t, float64(250), byName["readdb_wait_duration_ms"].Value)
}
func TestEnableMetricsExportsReadDBPoolStats(t *testing.T) {
metrics.InitializePrometheusMetrics()
db, err := NewMem()
require.NoError(t, err)
db.EnableMetrics()
gather := func() map[string]*dto.MetricFamily {
mfs, err := prometheus.DefaultGatherer.Gather()
require.NoError(t, err)
out := make(map[string]*dto.MetricFamily)
for _, mf := range mfs {
out[mf.GetName()] = mf
}
return out
}
m := gather()
require.Equal(t, dto.MetricType_GAUGE, m["thor_metrics_logdb_readdb_in_use_connections"].GetType())
require.Equal(t, dto.MetricType_COUNTER, m["thor_metrics_logdb_readdb_wait_count"].GetType())
require.Equal(t, dto.MetricType_COUNTER, m["thor_metrics_logdb_readdb_wait_duration_ms"].GetType())
// Closing the DB must release the collector.
require.NoError(t, db.Close())
_, present := gather()["thor_metrics_logdb_readdb_in_use_connections"]
require.False(t, present)
}