Skip to content

Commit eea25bc

Browse files
vitess-bot[bot]vitess-bot
authored andcommitted
fix: App and Dba Pool metrics (#18048)
Signed-off-by: Harshit Gangal <[email protected]>
1 parent 10f3d90 commit eea25bc

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

go/vt/dbconnpool/connection_pool.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@ import (
3636
// PooledDBConnection objects.
3737
type ConnectionPool struct {
3838
*smartconnpool.ConnPool[*DBConnection]
39+
40+
name string
3941
}
4042

43+
// usedNames is for preventing expvar from panicking. Tests
44+
// create pool objects multiple time. If a name was previously
45+
// used, expvar initialization is skipped.
46+
// through non-test code.
47+
var usedNames = make(map[string]bool)
48+
4149
// NewConnectionPool creates a new ConnectionPool. The name is used
4250
// to publish stats only.
4351
func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleTimeout time.Duration, maxLifetime time.Duration, dnsResolutionFrequency time.Duration) *ConnectionPool {
@@ -47,7 +55,18 @@ func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleT
4755
MaxLifetime: maxLifetime,
4856
RefreshInterval: dnsResolutionFrequency,
4957
}
50-
return &ConnectionPool{ConnPool: smartconnpool.NewPool(&config)}
58+
cp := &ConnectionPool{ConnPool: smartconnpool.NewPool(&config), name: name}
59+
if name == "" || usedNames[name] {
60+
return cp
61+
}
62+
usedNames[name] = true
63+
64+
if stats == nil {
65+
// This is unnamed exported so it will use the stats functions directly when adding to the expvar.
66+
stats = servenv.NewExporter("", "")
67+
}
68+
cp.ConnPool.RegisterStats(stats, name)
69+
return cp
5170
}
5271

5372
// Open must be called before starting to use the pool.

go/vt/vttablet/endtoend/config_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,29 @@ func TestQueryTimeout(t *testing.T) {
279279
compareIntDiff(t, vend, "Kills/Connections", vstart, 1)
280280
}
281281

282+
// TestHeartbeatMetric validates the heartbeat metrics exists from the connection pool.
283+
func TestHeartbeatMetric(t *testing.T) {
284+
tcases := []struct {
285+
metricName string
286+
exp any
287+
}{{
288+
metricName: "HeartbeatWriteAppPoolCapacity",
289+
exp: 2,
290+
}, {
291+
metricName: "HeartbeatWriteAllPrivsPoolCapacity",
292+
exp: 2,
293+
}}
294+
295+
metrics := framework.DebugVars()
296+
for _, tcase := range tcases {
297+
t.Run(tcase.metricName, func(t *testing.T) {
298+
mValue, exists := metrics[tcase.metricName]
299+
require.True(t, exists, "metric %s not found", tcase.metricName)
300+
require.EqualValues(t, tcase.exp, mValue, "metric %s value is %d, want %d", tcase.metricName, mValue, tcase.exp)
301+
})
302+
}
303+
}
304+
282305
func changeVar(t *testing.T, name, value string) (revert func()) {
283306
t.Helper()
284307

0 commit comments

Comments
 (0)