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:
parent
5e2a6883c1
commit
228168cee9
@ -55,6 +55,7 @@ type Metrics struct {
|
||||
log logr.Logger
|
||||
registry *prometheus.Registry
|
||||
|
||||
clockTimeSeconds prometheus.CounterFunc
|
||||
certificateExpiryTimeSeconds *prometheus.GaugeVec
|
||||
certificateReadyStatus *prometheus.GaugeVec
|
||||
acmeClientRequestDurationSeconds *prometheus.SummaryVec
|
||||
@ -66,6 +67,17 @@ var readyConditionStatuses = [...]cmmeta.ConditionStatus{cmmeta.ConditionTrue, c
|
||||
|
||||
func New(log logr.Logger) *Metrics {
|
||||
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(
|
||||
prometheus.GaugeOpts{
|
||||
Namespace: namespace,
|
||||
@ -124,6 +136,7 @@ func New(log logr.Logger) *Metrics {
|
||||
log: log.WithName("metrics"),
|
||||
registry: prometheus.NewRegistry(),
|
||||
|
||||
clockTimeSeconds: clockTimeSeconds,
|
||||
certificateExpiryTimeSeconds: certificateExpiryTimeSeconds,
|
||||
certificateReadyStatus: certificateReadyStatus,
|
||||
acmeClientRequestCount: acmeClientRequestCount,
|
||||
@ -136,6 +149,7 @@ func New(log logr.Logger) *Metrics {
|
||||
|
||||
// Start will register the Prometheus metrics, and start the Prometheus server
|
||||
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.certificateReadyStatus)
|
||||
m.registry.MustRegister(m.acmeClientRequestDurationSeconds)
|
||||
|
||||
55
pkg/metrics/metrics_test.go
Normal file
55
pkg/metrics/metrics_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user