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 27, 2024
1 parent a529268 commit d2bf743
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
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),
})

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

func init() {
prometheus.MustRegister(leaseGranted)
prometheus.MustRegister(leaseRevoked)
prometheus.MustRegister(leaseRenewed)
prometheus.MustRegister(leaseTotalTTLs)
prometheus.MustRegister(leaseTotal)
}
119 changes: 119 additions & 0 deletions server/lease/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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
// This is a counter metric, which means it can only be incremented
// The metric is incremented by 10 and we check if it is 10
leaseGranted.Add(10)
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 10
`
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
// This is a counter metric, which means it can only be incremented
// The metric is incremented by 10 and we check if it is 10
leaseRevoked.Add(10)
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 10
`
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
// This is a counter metric, which means it can only be incremented
// The metric is incremented by 10 and we check if it is 10
leaseRenewed.Add(10)
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 10
`
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
// This is a histogram metric, which means it can be incremented and observed
// The metric is incremented by 10 and we check if it is 10
leaseTotalTTLs.Observe(10)
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"} 0
etcd_debugging_lease_ttl_total_bucket{le="2"} 0
etcd_debugging_lease_ttl_total_bucket{le="4"} 0
etcd_debugging_lease_ttl_total_bucket{le="8"} 0
etcd_debugging_lease_ttl_total_bucket{le="16"} 1
etcd_debugging_lease_ttl_total_bucket{le="32"} 1
etcd_debugging_lease_ttl_total_bucket{le="64"} 1
etcd_debugging_lease_ttl_total_bucket{le="128"} 1
etcd_debugging_lease_ttl_total_bucket{le="256"} 1
etcd_debugging_lease_ttl_total_bucket{le="512"} 1
etcd_debugging_lease_ttl_total_bucket{le="1024"} 1
etcd_debugging_lease_ttl_total_bucket{le="2048"} 1
etcd_debugging_lease_ttl_total_bucket{le="4096"} 1
etcd_debugging_lease_ttl_total_bucket{le="8192"} 1
etcd_debugging_lease_ttl_total_bucket{le="16384"} 1
etcd_debugging_lease_ttl_total_bucket{le="32768"} 1
etcd_debugging_lease_ttl_total_bucket{le="65536"} 1
etcd_debugging_lease_ttl_total_bucket{le="131072"} 1
etcd_debugging_lease_ttl_total_bucket{le="262144"} 1
etcd_debugging_lease_ttl_total_bucket{le="524288"} 1
etcd_debugging_lease_ttl_total_bucket{le="1.048576e+06"} 1
etcd_debugging_lease_ttl_total_bucket{le="2.097152e+06"} 1
etcd_debugging_lease_ttl_total_bucket{le="4.194304e+06"} 1
etcd_debugging_lease_ttl_total_bucket{le="8.388608e+06"} 1
etcd_debugging_lease_ttl_total_bucket{le="+Inf"} 1
etcd_debugging_lease_ttl_total_sum 10
etcd_debugging_lease_ttl_total_count 1
`
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 leaseTotal metric
// This is a gauge metric, which means it can be set to any value
// The metric is set to 10 and we check if it is 10
leaseTotal.Set(10)
expected := `
# HELP etcd_debugging_lease_total The total number of active leases.
# TYPE etcd_debugging_lease_total gauge
etcd_debugging_lease_total 10
`
err := testutil.CollectAndCompare(leaseTotal, strings.NewReader(expected))
require.NoError(t, err, "Collected metrics did not match expected metrics: %v", err)
}

0 comments on commit d2bf743

Please sign in to comment.