diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 108fdb5d5..b76a46108 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -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) diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go new file mode 100644 index 000000000..2ff2a181b --- /dev/null +++ b/pkg/metrics/metrics_test.go @@ -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) + } + }) + } +}