Skip to content

Commit

Permalink
feat: add etcd_debugging_lease_total gauge metric
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy- <zhenguo251@gmail.com>
  • Loading branch information
jimmy-bro committed May 28, 2024
1 parent a529268 commit 81ae9e4
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/lease/lessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (le *lessor) Grant(id LeaseID, ttl int64) (*Lease, error) {

leaseTotalTTLs.Observe(float64(l.ttl))
leaseGranted.Inc()
leaseActive.Inc()

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

leaseRevoked.Inc()
leaseActive.Dec()
return nil
}

Expand Down Expand Up @@ -812,6 +814,7 @@ func (le *lessor) initAndRecover() {
}
le.leaseExpiredNotifier.Init()
heap.Init(&le.leaseCheckpointHeap)
leaseActive.Set(float64(len(le.leaseMap)))

le.b.ForceCommit()
}
Expand Down
8 changes: 8 additions & 0 deletions server/lease/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ var (
// 1 second -> 3 months
Buckets: prometheus.ExponentialBuckets(1, 2, 24),
})

leaseActive = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "etcd_debugging",
Subsystem: "lease",
Name: "active",
Help: "The current number of active leases.",
})
)

func init() {
prometheus.MustRegister(leaseGranted)
prometheus.MustRegister(leaseRevoked)
prometheus.MustRegister(leaseRenewed)
prometheus.MustRegister(leaseTotalTTLs)
prometheus.MustRegister(leaseActive)
}
104 changes: 104 additions & 0 deletions server/lease/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2022 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lease

import (
"strings"
"testing"

"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
)

func TestLeaseGranted(t *testing.T) {
// Test the leaseGranted metric
expected := `
# HELP etcd_debugging_lease_granted_total The total number of granted leases.
# TYPE etcd_debugging_lease_granted_total counter
etcd_debugging_lease_granted_total 218
`
err := testutil.CollectAndCompare(leaseGranted, strings.NewReader(expected))
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
}

func TestNewLeaseRevoked(t *testing.T) {
// Test the leaseRevoked metric
expected := `
# HELP etcd_debugging_lease_revoked_total The total number of revoked leases.
# TYPE etcd_debugging_lease_revoked_total counter
etcd_debugging_lease_revoked_total 1
`
err := testutil.CollectAndCompare(leaseRevoked, strings.NewReader(expected))
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
}

func TestLeaseRenewed(t *testing.T) {
// Test the leaseRenewed metric
expected := `
# HELP etcd_debugging_lease_renewed_total The number of renewed leases seen by the leader.
# TYPE etcd_debugging_lease_renewed_total counter
etcd_debugging_lease_renewed_total 2
`
err := testutil.CollectAndCompare(leaseRenewed, strings.NewReader(expected))
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
}

func TestLeaseTotalTTLs(t *testing.T) {
// Test the leaseTotalTTLs metric
expected := `
# HELP etcd_debugging_lease_ttl_total Bucketed histogram of lease TTLs.
# TYPE etcd_debugging_lease_ttl_total histogram
etcd_debugging_lease_ttl_total_bucket{le="1"} 2
etcd_debugging_lease_ttl_total_bucket{le="2"} 3
etcd_debugging_lease_ttl_total_bucket{le="4"} 3
etcd_debugging_lease_ttl_total_bucket{le="8"} 7
etcd_debugging_lease_ttl_total_bucket{le="16"} 214
etcd_debugging_lease_ttl_total_bucket{le="32"} 215
etcd_debugging_lease_ttl_total_bucket{le="64"} 215
etcd_debugging_lease_ttl_total_bucket{le="128"} 218
etcd_debugging_lease_ttl_total_bucket{le="256"} 218
etcd_debugging_lease_ttl_total_bucket{le="512"} 218
etcd_debugging_lease_ttl_total_bucket{le="1024"} 218
etcd_debugging_lease_ttl_total_bucket{le="2048"} 218
etcd_debugging_lease_ttl_total_bucket{le="4096"} 218
etcd_debugging_lease_ttl_total_bucket{le="8192"} 218
etcd_debugging_lease_ttl_total_bucket{le="16384"} 218
etcd_debugging_lease_ttl_total_bucket{le="32768"} 218
etcd_debugging_lease_ttl_total_bucket{le="65536"} 218
etcd_debugging_lease_ttl_total_bucket{le="131072"} 218
etcd_debugging_lease_ttl_total_bucket{le="262144"} 218
etcd_debugging_lease_ttl_total_bucket{le="524288"} 218
etcd_debugging_lease_ttl_total_bucket{le="1.048576e+06"} 218
etcd_debugging_lease_ttl_total_bucket{le="2.097152e+06"} 218
etcd_debugging_lease_ttl_total_bucket{le="4.194304e+06"} 218
etcd_debugging_lease_ttl_total_bucket{le="8.388608e+06"} 218
etcd_debugging_lease_ttl_total_bucket{le="+Inf"} 218
etcd_debugging_lease_ttl_total_sum 2514
etcd_debugging_lease_ttl_total_count 218
`
err := testutil.CollectAndCompare(leaseTotalTTLs, strings.NewReader(expected))
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
}

func TestLeaseTotal(t *testing.T) {
// Test the leaseActive metric
expected := `
# HELP etcd_debugging_lease_active The current number of active leases.
# TYPE etcd_debugging_lease_active gauge
etcd_debugging_lease_active 1
`
err := testutil.CollectAndCompare(leaseActive, strings.NewReader(expected))
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
}

0 comments on commit 81ae9e4

Please sign in to comment.