Skip to content

Commit 81ae9e4

Browse files
committed
feat: add etcd_debugging_lease_total gauge metric
Signed-off-by: Jimmy- <[email protected]>
1 parent a529268 commit 81ae9e4

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

server/lease/lessor.go

+3
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ func (le *lessor) Grant(id LeaseID, ttl int64) (*Lease, error) {
303303

304304
leaseTotalTTLs.Observe(float64(l.ttl))
305305
leaseGranted.Inc()
306+
leaseActive.Inc()
306307

307308
if le.isPrimary() {
308309
item := &LeaseWithTime{id: l.ID, time: l.expiry}
@@ -351,6 +352,7 @@ func (le *lessor) Revoke(id LeaseID) error {
351352
txn.End()
352353

353354
leaseRevoked.Inc()
355+
leaseActive.Dec()
354356
return nil
355357
}
356358

@@ -812,6 +814,7 @@ func (le *lessor) initAndRecover() {
812814
}
813815
le.leaseExpiredNotifier.Init()
814816
heap.Init(&le.leaseCheckpointHeap)
817+
leaseActive.Set(float64(len(le.leaseMap)))
815818

816819
le.b.ForceCommit()
817820
}

server/lease/metrics.go

+8
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ var (
4949
// 1 second -> 3 months
5050
Buckets: prometheus.ExponentialBuckets(1, 2, 24),
5151
})
52+
53+
leaseActive = prometheus.NewGauge(prometheus.GaugeOpts{
54+
Namespace: "etcd_debugging",
55+
Subsystem: "lease",
56+
Name: "active",
57+
Help: "The current number of active leases.",
58+
})
5259
)
5360

5461
func init() {
5562
prometheus.MustRegister(leaseGranted)
5663
prometheus.MustRegister(leaseRevoked)
5764
prometheus.MustRegister(leaseRenewed)
5865
prometheus.MustRegister(leaseTotalTTLs)
66+
prometheus.MustRegister(leaseActive)
5967
}

server/lease/metrics_test.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2022 The etcd Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package lease
16+
17+
import (
18+
"strings"
19+
"testing"
20+
21+
"github.com/prometheus/client_golang/prometheus/testutil"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestLeaseGranted(t *testing.T) {
26+
// Test the leaseGranted metric
27+
expected := `
28+
# HELP etcd_debugging_lease_granted_total The total number of granted leases.
29+
# TYPE etcd_debugging_lease_granted_total counter
30+
etcd_debugging_lease_granted_total 218
31+
`
32+
err := testutil.CollectAndCompare(leaseGranted, strings.NewReader(expected))
33+
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
34+
}
35+
36+
func TestNewLeaseRevoked(t *testing.T) {
37+
// Test the leaseRevoked metric
38+
expected := `
39+
# HELP etcd_debugging_lease_revoked_total The total number of revoked leases.
40+
# TYPE etcd_debugging_lease_revoked_total counter
41+
etcd_debugging_lease_revoked_total 1
42+
`
43+
err := testutil.CollectAndCompare(leaseRevoked, strings.NewReader(expected))
44+
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
45+
}
46+
47+
func TestLeaseRenewed(t *testing.T) {
48+
// Test the leaseRenewed metric
49+
expected := `
50+
# HELP etcd_debugging_lease_renewed_total The number of renewed leases seen by the leader.
51+
# TYPE etcd_debugging_lease_renewed_total counter
52+
etcd_debugging_lease_renewed_total 2
53+
`
54+
err := testutil.CollectAndCompare(leaseRenewed, strings.NewReader(expected))
55+
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
56+
}
57+
58+
func TestLeaseTotalTTLs(t *testing.T) {
59+
// Test the leaseTotalTTLs metric
60+
expected := `
61+
# HELP etcd_debugging_lease_ttl_total Bucketed histogram of lease TTLs.
62+
# TYPE etcd_debugging_lease_ttl_total histogram
63+
etcd_debugging_lease_ttl_total_bucket{le="1"} 2
64+
etcd_debugging_lease_ttl_total_bucket{le="2"} 3
65+
etcd_debugging_lease_ttl_total_bucket{le="4"} 3
66+
etcd_debugging_lease_ttl_total_bucket{le="8"} 7
67+
etcd_debugging_lease_ttl_total_bucket{le="16"} 214
68+
etcd_debugging_lease_ttl_total_bucket{le="32"} 215
69+
etcd_debugging_lease_ttl_total_bucket{le="64"} 215
70+
etcd_debugging_lease_ttl_total_bucket{le="128"} 218
71+
etcd_debugging_lease_ttl_total_bucket{le="256"} 218
72+
etcd_debugging_lease_ttl_total_bucket{le="512"} 218
73+
etcd_debugging_lease_ttl_total_bucket{le="1024"} 218
74+
etcd_debugging_lease_ttl_total_bucket{le="2048"} 218
75+
etcd_debugging_lease_ttl_total_bucket{le="4096"} 218
76+
etcd_debugging_lease_ttl_total_bucket{le="8192"} 218
77+
etcd_debugging_lease_ttl_total_bucket{le="16384"} 218
78+
etcd_debugging_lease_ttl_total_bucket{le="32768"} 218
79+
etcd_debugging_lease_ttl_total_bucket{le="65536"} 218
80+
etcd_debugging_lease_ttl_total_bucket{le="131072"} 218
81+
etcd_debugging_lease_ttl_total_bucket{le="262144"} 218
82+
etcd_debugging_lease_ttl_total_bucket{le="524288"} 218
83+
etcd_debugging_lease_ttl_total_bucket{le="1.048576e+06"} 218
84+
etcd_debugging_lease_ttl_total_bucket{le="2.097152e+06"} 218
85+
etcd_debugging_lease_ttl_total_bucket{le="4.194304e+06"} 218
86+
etcd_debugging_lease_ttl_total_bucket{le="8.388608e+06"} 218
87+
etcd_debugging_lease_ttl_total_bucket{le="+Inf"} 218
88+
etcd_debugging_lease_ttl_total_sum 2514
89+
etcd_debugging_lease_ttl_total_count 218
90+
`
91+
err := testutil.CollectAndCompare(leaseTotalTTLs, strings.NewReader(expected))
92+
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
93+
}
94+
95+
func TestLeaseTotal(t *testing.T) {
96+
// Test the leaseActive metric
97+
expected := `
98+
# HELP etcd_debugging_lease_active The current number of active leases.
99+
# TYPE etcd_debugging_lease_active gauge
100+
etcd_debugging_lease_active 1
101+
`
102+
err := testutil.CollectAndCompare(leaseActive, strings.NewReader(expected))
103+
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
104+
}

0 commit comments

Comments
 (0)