Add clock_time_seconds metric

Fixes: https://github.com/jetstack/cert-manager/issues/3730
Related: https://github.com/jetstack/cert-manager/pull/3746

Signed-off-by: kit837 <66801824+kit837@users.noreply.github.com>
This commit is contained in:
kit837 2021-06-14 20:23:18 +00:00
parent 5e2a6883c1
commit 228168cee9
2 changed files with 69 additions and 0 deletions

View File

@ -55,6 +55,7 @@ type Metrics struct {
log logr.Logger log logr.Logger
registry *prometheus.Registry registry *prometheus.Registry
clockTimeSeconds prometheus.CounterFunc
certificateExpiryTimeSeconds *prometheus.GaugeVec certificateExpiryTimeSeconds *prometheus.GaugeVec
certificateReadyStatus *prometheus.GaugeVec certificateReadyStatus *prometheus.GaugeVec
acmeClientRequestDurationSeconds *prometheus.SummaryVec acmeClientRequestDurationSeconds *prometheus.SummaryVec
@ -66,6 +67,17 @@ var readyConditionStatuses = [...]cmmeta.ConditionStatus{cmmeta.ConditionTrue, c
func New(log logr.Logger) *Metrics { func New(log logr.Logger) *Metrics {
var ( var (
clockTimeSeconds = prometheus.NewCounterFunc(
prometheus.CounterOpts{
Namespace: namespace,
Name: "clock_time_seconds",
Help: "The clock time given in seconds (from 1970/01/01 UTC).",
},
func() float64 {
return float64(time.Now().Unix())
},
)
certificateExpiryTimeSeconds = prometheus.NewGaugeVec( certificateExpiryTimeSeconds = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Namespace: namespace, Namespace: namespace,
@ -124,6 +136,7 @@ func New(log logr.Logger) *Metrics {
log: log.WithName("metrics"), log: log.WithName("metrics"),
registry: prometheus.NewRegistry(), registry: prometheus.NewRegistry(),
clockTimeSeconds: clockTimeSeconds,
certificateExpiryTimeSeconds: certificateExpiryTimeSeconds, certificateExpiryTimeSeconds: certificateExpiryTimeSeconds,
certificateReadyStatus: certificateReadyStatus, certificateReadyStatus: certificateReadyStatus,
acmeClientRequestCount: acmeClientRequestCount, acmeClientRequestCount: acmeClientRequestCount,
@ -136,6 +149,7 @@ func New(log logr.Logger) *Metrics {
// Start will register the Prometheus metrics, and start the Prometheus server // Start will register the Prometheus metrics, and start the Prometheus server
func (m *Metrics) Start(listenAddress string, enablePprof bool) (*http.Server, error) { func (m *Metrics) Start(listenAddress string, enablePprof bool) (*http.Server, error) {
m.registry.MustRegister(m.clockTimeSeconds)
m.registry.MustRegister(m.certificateExpiryTimeSeconds) m.registry.MustRegister(m.certificateExpiryTimeSeconds)
m.registry.MustRegister(m.certificateReadyStatus) m.registry.MustRegister(m.certificateReadyStatus)
m.registry.MustRegister(m.acmeClientRequestDurationSeconds) m.registry.MustRegister(m.acmeClientRequestDurationSeconds)

View File

@ -0,0 +1,55 @@
/*
Copyright 2020 The cert-manager 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 metrics
import (
"fmt"
"strings"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus/testutil"
logtesting "github.com/jetstack/cert-manager/pkg/logs/testing"
)
func TestClockMetrics(t *testing.T) {
type testT struct {
expectedFmt string
}
tests := map[string]testT{
"clock time seconds as expected": {
expectedFmt: `
# HELP certmanager_clock_time_seconds The clock time given in seconds (from 1970/01/01 UTC).
# TYPE certmanager_clock_time_seconds counter
certmanager_clock_time_seconds %f
`,
},
}
for n, test := range tests {
t.Run(n, func(t *testing.T) {
m := New(logtesting.TestLogger{T: t})
if err := testutil.CollectAndCompare(m.clockTimeSeconds,
strings.NewReader(fmt.Sprintf(test.expectedFmt, float64(time.Now().Unix()))),
"certmanager_clock_time_seconds",
); err != nil {
t.Errorf("unexpected collecting result:\n%s", err)
}
})
}
}